Mybatis如何實(shí)現(xiàn)延遲加載及緩存
一、延遲加載
1、在mybatis.xml配置文件中,開啟延遲加載
<settings>
<!--開啟延遲加載-->
<setting name="lazyLoadingEnabled" value="true"></setting>
<setting name="aggressiveLazyLoading" value="false"></setting>
<!--延遲加載觸發(fā)方法,equals、hashCode、toString都會(huì)觸發(fā)加載-->
<setting name="lazyLoadTriggerMethods" value="hashCode"></setting>
<!--數(shù)據(jù)庫下劃線(_)命名轉(zhuǎn)駝峰命名-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
2、配置mapper文件
1、一對(duì)一
* 一方
<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> <!--關(guān)閉延遲加載會(huì)做兩次查詢-->
<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>
* 測(cè)試
Student student = smapper.selectStudentGradeById(4);
System.out.println(student);
// student.hashCode();
System.out.println(student.getGrade());
2、一對(duì)多
* 一方
<resultMap type="Grade" id="gradeStudents">
<id column="id" property="id"></id>
<result column="name" property="name"></result> <!--關(guān)閉延遲加載會(huì)做兩次查詢-->
<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>
* 測(cè)試
Grade grade = gmapper.selectById(1);
System.out.println(grade);
// student.hashCode();
System.out.println(grade.getStudents());
二、緩存
1、一級(jí)緩存
1、概念
一級(jí)緩存是SqlSession范圍的緩存,當(dāng)調(diào)用SqlSession的修改,添加,刪除,commit(),close()等方法時(shí),就會(huì)清空一級(jí)緩存。
2、測(cè)試
// 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、二級(jí)緩存
1、開啟二級(jí)緩存
1、對(duì)象需要實(shí)現(xiàn)Serializable接口
2、在mybatis.xml配置文件中,開啟二級(jí)緩存
<settings>
<!--開啟二級(jí)緩存-->
<setting name="cacheEnabled" value="true"/>
</settings>
3、配置mapper文件
<cache/>
<select id="selectStudentGradeById" resultMap="studentGradeById" useCache="true">
select * from student where id = #{id}
</select>
2、測(cè)試
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();
// 只查詢了一次數(shù)據(jù)庫。二級(jí)緩存存儲(chǔ)的是數(shù)據(jù),并不是對(duì)象
System.out.println(student1 == student2); // false
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot@DeleteMapping(/xxx/{id})請(qǐng)求報(bào)405的解決
這篇文章主要介紹了SpringBoot@DeleteMapping(/xxx/{id})請(qǐng)求報(bào)405的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
多模塊的springboot項(xiàng)目發(fā)布指定模塊的腳本方式
該文章主要介紹了如何在多模塊的SpringBoot項(xiàng)目中發(fā)布指定模塊的腳本,作者原先的腳本會(huì)清理并編譯所有模塊,導(dǎo)致發(fā)布時(shí)間過長(zhǎng),通過簡(jiǎn)化腳本,只使用`mvn clean install`命令,可以快速發(fā)布指定模塊及其依賴的模塊2025-01-01
使用@CachePut?更新數(shù)據(jù)庫和更新緩存
這篇文章主要介紹了使用@CachePut?更新數(shù)據(jù)庫和更新緩存方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
詳解spring security 配置多個(gè)AuthenticationProvider
這篇文章主要介紹了詳解spring security 配置多個(gè)AuthenticationProvider ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
SpringBoot定時(shí)任務(wù)的實(shí)現(xiàn)詳解
這篇文章主要介紹了SpringBoot定時(shí)任務(wù)的實(shí)現(xiàn)詳解,定時(shí)任務(wù)是企業(yè)級(jí)開發(fā)中最常見的功能之一,如定時(shí)統(tǒng)計(jì)訂單數(shù)、數(shù)據(jù)庫備份、定時(shí)發(fā)送短信和郵件、定時(shí)統(tǒng)計(jì)博客訪客等,簡(jiǎn)單的定時(shí)任務(wù)可以直接通過Spring中的@Scheduled注解來實(shí)現(xiàn),需要的朋友可以參考下2024-01-01

