MyBatis深入解读懒加载的实现

作者:羡羡ˇ 时间:2021-10-02 01:09:44 

懒加载 ,也称为嵌套查询

       需要查询关联信息时,使用 Mybatis 懒加载特性可有效的减少数据库压力, 首次查询只查询主表信息,关联表的信息在用户获取时再加载。        

       Mybatis 一对一关联的 association 和一对多的 collection 可以实现懒加载。懒加载时要 使用resultMap,不能使用 resultType 。

这里我们以员工表和部门表为例 

MyBatis深入解读懒加载的实现

通过deptId 与 部门表 id 关联

 我们这里首先需要开启一个设置

<settings>
   <!--指定哪些方法去触发延迟加载,hashCode,equals,clone,toString-->
   <setting name="lazyLoadTriggerMethods" value=""/>
</settings>

懒加载功能是默认开启的, 但这里我们也需要设置这个属性, 不设置则不会触发延迟加载功能

Employee selectOneEmployee(int id);

我们以查询单个员工为例 , resultMap 与sql 如下

<!--定义resultMap-->
<resultMap id="employeeMap1" type="Employee">
  <id column="id" property="id"/>
  <result property="name" column="name"/>
  <result property="age" column="age"/>
  <!--fetchType为查询的类型,这里选择lazy  select为嵌套查询-->
  <association property="dept" javaType="Dept" fetchType="lazy"
        select="selectDept" column="deptId">
  <result column="name" property="name"/>
  </association>
</resultMap>
<select id="selectOneEmployee" resultMap="employeeMap1">
  select id,name,age,deptId from employee where id=#{id}
</select>
<!--通过上一级sql提供的deptId查询-->
<select id="selectDept" resultType="Dept">
  select name from dept where id=#{deptId}
</select>

此处一对一 ,我们使用<association>

java测试 : 

public static void main(String[] args) {
  SqlSession sqlSession= MybatisUtil.getSqlSession();
  EmployeeDao mapper=sqlSession.getMapper(EmployeeDao.class);
  Employee employee = mapper.selectOneEmployee(3);
  System.out.println(employee);
  System.out.println(employee.getDept());
  sqlSession.commit();  //提交事务
  sqlSession.close();   //关闭

查询结果 :

MyBatis深入解读懒加载的实现

通过结果可以看到 , 当我们第一次输出这个 employee 对象时, 部门是没有被查询的 , 而当我们需要使用到部门的信息时, 才会去触发这个查询 

查询部门 resultMap 与 sql如下: 

<resultMap id="deptMap1" type="Dept">
   <id column="id" property="id"/>
   <result column="name" property="name"/>
   <!--collection为一对多 , 这里一个部门包含多个员工-->
   <collection property="list" javaType="List" ofType="Employee"
         select="selectEmployee" fetchType="lazy" column="id">
   <result property="name" column="name"/>
   </collection>
</resultMap>
<select id="selectOneDept" resultMap="deptMap1">
   SELECT id,name FROM dept where id=#{id}
</select>
<select id="selectEmployee" resultType="Employee">
   select name from employee where deptId=#{id}
</select>

一对多,我们使用<collection>

懒加载就介绍到这里,感谢阅读

来源:https://blog.csdn.net/xx12321q/article/details/123848565

标签:MyBatis,懒加载,原理,配置
0
投稿

猜你喜欢

  • android实现软件自动更新的步骤

    2022-12-27 00:48:17
  • mybatis if标签判断不生效的解决方法

    2023-11-28 12:30:49
  • java二维数组基础知识详解

    2023-03-28 11:10:54
  • Java实现人机猜拳游戏

    2023-10-16 08:47:56
  • java反射遍历实体类属性和类型,并赋值和获取值的简单方法

    2023-07-15 04:37:18
  • Hibernate实现批量添加数据的方法

    2023-11-29 08:53:56
  • java贪吃蛇游戏实现代码

    2023-02-21 00:07:59
  • Android自定义弹出框dialog效果

    2023-06-29 14:14:32
  • C#实现滑动开关效果

    2023-11-26 22:18:29
  • springboot FeignClient注解及参数

    2021-07-09 21:59:07
  • 浅谈Java内省机制

    2021-06-13 17:54:05
  • Android 广播大全 Intent Action 事件详解

    2021-09-10 12:40:15
  • 详解Spring Cloud负载均衡重要组件Ribbon中重要类的用法

    2023-07-06 02:54:01
  • java实现向有序数组中插入一个元素实例

    2023-04-03 23:10:49
  • SpringBoot如何使用RateLimiter通过AOP方式进行限流

    2023-09-16 18:18:44
  • AndroidStudio 实现加载字体资源的方法

    2023-06-24 06:18:11
  • Android 二维码 生成和识别二维码 附源码下载

    2023-05-01 12:45:01
  • Kotlin协程Channel源码示例浅析

    2023-06-14 22:54:08
  • Java Swing实现简单的体重指数(BMI)计算器功能示例

    2022-12-16 09:34:56
  • 使用JPA主键@Id,@IdClass,@Embeddable,@EmbeddedId问题

    2022-07-07 18:35:41
  • asp之家 软件编程 m.aspxhome.com