国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Mybatis如何實現(xiàn)延遲加載及緩存

瀏覽:10日期:2023-10-22 12:30:37

一、延遲加載

1、在mybatis.xml配置文件中,開啟延遲加載

<settings> <!--開啟延遲加載--> <setting name='lazyLoadingEnabled' value='true'></setting> <setting name='aggressiveLazyLoading' value='false'></setting> <!--延遲加載觸發(fā)方法,equals、hashCode、toString都會觸發(fā)加載--> <setting name='lazyLoadTriggerMethods' value='hashCode'></setting> <!--數(shù)據(jù)庫下劃線(_)命名轉(zhuǎn)駝峰命名--> <setting name='mapUnderscoreToCamelCase' value='true'/> </settings>

2、配置mapper文件

1、一對一

* 一方

<resultMap 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)閉延遲加載會做兩次查詢--><association column='grade_id' property='grade' javaType='Grade' select='com.wuxi.daos.GradeMapper.selectById'></association> </resultMap> <select resultMap='studentGradeById'>select * from student where id = #{id} </select>

* 另一方

<select 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>  <!--關(guān)閉延遲加載會做兩次查詢--><collection property='students' ofType='Student' column='id' select='com.wuxi.daos.StudentMapper.selectStudentsByGrade'></collection> </resultMap> <select resultMap='gradeStudents'>select * from grade where id = #{id} </select>

* 多方

<select 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范圍的緩存,當(dāng)調(diào)用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、對象需要實現(xiàn)Serializable接口

2、在mybatis.xml配置文件中,開啟二級緩存

<settings><!--開啟二級緩存--><setting name='cacheEnabled' value='true'/></settings>

3、配置mapper文件

<cache/><select 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();// 只查詢了一次數(shù)據(jù)庫。二級緩存存儲的是數(shù)據(jù),并不是對象System.out.println(student1 == student2); // false

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Mybatis 數(shù)據(jù)庫
相關(guān)文章:
主站蜘蛛池模板: 咸丰县| 拉萨市| 奉新县| 沿河| 灵山县| 三都| 宁波市| 甘孜| 博野县| 依安县| 诏安县| 抚松县| 陈巴尔虎旗| 新安县| 游戏| 大竹县| 务川| 武强县| 灵山县| 色达县| 荆门市| 乐山市| 平塘县| 浮山县| 宣化县| 黄大仙区| 茂名市| 林周县| 南阳市| 城口县| 玉林市| 舒兰市| 永清县| 雷山县| 台州市| 耿马| 蒙阴县| 延川县| 徐州市| 祁连县| 阳城县|