1. getMapper() interface
Analysis: getMapper() interface IDept.class defines an interface,
Mounting a method that is not implemented. In special terms, any method that borrows the building must be consistent with the id attribute in the small configuration.
Through proxy: generate the implementation class name of the interface, maintain the name $$Dept_abc, selectDeptByNo() in the underlying MyBatis
It's equivalent to a strong type
Eg
Step 1: Define an interface in cn.happy.dao
package cn.happy.dao;import java.util.List;import cn.happy.entity.Dept;public interface IDeptDao {//View all------------getAllDept should be the same as the id in the small configuration public List<Dept> getAllDept();} Step 2: IDept.xml configuration small configuration
Analysis: The Id attribute in select should be the same as the interface method name in the interface; the namespace attribute package name of mapper is cn.happy.dao.IDeptDao interface
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.happy.dao.IDeptDao"><select id="getAllDept" resultType="cn.happy.entity.Dept">select * from Dept </select></mapper>
Step 3: Test Class
Analysis: There are two ways to view all information
1) session.selectList("cn.happy.dao.IDeptDao.getAllDept");---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2) IDeptDao mapper = session.getMapper(IDeptDao.class); is equivalent to the implementation class, getMapper is a strong type
// 01View all information GetMapper() The method name of the interface class should be the same as the id of the small configuration @Testpublic void testSelectAll() {SqlSession session = factory.openSession();//Use weak type ======== Entity class. The Id name in the small configuration ============ String/*List<Dept> list = session.selectList("cn.happy.dao.IDeptDao.getAllDept");for (Dept dept : list) {System.out.println(dept.getDeptName());}*/// Use getMapper method HIbernate to help us proxy an interface implementation class in memory ===============Equivalent to strong type //mapper is an implementation class object IDeptDao mapper = session.getMapper(IDeptDao.class);List<Dept> list = mapper.getAllDept(); for (Dept dept : list) {System.out.println(dept.getDeptName());} Step 4: Use a large configuration in the whole text
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- Change the attribute value of the type in the Alias alias small configuration to the alias--><typeAliases><typeAlias type="cn.resultMap.enetity.Emp" alias="emp"/></typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="oracle.jdbc.OracleDriver" /><property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /><property name="username" value="sa" /><property name="password" value="1" /></dataSource></environment></environments><!--Mapping file: Describe the correspondence between an entity and a database table--><mappers><mapper resource="cn/resultMap/enetity/Emp.xml" /></mappers></configuration>
2. resultMap tag
Analysis: The scenario used is that when the properties of the entity class do not match the database, the properties of the entity class and the database must be consistent. (I used entity class before)
Eg searches all employees and affiliated departments
Step 1: Create an interface
package cn.resultMap.dao;import java.util.List;import cn.resultMap.enetity.Emp;public interface IEmpDao {//Retrieve all employees and affiliated departments public List<Emp> getAllEmps();} Step 2: Configure the properties in the small configuration
Analysis: For one party with a large employee perspective, please use association to embed each attribute of one party. (If the association is removed, it is the basic resultMap)
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.resultMap.dao.IEmpDao"><resultMap type="cn.resultMap.enetity.Emp" id="empMap"><id property="empId" column="EMPID"/><result property="empName" column="EMPNAME"/><result property="empCity" column="EMPCITY"/><!-- For each party with a large employee perspective, please use association --><association property="dept" javaType="cn.resultMap.enetity.Dept"><result property="deptName" column="DEPTNAME"/><result property="deptNo" column="DEPTNO"/></association></resultMap><select id="getAllEmps" resultMap="empMap">select e.*,d.* from Emp e,Dept dwhere e.deptNo=d.deptNo</select></mapper>
Step 3: Test Class
//resultMap: The attribute name of the entity and the field name of the table are guaranteed to be consistent with resultMap//If you report NullException, see if the mapping association of the small configuration is configured. resultMap is configured @Testpublic void testAllEmp(){SqlSession session=factory.openSession();IEmpDao mapper = session.getMapper(IEmpDao.class);List<Emp> allEmps = mappper.getAllEmps(); for (Emp emp : allEmps) {System.out.println(emp.getEmpName()+"/t affiliate department"+emp.getDept().getDeptName());}session.close();} Step 4: Introduce small configurations in large configurations
3. Extract SQL columns
Analysis: Sql tag simplifies the code volume in small configuration
<!-- Use of SQl tags--><sql id="columns">d.deptNo,d.deptName</sql><!-- Use of SQl tags--><select id="getAllEmps" resultMap="empMap">select e.*,<include refid="columns"/> from Emp e,Dept dwhere e.deptNo=d.deptNo</select>
4. Alias alias
Analysis: Write on large configurations, so that alias can be referenced in small configurations
<!-- Change the attribute value of type in the Alias alias configuration to alias--><typeAliases><typeAlias type="cn.resultMap.enetity.Emp" alias="emp"/></typeAliases>
5. Dynamic operation
Analysis: The main elements used to implement dynamic SQL are:
if choose(when,otherwise) where set
Eg View people in Beijing city
Step 1: Interface
package cn.resultMap.dao;import java.util.List;import cn.resultMap.enetity.Emp;public interface IEmpDao {//Retrieve all employees and affiliated departments public List<Emp> getAllEmps();}Step 2: Small version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.resultMap.dao.IEmpDao"><resultMap type="cn.resultMap.enetity.Emp" id="empMap"><id property="empId" column="EMPID"/><result property="empName" column="EMPNAME"/><result property="empCity" column="EMPCITY"/><!-- For one party with a large employee perspective, please use association --><association property="dept" javaType="cn.resultMap.enetity.Dept"><result property="deptName" column="DEPTNAME"/><result property="deptNo" column="DEPTNO"/></resultMap><select id="getAllEmps" resultMap="empMap">select e.*,d.* from Emp e,Dept dwhere e.deptNo=d.deptNo</select><!--Query Dynamic Query--><select id="testAllEmpBuSelect" parameterType="cn.resultMap.enetity.Emp" resultType="cn.resultMap.enetity.Emp" resultType="cn.resultMap.enetity.Emp">select * from Emp<where><if test="empId!=null">and empId=#{empId}</if><if test="empName!=null">and empName=#{empName}</if><if test="empCity!=null">and empCity=#{empCity}</if></where></select></mapper> Step 3: Test
//Dynamic query @Testpublic void testSelect(){SqlSession session=factory.openSession();Emp emp=new Emp();//emp.setEmpName("331");emp.setEmpCity("sh");List<Emp> list = session.selectList("cn.resultMap.dao.IEmpDao.testAllEmpBuSelect",emp);for (Emp emps : list) {System.out.println(emps.getEmpName());}session.close();} Step 4: Introduce small configurations in large configurations
Eg Modify department information
Step 1: Interface
Step 2: Small configuration
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.resultMap.dao.IDeptDao"><resultMap type="cn.happy.entity.Dept" id="deptResultMap"><id property="deptNo" column="deptNo"/><result property="deptName" column="deptName"/></resultMap><select id="getAllDept" resultMap="deptResultMap">select d.*, e.* from Dept d,Emp ewhere d.deptNo=e.deptNo and d.deptNo=#{deptNo}</select><!--Modify dynamic query--><select id="testUpdate" parameterType="int" resultType="cn.resultMap.enetity.Dept">update dept<set><if test="deptNo!=null">deptNo=#{deptNo},</if><if test="deptName!=null">deptName=#{deptName},</if></set>where deptNo=#{deptNo}</select></mapper> Step 3: Test
/*** Dynamic modification* */@Testpublic void testUpdate(){SqlSession session=factory.openSession();Dept dept=new Dept();dept.setDeptName("Financial Department");dept.setDeptNo(1);int count = session.update("cn.resultMap.dao.IDeptDao.testUpdate",dept);session.commit();System.out.println(count);session.close();}The above is a detailed explanation of MyBatis' getMapper() interface, resultMap tag, Alias alias, try to extract SQL columns, and dynamic operations. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!