1. Delay loading
resultMap can implement advanced mapping (using association and collection to implement one-to-one and one-to-many mapping). The association and collection have lazy loading functions.
Delay loading: First query from a single table, and then associate query from the associated table when needed, greatly improving database performance, because querying a single table is faster than querying multiple tables.
Configure in mybatis core configuration file:
lazyLoadingEnabled, aggressiveLazyLoading
Settings | describe | Allowed values | default value |
lazyLoadingEnabled | Global settings lazy loading. If set to 'false', all associated ones will be initialized and loaded. | true | false | false |
aggressiveLazyLoading | When set to 'true', lazy loading objects may be loaded by all lazy properties. Otherwise, each property is loaded as needed. | true | false | true |
<settings> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/></settings>
occasion:
When only some records need to be associated query other information, they can be delayed loading as needed. When the associated query is required, SQL will be issued to the database to improve database performance.
When all the associated query information is needed, there is no need to delay loading at this time, just return all the associated query information. You can use resultType or resultMap to complete the mapping.
Two: Case: (One-to-many in department and employees)
Source code introduction:
1.Dept.java
package cn.zhang.entity;import java.util.HashSet;import java.util.Set;public class Dept { private Integer deptno; private String deptname; private Set<Emp> emp = new HashSet<Emp>(); @Override public String toString() { return "Deptno=" + deptno + ", deptname=" + deptname + ", emp=" + emp + "]"; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public String getDeptname() { return deptname; } public void setDeptname(String deptname) { this.deptname = deptname; } public Set<Emp> getEmp() { return emp; } public void setEmp(Set<Emp> emp) { this.emp = emp; }}2.Emp.java
package cn.zhang.entity;public class Emp { private Integer empno; private String empname; @Override public String toString() { return "Emp [empno=" + empno + ", empname=" + empname + "]"; } public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEmpname() { return empname; } public void setEmpname(String empname) { this.empname = empname; }}3.MybatisUtil.java
package cn.zhang.util;import java.io.IOException;import java.io.Reader;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;/** * Tool class* */public class MybatisUtil { private static String config = "mybatis-config.xml"; static Reader reader; static { try { reader = Resources.getResourceAsReader(config); } catch (IOException e) { e.printStackTrace(); } } private static SqlSessionFactory factory = new SqlSessionFactoryBuilder() .build(reader); // Provide a method that can get the session public static SqlSession getSession() throws IOException { SqlSession session = factory.openSession(); return session; }}4.DeptDao.java
package cn.zhang.dao;import java.io.IOException;import cn.zhang.entity.Dept;public interface DeptDao { /** * Query the specified record* @return * @throws IOException */ public Dept findById(Integer id) throws IOException;}5.DeptDAO.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mappperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.zhang.dao.DeptDao"> <!-- 3. Query employee information based on employee id--> <select id="selectEmpByDeptNo" resultType="Emp"> select empno,empname from emp where deptno=#{deptno} </select> <!-- 2. Mapping of department entities --> <resultMap type="Dept" id="deptMapper"> <id property="deptno" column="deptno" /> <result property="deptname" column="deptname" /> <!-- One-to-many department-related employees --> <!--select: Associated employee query --> <!--column: Conditions required for associate employee query (source from 1) --> <collection property="emp" ofType="Emp" select="selectEmpByDeptNo" column="deptno" /> </resultMap> <!--1. Query department information based on department id --> <select id="findById" resultMap="deptMapper"> select deptno,deptname from dept where deptno=#{deptno} </select></mapper>6.mybatis-config.xml (the configuration for lazy loading is here)
<?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> <!--lazyLoadingEnabled: Set lazy loading, default to false. If false: then all associated will be initialized and loaded. aggressiveLazyLoading: default is true. When set to true, lazy-loaded objects may be loaded by any lazy attributes; otherwise, each attribute will be loaded as needed. --> <settings> <!-- Turn on the delayed loading switch-> <setting name="lazyLoadingEnabled" value="true" /> <!-- Change active loading to message loading that is loading as required-> <setting name="aggressiveLazyLoading" value="false" /> </settings> <!--Configuration alias--> <typeAliases> <!--Method 1: Customize alias by type name--> <!--Method 2: Use the simple class name under the currently specified package as the alias--> <package name="cn.zhang.entity" /> </typeAliases> <environments default="oracle"> <environment id="oracle"> <!-- Transactions using jdbc --> <transactionManager type="JDBC" /> <!-- Using the built-in connection pool --> <dataSource type="POOLED"> <!-- The Oracle database I use --> <property name="driver" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name="username" value="study" /> <property name="password" value="123" /> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/zhang/dao/DeptDAO.xml" /> </mappers></configuration>
7.MyTest.java (test class)
package cn.zhang.test;//One-to-many import java.io.IOException;import org.apache.ibatis.session.SqlSession;import org.junit.Before;import org.junit.Test;import cn.zhang.dao.DeptDao;import cn.zhang.entity.Dept;import cn.zhang.util.MybatisUtil;public class MyTest { DeptDao dao; @Before public void initData() throws IOException{ SqlSession session = MybatisUtil.getSession(); dao = session.getMapper(DeptDao.class); } /** * Query the specified record* @throws IOException */ @Test public void findAll() throws IOException{ Dept dept = dao.findById(1); System.out.println(dept); }}Test results:
Break point below
Situation 1: No configuration in mybatis-config.xml
Situation 2: Configure in mybatis-config.xml
<settings> <!-- Turn on the delayed loading switch--> <setting name="lazyLoadingEnabled" value="true" /> <!-- Change active loading to message loading that is loading as needed--> <setting name="aggressiveLazyLoading" value="false" /></settings>
Next step:
F6 next step:
F6 next step: type the employee's name
Situation 3:
F6 Next Step:
F6 Next: Print out the employee’s name
The above is the delay loading in Mybatis introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!