日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Mybatis 之应用篇】 3_Lombok、多对一处理和一对多处理

發布時間:2025/3/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Mybatis 之应用篇】 3_Lombok、多对一处理和一对多处理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

      • Mybatis
        • 九、Lombok
        • 十、多對一處理
          • 1.測試環境搭建
          • 2.多表查詢方法一:按照查詢嵌套處理
          • 3.多表查詢方法二:按照結果嵌套處理☆
        • 十一、一對多處理
          • 1.實體類的搭建
          • 2.按照結果嵌套處理☆
          • 3.按照查詢嵌套
          • 4.小結

Mybatis

九、Lombok

使用步驟:

  • 在IDEA中安裝Lombok插件!

  • 在項目中導入lombok的jar包

    <dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.10</version> </dependency>
  • 在實體類上加注解即可!

    @Data @AllArgsConstructor @NoArgsConstructor
  • @Getter and @Setter @FieldNameConstants @ToString @EqualsAndHashCode @AllArgsConstructor, @RequiredArgsConstructor and @NoArgsConstructor @Log, @Log4j, @Log4j2, @Slf4j, @XSlf4j, @CommonsLog, @JBossLog, @Flogger @Data @Builder @Singular @Delegate @Value @Accessors @Wither @SneakyThrows

    說明:

    @Data:無參構造,get、set、tostring、hashcode,equals @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode @ToString @Getter

    十、多對一處理

    • 多個學生,對應一個老師
    • 對于學生這邊而言, 關聯 … 多個學生,關聯一個老師 【多對一】
    • 對于老師而言, 集合 , 一個老師,有很多學生 【一對多】
    1.測試環境搭建
  • 導入lombok
  • 新建實體類 Teacher,Student
  • 建立Mapper接口
  • 建立Mapper.XML文件
  • 在核心配置文件中綁定注冊我們的Mapper接口或者文件!【方式很多,隨心選】
  • 測試查詢是否能夠成功!
  • 2.多表查詢方法一:按照查詢嵌套處理
    <!--在做多對一的時候,思路:1.查詢所有的學生信息2.根據查詢出來的學生的tid,尋找對應的老師--> <select id="getStudent" resultMap="map">select * from student s,teacher t where s.tid=t.id; </select> <!--本質最后返回的是一個學生,id是學生的id,name是學生的name,association則是最后的對象屬性--> <resultMap id="map" type="Student"><result property="id" column="id"/><result property="name" column="name"/><!--復雜 的屬性,我們需要單獨處理對象:association集合:collection--><!--property的值是指類中的字段,column的值是指數據庫中的字段,javaType(=resultType)的值為對象的類型--><association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="Teacher">select * from teacher where id = #{tid} </select>
    3.多表查詢方法二:按照結果嵌套處理☆
    <select id="getStudent2" resultMap="map2">select s.id sid,s.name smame,t.name tnamefrom student s,teacher twhere s.tid=t.id; </select><resultMap id="map2" type="Student"><result property="id" column="sid"/><result property="name" column="sname"/><association property="teacher" javaType="Teacher"><!--這里的name是Teacher類中的name--><result property="name" column="tname"/></association> </resultMap>

    十一、一對多處理

    比如:一個老師擁有多個學生!

    對于老師而言,就是一對多的關系!

    1.實體類的搭建
    @Data public class Student {private int id;private String name;//學生需要關聯一個老師private int tid; } @Data public class Teacher {private int id;private String name;private List<Student> students; }
    2.按照結果嵌套處理☆
    //接口中 List<Teacher> getTeacher(@Param("tid") int id); <!--在配置文件中--> <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="dao.TeacherMapper"><select id="getTeacher" resultMap="map">select s.id sid,s.name sname,t.name tnamefrom student s,teacher twhere s.tid = t.id and t.id = #{tid};</select><resultMap id="map" type="Teacher"><result property="name" column="tname"/><!--復雜的屬性,我們需要單獨處理 對象: association 集合: collectionjavaType="" 指定屬性的類型!集合中的泛型信息,我們使用ofType獲取--><collection property="students" ofType="Student"><result property="id" column="sid"/><result property="name" column="sname"/></collection></resultMap> </mapper> //測試類@Testpublic void testGetTeacer(){SqlSession sqlSession = MybatisUtil.getSqlSession();TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);List<Teacher> teacher = mapper.getTeacher(1);System.out.println(teacher);sqlSession.close();}
    3.按照查詢嵌套
    <select id="getTeacher2" resultMap="map2">select * from mybatis.teacher where id = #{tid}; </select><resultMap id="map2" type="Teacher"><collection property="students" javaType="ArrayList" ofType="Student" select="getTeacherByStudentId" column="id"/> </resultMap><select id="getTeacherByStudentId" resultType="Teacher">select * from mybatis.student where tid = #{id}; </select>
    4.小結
  • 關聯 - association 【多對一】
  • 集合 - collection 【一對多】
  • javaType & ofType
  • JavaType 用來指定實體類中屬性的類型
  • ofType 用來指定映射到List或者集合中的 pojo類型,泛型中的約束類型!
  • 注意點:

    • 保證SQL的可讀性,盡量保證通俗易懂
    • 注意一對多和多對一中,屬性名和字段的問題!
    • 如果問題不好排查錯誤,可以使用日志 , 建議使用 Log4j

    慢SQL 1s 1000s

    面試高頻

    • Mysql引擎

    • InnoDB底層原理

    • 索引

    • 索引優化!

    • ofType 用來指定映射到List或者集合中的 pojo類型,泛型中的約束類型!

    注意點:

    • 保證SQL的可讀性,盡量保證通俗易懂
    • 注意一對多和多對一中,屬性名和字段的問題!
    • 如果問題不好排查錯誤,可以使用日志 , 建議使用 Log4j

    慢SQL 1s 1000s

    面試高頻

    • Mysql引擎
    • InnoDB底層原理
    • 索引
    • 索引優化!

    總結

    以上是生活随笔為你收集整理的【Mybatis 之应用篇】 3_Lombok、多对一处理和一对多处理的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。