亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Mybatis如何實現延遲加載及緩存

 更新時間:2020年10月09日 10:54:55   作者:一路繁花似錦繡前程  
這篇文章主要介紹了Mybatis如何實現延遲加載及緩存,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

一、延遲加載

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>
    <!--數據庫下劃線(_)命名轉駝峰命名-->
    <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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java中輸入單個char類型的字符方式

    Java中輸入單個char類型的字符方式

    這篇文章主要介紹了Java中輸入單個char類型的字符方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Ribbon和Feign的區(qū)別及說明

    Ribbon和Feign的區(qū)別及說明

    本文介紹了Spring Cloud Netflix中的兩個負載均衡組件:Ribbon和Feign,Ribbon是一個基于HTTP和TCP客戶端的負載均衡工具,使用起來較為繁瑣,而Feign是一個使用接口方式的HTTP客戶端,采用類似MyBatis的@Mapper注解方式,使得編寫客戶端變得非常容易
    2024-11-11
  • SpringBoot@DeleteMapping(/xxx/{id})請求報405的解決

    SpringBoot@DeleteMapping(/xxx/{id})請求報405的解決

    這篇文章主要介紹了SpringBoot@DeleteMapping(/xxx/{id})請求報405的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 多模塊的springboot項目發(fā)布指定模塊的腳本方式

    多模塊的springboot項目發(fā)布指定模塊的腳本方式

    該文章主要介紹了如何在多模塊的SpringBoot項目中發(fā)布指定模塊的腳本,作者原先的腳本會清理并編譯所有模塊,導致發(fā)布時間過長,通過簡化腳本,只使用`mvn clean install`命令,可以快速發(fā)布指定模塊及其依賴的模塊
    2025-01-01
  • Java編程打印購物小票實現代碼

    Java編程打印購物小票實現代碼

    這篇文章主要介紹了Java編程打印購物小票實現代碼,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • MyBatis框架中mybatis配置文件詳細介紹

    MyBatis框架中mybatis配置文件詳細介紹

    這篇文章主要介紹了MyBatis框架中mybatis配置文件詳細介紹,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-01-01
  • 使用@CachePut?更新數據庫和更新緩存

    使用@CachePut?更新數據庫和更新緩存

    這篇文章主要介紹了使用@CachePut?更新數據庫和更新緩存方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 詳解spring security 配置多個AuthenticationProvider

    詳解spring security 配置多個AuthenticationProvider

    這篇文章主要介紹了詳解spring security 配置多個AuthenticationProvider ,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • SpringBoot定時任務的實現詳解

    SpringBoot定時任務的實現詳解

    這篇文章主要介紹了SpringBoot定時任務的實現詳解,定時任務是企業(yè)級開發(fā)中最常見的功能之一,如定時統(tǒng)計訂單數、數據庫備份、定時發(fā)送短信和郵件、定時統(tǒng)計博客訪客等,簡單的定時任務可以直接通過Spring中的@Scheduled注解來實現,需要的朋友可以參考下
    2024-01-01
  • Java輸入/輸出流體系詳解

    Java輸入/輸出流體系詳解

    這篇文章主要介紹了Java輸入/輸出流體系詳解,涉及字節(jié)流和字符流,輸入輸出體系,轉換流,以及文件的讀寫等相關內容,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11

最新評論