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

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="studentGradeById">
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
2、二级缓存
1、开启二级缓存
1、对象需要实现Serializable接口
2、在mybatis.xml配置文件中,开启二级缓存
<settings>
<!--开启二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
3、配置mapper文件
<cache/>
<select id="selectStudentGradeById" resultMap="studentGradeById" useCache="true">
select * from student where id = #{id}
</select>
2、测试
SqlSession sqlSession1 = sqlSessionFactory.openSession();
StudentMapper mapper1 = sqlSession1.getMapper(StudentMapper.class);
Student student1 = mapper1.selectStudentGradeById(1);
sqlSession1.close();
SqlSession sqlSession2 = sqlSessionFactory.openSession();
StudentMapper mapper2 = sqlSession2.getMapper(StudentMapper.class);
Student student2 = mapper2.selectStudentGradeById(1);
sqlSession2.close();
// 只查询了一次数据库。二级缓存存储的是数据,并不是对象
System.out.println(student1 == student2); // false
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
代码知识SEO上一篇 : Python collections模块的使用方法
下一篇 : java 中类似js encodeURIComponent 函数的实现案例
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!