MyBatis+SpringMVC+SpringBoot

5. MyBatis延迟加载

2019-03-30  本文已影响0人  飞扬code

5.1 什么是延迟加载

resultMap可以实现高级映射(使用association、collection实现一对一及一对多映射),association、collection具备延迟加载功能。

需求:

如果查询订单并且关联查询用户信息。如果先查询订单信息即可满足要求,当我们需要查询用户信息时再查询用户信息。把对用户信息的按需去查询就是延迟加载。

延迟加载:先从单表查询、需要时再从关联表去关联查询,大大提高 数据库性能,因为查询单表要比关联查询多张表速度要快。


5.2 使用association实现延迟加载

image.png

5.2.1 需求

查询订单并关联用户信息

5.2.2 mapper.xml

需要定义两个mapper的方法对应的statement。
1)只查询订单信息

    select * from emp;
    在查询订单的statement中使用association去延迟加载(执行)下边的satatement(关联查询用户信息)
    <!-- 查询员工信息关联部门,部门信息需要延迟加载 -->
    <select id="findEmpLazyLoading" resultMap="EmpLazyLoadingResultMap">
         select * from emp
    </select>

2)关联查询部门信息

 通过上面查询的部门编码dept_id去关联查询部门信息
    <select id="findEmpById" parameterType="int" resultType="emp">
         SELECT * FROM USER WHERE dept_id=#{value}
    </select>

上边先去执行findEmpLazyLoading,当需要去查询用户的时候再去执行findEmpById,通过resultMap的定义将延迟加载执行配置起来。

5.2.3 延迟加载resultMap

使用association中的select指定延迟加载去执行的statement的id。

    <!-- 延迟加载的resultMap  -->
    <resultMap type="com.demo.entity.Emp" id="EmpDeptLazyLoadingResultMap">
         <!-- 对员工信息进行映射配置 -->
         <!-- 实现对部门信息进行延迟加载 -->
         <id column="emp_id" property="empId"/>
         <result column="emp_name" property="empName"/>
         <result column="emp_sex" property="empSex"/>
         <result column="dept_id" property="deptId"/>
    <!-- 实现对员工信息进行延迟加载
         select:指定延迟加载需要执行的statement的id(是根据dept_id查询部门信息的statement)
         要使用DeptMapper.xml中findDeptById完成根据部门id(dept_id)查询部门信息,
         如果findEmpById不在本mapper中需要前边加namespace
         column:部门信息中关联到员工信息查询的列,是dept_id
         关联查询的sql理解为:
         select emp.*,
         (select dept_id from dept where emp.dept_id = dept.dept_id) dept_id,
         (select dept_name from dept where emp.dept_id = dept.dept_id) dept_name
         from emp
    -->
         <association property="dept" javaType="com.demo.entity.Dept"
             select="com.demo.entity.DeptMapper.selectDept" column="dept_id">
         </association>
    </resultMap>

Empmapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//[mybatis.org//DTD](http://mybatis.org//DTD) Mapper 3.0//EN" "[http://mybatis.org/dtd/mybatis-3-mapper.dtd](http://mybatis.org/dtd/mybatis-3-mapper.dtd)">
<mapper namespace="com.demo.entity.DeptMapper">
    <!--resultMap用于查询,可以把查询后字段值封装到对应类的属性, type指定的是对应的实体类 -->
    <resultMap type="Dept" id="deptResultMap">
         <!-- id用来配置表的主键与类的属性的映射关系 ,column指定的是表的字段名; property指定的是类的属性名-->
         <id column="dept_id" property="deptId"/>
         <!-- result用来配置 普通字段与类的属性的映射关系 ,column指定的是表的字段名; property指定的是类的属性名-->
         <result column="dept_name" property="deptName"/>
         <result column="dept_address" property="deptAddress"/>
    </resultMap>
    <!-- 查询单个部门信息 -->
    <select id="selectDept" parameterType="integer" resultMap="deptResultMap" >
         select dept_id,dept_name,dept_address from dept where dept_id = #{deptId}
    </select>
</mapper>

实现类:EmpDaoImpl.java

package com.demo.dao.imp;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.demo.entity.Emp;
import com.demo.util.MyBatisUtil;
public class EmpDaoImpl {
    SqlSession session;
        public List<Emp> findEmpLazyLoading() {
            // TODO Auto-generated method stub
            List<Emp> emps =null;
            try {
                session = MyBatisUtil.getSession();    
                emps = session.selectList("com.demo.entity.EmpMapper.findEmpDeptLazyLoading");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace()
            }finally{
                try {
                    MyBatisUtil.closeSession();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return emps;
        }
}

5.3 测试

5.3.1 测试思路:

1、执行上边mapper方法(findOrdersUserLazyLoading),内部去调用cn.itcast.mybatis.mapper.OrdersMapperCustom中的findOrdersUserLazyLoading只查询orders信息(单表)。
2、在程序中去遍历上一步骤查询出的List<Orders>,当我们调用Orders中的getUser方法时,开始进行延迟加载。
3、延迟加载,去调用UserMapper.xml中findUserbyId这个方法获取用户信息。

5.3.2 延迟加载配置

mybatis默认没有开启延迟加载,需要在SqlMapConfig.xml中setting配置。
在mybatis核心配置文件中配置:
lazyLoadingEnabled、aggressiveLazyLoading


image.png

在config.xml中配置:

    <!-- 引用db.properties配置文件 -->
    <properties resource="db.properties"/>
    <!-- 全局配置参数,需要时再设置 -->
    <settings>
         <!-- 打开延迟加载 的开关 -->
         <setting name="lazyLoadingEnabled" value="true"/>
         <!-- 将积极加载改为消极加载即按需要加载 -->
         <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

5.3.3 测试代码 TestEmpMapDept.java

package com.demo.test;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.demo.dao.imp.EmpDaoImpl;
import com.demo.entity.Dept;
import com.demo.entity.Emp;
public class TestEmpMapDept {
    private static EmpDaoImpl empDaoImpl;
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        empDaoImpl = new EmpDaoImpl();
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        empDaoImpl = null;
    }

    // 查询订单关联查询用户,用户信息使用延迟加载
    @Test
    public void testFindEmpLazyLoading() throws Exception {
        // 查询员工信息(单表)
        List<Emp> list = empDaoImpl.findEmpLazyLoading();
        // 遍历上边的员工列表
        for (Emp emp : list) {
            //System.out.println(emp.getDeptId());
            Dept dept = emp.getDept();
            System.out.println(dept);
        }
    }
}

测试效果:


image.png

5.3.4 延迟加载思考

不使用mybatis提供的association及collection中的延迟加载功能,如何实现延迟加载?
实现方法如下:
定义两个mapper方法:
1、查询订单列表
2、根据用户id查询用户信息

实现思路:
先去查询第一个mapper方法,获取订单信息列表
在程序中(service),按需去调用第二个mapper方法去查询用户信息。

总之:
使用延迟加载方法,先去查询简单的sql(最好单表,也可以关联查询),再去按需要加载关联查询的其它信息。

上一篇下一篇

猜你喜欢

热点阅读