MyBatis configuration, mybatis configuration file
Compile the MyBatis configuration file (the configuration file can be found in the PDF file downloaded from the compressed package root, which also contains the sample configuration)
Emp. xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="cn.happy.dao.EmpDao"> 6 <resultMap id="empMap" type="cn.happy.entity.Emp"> 7 <id property="empId" column="empNo"/> 8 <result property="empName" column="ENAME"/> 9 <result property="job"/>10 <result property="mgr"/>11 <result property="hiredate"/>12 <result property="sal"/>13 <result property="comm"/>14 <association property="dept" javaType="cn.happy.entity.Dept">15 <id property="deptNo"/>16 <result property="deptName" column="DNAME"/>17 <result property="loc"/>18 </association>19 </resultMap>20 21 <select id="getAllEmpInfo" resultMap="empMap">22 select * from Emp e,Dept d where e.deptno=d.deptno23 </select>24 25 <!-- <select id="countAll" resultType="int" > 26 select count(*) from Emp27 </select> -->28 </mapper>
The functions of several common elements are as follows: (1. environment and 2. mappers)
1. environment Element: used to configure multiple data environments, so that multiple database information can be mapped. Use default to specify which database environment is used by default. Environment is the specific configuration of each database environment,
Includes data connection information and transaction management information.
There are two Transaction Manager types in MyBatis (that is, type = "[JDBC | MANAGED]")
JDBC: directly use JDBC to control transactions. It relies on the connection obtained from the database to manage the transaction scope.
MANAGED: MyBatis is not responsible for controlling transactions. It allows containers to manage the entire lifecycle of transactions (such as through Spring or javaEE Application Server)
There are three data source types: UNPOOLED, POOLED, and JNDI.
UNPOOLED: the implementation of this data source is to simply open and close the connection every time a request is made, it is a little slow, this is a good choice for simple applications, because it does not require timely available connections.
POOLED: Use a connection pool to avoid the initial connection and authentication time required to create a new connection instance. This is a popular method for Web applications to quickly respond to requests.
JNDI: This data source is implemented to use containers such as Spring or application server. Containers can configure data sources centrally or externally. MyBatis obtains the data source through JNDI.
2. mappers element: contains a list of all mapper, telling MyBatis where to find the SQL statement. Mapper mainly defines the SQL ing file.
03. Create a materialized class and a database interface (Dept class is demonstrated here .. Few fields, good operation !)
1 public class Dept {2 private Integer deptNo; 3 4 private String deptName; 5 6 private String loc;
(Omit other getter () and Setter ()...) 7}
04. Create an SQL Er (DeptMapper. xml adds the configuration for adding, deleting, modifying, and querying the corresponding database (fuzzy query): Non-ResultMap)
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="cn.happy.dao.DeptDao"> 6 7 <select id="getAllDept" resultType="cn.happy.entity.Dept"> 8 select * from Dept 9 </select>10 11 <insert id="addDept" parameterType="cn.happy.entity.Dept">12 insert into dept values(SEQ_NUM.Nextval,#{deptName},#{loc})13 </insert>14 15 <delete id="delDept" parameterType="cn.happy.entity.Dept">16 delete from dept where deptNo=#{deptNo}17 </delete>18 <update id="">19 20 </update>21 <select id="ilikeDept" parameterType="cn.happy.entity.Dept" resultType="cn.happy.entity.Dept">22 select * from Dept where dname like '%${dname}%'23 </select>24 </mapper>
Simple operations for adding, deleting, modifying, and querying (fuzzy query) corresponding to the database:
1 package cn. happy. test; 2 3 import java. io. IOException; 4 import java. io. inputStream; 5 import java. util. list; 6 7 import org. apache. ibatis. io. resources; 8 import org. apache. ibatis. session. sqlSession; 9 import org. apache. ibatis. session. sqlSessionFactory; 10 import org. apache. ibatis. session. sqlSessionFactoryBuilder; 11 import org. junit. before; 12 import org. junit. test; 13 14 import cn. happy. dao. dep TDao; 15 import cn. happy. entity. dept; 16 import cn. happy. entity. emp; 17 18 public class Test3_MyBatis {19 SqlSession session; 20 @ Before 21 public void getInit () throws IOException {22 InputStream stream = Resources. getResourceAsStream ("mybatis-config.xml"); 23 SqlSessionFactory sf = new SqlSessionFactoryBuilder (). build (stream); 24 session = sf. openSession (); 25} 26 27/** 28 * ilikeDept 29 */3 0 @ Test 31 public void Test_ilikeDept () {32 Dept dept = new Dept (); 33 dept. setDname (""); 34 List <Dept> list = session. selectList ("ilikeDept", dept); 35 for (Dept item: list) {36 System. out. println (item); 37} 38 39 40} 41 42 43/** 44 * delDept 45 */46 @ Test 47 public void Test_delDept () {48 int count = session. delete ("delDept", 63); 49 session. commit (); 50 System. out. println (count); 51} 52 5 3/** 54 * addDept 55 */56 @ Test 57 public void Test_addDept () {58 Dept dept = new Dept (); 59 dept. setDname ("administrative"); 60 dept. setLoc ("channels"); 61 int count = session. insert ("addDept", dept); 62 session. commit (); 63 System. out. println (count + "\ tsave OK! "); 64} 65 66/** 67 * resultMap 68 */69 @ Test 70 public void Test_getAllEmpInfo () {71 List <Emp> list = session. selectList ("getAllEmpInfo"); 72 for (Emp item: list) {73 System. out. println (item. getEmpName (); 74} 75} 76 77/* 78 * condition query 79 */80 @ Test 81 public void Test03 () throws IOException {82 83 // List <Dept> list = session. selectList ("cn. happy. dao. deptDao. getAllDept "); 84 DeptDao mapper = session. getMapper (DeptDao. class); 85 List <Dept> list = mapper. getAllDept (); 86 87 for (Dept item: list) {88 System. out. println (item. getDeptNo (); 89} 90 // System. out. println (Dept) obj ). getDeptName (); 91 92} 93 94 95 96/* 97 * mybatis query employee table records 98 */99 @ Test100 public void Test02 () throws IOException {101 InputStream stream = Resources. getResourceAsStream ("mybatis-config.xml"); 102 SqlSessionFactory sf = new SqlSessionFactoryBuilder (). build (stream); 103 SqlSession session = sf. openSession (); 104 Object object = session. selectOne ("cn. happy. dao. empDao. countAll "); 105 System. out. println (object); 106 session. close (); 107} 108 109 110 @ Test111 public void Test01 () throws IOException {112 String resource = "mybatis-config.xml"; 113 InputStream inputStream = Resources. getResourceAsStream (resource); 114 // obtain the session factory 115 SqlSessionFactory sf = new SqlSessionFactoryBuilder (). build (inputStream); 116 // session 117 SqlSession session = sf. openSession (); 118 // use the namespace to uniquely lock the sql119 Dept dept = (Dept) session to be executed. selectOne ("cn. happy. entity. dept. selectDeptByNo "," 63 "); 120 System. out. println (dept); 121 session. close (); 122} 123 124}