1.概念
懒加载就是按需加载,我们需要什么的时候再去进行什么操作。而且先从单表查询,需要时再从关联表去关联查询,能很大提高数据库性能,因为查询单表要比关联查询多张表速度要快。
在mybatis中,resultMap可以实现高级映射(使用association、collection实现一对一及一对多映射),association、collection具备延迟加载功能。
2.demo
1.创建部门表和员工表(ps:这里员工表有一个外键映射的关系,实现一对多)
2.创建实体
@Data
public class Department {
private Integer id;
private String name;
private List<Employee> employeeList;
}
@Data
public class Employee {
private Integer id;
private String name;
private Integer departmentId;
}
3.创建dao和mapper(ps:fetchType="lazy",这个拉取类型是配置懒加载的关键)
/**
* 查询所有部门
* @return
*/
List<Department> getDepartment();
/**
* 根据部门ID查询员工
* @param departmentId
* @return
*/
List<Employee> getEmployeeByDepartmentId(Integer departmentId);
<!--查询所有部门-->
<select id="getDepartment" resultMap="departmentMap">
SELECT
*
FROM department
</select>
<!--部门映射结果集-->
<resultMap type="com.mljr.movies.entity.Department" id="departmentMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<collection property="employeeList" column="id"
javaType="ArrayList" ofType="com.mljr.movies.entity.Employee"
select="getEmployeeByDepartmentId" fetchType="lazy">
</collection>
</resultMap>
<!--根据部门ID查询员工-->
<select id="getEmployeeByDepartmentId" parameterType="java.lang.Integer" resultMap="employeeMap">
SELECT
*
FROM employee
where department_id = #{departmentId}
</select>
<!--员工映射结果集-->
<resultMap type="com.mljr.movies.entity.Employee" id="employeeMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="departmentId" column="department_id"/>
</resultMap>
3.测试
1.test
@Test
public void contextLoads() {
List<Department> departments = userDao.getDepartment();
System.out.println("=========only query department==========");
System.out.println("departmentName: "+departments.get(0).getName());
System.out.println("=========query department and employee==========");
System.out.println("employeeName: "+departments.get(0).getEmployeeList().get(0).getName());
}
2.总结
当配置懒加载(fetchType="lazy")时,可以看到控制台输出,在只查询部门时,仅查询部门sql,在需要获取员工时,才回去查询涉及到员工的sql。
JDBC Connection [HikariProxyConnection@1749758430 wrapping com.mysql.jdbc.JDBC4Connection@6b52dd31] will not be managed by Spring
==> Preparing: SELECT * FROM department
==> Parameters:
<== Columns: id, name
<== Row: 1, 开发部
<== Row: 2, 产品部
<== Row: 3, 人事部
<== Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@216914]
=========only query department==========
departmentName: 开发部
=========query department and employee==========
JDBC Connection [HikariProxyConnection@275002771 wrapping com.mysql.jdbc.JDBC4Connection@6b52dd31] will not be managed by Spring
==> Preparing: SELECT * FROM employee where department_id = ?
==> Parameters: 1(Integer)
<== Columns: id, name, department_id
<== Row: 1, 张三, 1
<== Row: 4, 王健, 1
<== Total: 2
employeeName: 张三
当不配置懒加载(fetchType="lazy")时,可以看到控制台输出,在结果集之前sql全部执行。
JDBC Connection [HikariProxyConnection@438897070 wrapping com.mysql.jdbc.JDBC4Connection@e042c99] will not be managed by Spring
==> Preparing: SELECT * FROM department
==> Parameters:
<== Columns: id, name
<== Row: 1, 开发部
====> Preparing: SELECT * FROM employee where department_id = ?
====> Parameters: 1(Integer)
<==== Columns: id, name, department_id
<==== Row: 1, 张三, 1
<==== Row: 4, 王健, 1
<==== Total: 2
<== Row: 2, 产品部
====> Preparing: SELECT * FROM employee where department_id = ?
====> Parameters: 2(Integer)
<==== Columns: id, name, department_id
<==== Row: 2, 李四, 2
<==== Total: 1
<== Row: 3, 人事部
====> Preparing: SELECT * FROM employee where department_id = ?
====> Parameters: 3(Integer)
<==== Columns: id, name, department_id
<==== Row: 3, 王二, 3
<==== Total: 1
<== Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@35764bef]
=========only query department==========
departmentName: 开发部
=========query department and employee==========
employeeName: 张三