• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Mybatis如何实现延迟加载及缓存

mybatis 搞代码 4年前 (2022-01-09) 28次浏览 已收录 0个评论

一、延迟加载

1、在mybatis.xml配置文件中,开启延迟加载

  <settings>
    <!--开启延迟加载-->
    <setting name="lazyLoadingEnabled" value="true"></setting>
    <setting name="aggressiveLazyLoading" value="false"></setting>
    <!--延迟加载触发方法,equals、hashCode、toString都会触发加载-->
    <setting name="lazyLoadTriggerMethods" value="hashCode"></setting>
    <!--数据库下划线(_)命名转驼峰命名-->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
  </settings>

2、配置mapper文件

1、一对一

* 一方

      <resultMap id="studentGradeById" type="Student">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="age" property="age"></result>
        <result column="sex" property="sex"></result>          <!--关闭延迟加载会做两次查询-->
        <association column="grade_id" property="grade" javaType="Grade"
               select="com.wuxi.daos.GradeMapper.selectById"></association>
      </resultMap>
      <select id="selectStudentGradeById" resultMap="studentGrade<mark>本文来源gaodaimacom搞#代%码@网-</mark>ById">
        select * from student where id = #{id}
      </select>

* 另一方

      <select id="selectById" resultType="Grade">
        select * from grade where id = #{id}
      </select>

* 测试

Student student = smapper.selectStudentGradeById(4);
System.out.println(student);
// student.hashCode();
System.out.println(student.getGrade());

2、一对多

* 一方

      <resultMap type="Grade" id="gradeStudents">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>          <!--关闭延迟加载会做两次查询-->
        <collection property="students" ofType="Student" column="id"
              select="com.wuxi.daos.StudentMapper.selectStudentsByGrade"></collection>
      </resultMap>
      <select id="selectById" resultMap="gradeStudents">
        select * from grade where id = #{id}
      </select>

* 多方

      <select id="selectStudentsByGrade" resultType="Student">
        select * from student where grade_id=#{grade_id}
      </select>

* 测试

Grade grade = gmapper.selectById(1);
System.out.println(grade);
// student.hashCode();
System.out.println(grade.getStudents());

二、缓存

1、一级缓存

1、概念

一级缓存是SqlSession范围的缓存,当调用SqlSession的修改,添加,删除,commit(),close()等方法时,就会清空一级缓存。

2、测试

// Student student1 = smapper.selectStudentGradeById(1);
// Student student2 = smapper.selectStudentGradeById(1);
// System.out.println(student1 == student2); // true
// ********************************
Student student1 = smapper.selectStudentGradeById(1);
Student student = new Student();
student.setName(“杜兰特”);
student.setAge(28);
student.setSex(1);
smapper.insertStudent(student);
Student student2 = smapper.selectStudentGradeById(1);
System.out.println(student1 == student2); // false


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Mybatis如何实现延迟加载及缓存
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址