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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。