欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis一對多和多對一處理的深入講解

 更新時間:2021年09月13日 09:02:51   作者:iiis1327  
Mybatis可以通過關聯(lián)查詢實現(xiàn),關聯(lián)查詢是幾個表聯(lián)合查詢,只查詢一次,通過在resultMap里面的association,collection節(jié)點配置一對一,一對多的類就可以完成,這篇文章主要給大家介紹了關于Mybatis一對多和多對一處理的相關資料,需要的朋友可以參考下

建表

SQL:

create table teacher(
    id int not null,
    name varchar(30) default null,
    primary key (id)
);

insert into teacher (id, name) values (1, '蔡老師');

create table student(
    id int not null ,
    name varchar(30) default null,
    tid int default null,
    constraint fk_tid foreign key (tid) references teacher(id)
);

insert into student(id, name, tid) VALUES (1, '小名', 1);
insert into student(id, name, tid) VALUES (2, '小紅', 1);
insert into student(id, name, tid) VALUES (3, '小亮', 1);
insert into student(id, name, tid) VALUES (4, '小蘭', 1);
insert into student(id, name, tid) VALUES (5, '笑笑', 1);

多對一處理

  • 多個學生對應一個老師
  • 對于學生這邊而言,關聯(lián)。即多個學生關聯(lián)一個老師【多對一】
  • 對于老師這邊而言,集合。即一個老師有很多的學生【一對多】

mapper

//查詢所有的學生信息以及對應的老師的信息
List<Student> queryStudentAndTeacher();

實體類

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private int id;
    private String name;

    //學生需要關聯(lián)一個老師
    private Teacher teacher;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
    private int id;
    private String name;
}

按照查詢嵌套處理

<!--思路:
	1.查詢所有的學生
	2.根據(jù)查詢出來的學生的tid尋找對應的老師  尋找對應的老師,子查詢
-->
<resultMap id="rStuAndTea" type="student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <!-- 復雜的屬性我們需要單獨處理  
							指定屬性的類型
		 對象使用association  javaType
		 集合使用collection   ofType
	-->
    <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="queryStudentAndTeacher" resultMap="rStuAndTea">
    select * from student
</select>
<select id="getTeacher" resultType="teacher">
    select * from teacher where id = #{id}
</select>

按照結果嵌套處理

<!--方式二  按照結果嵌套處理-->
<resultMap id="rStuAndTea2" type="student">
    <result property="id" column="sid"/>
    <result property="name" column="sname"/>
    <association property="teacher" javaType="Teacher">
        <result property="name" column="tname"/>
    </association>
</resultMap>
<select id="queryStudentAndTeacher2" resultMap="rStuAndTea2">
    select s.id sid, s.name sname, t.name tname
    from student s, teacher t
    where s.tid = t.id
</select>

回顧Mysql多對一查詢方式

  • 子查詢
  • 聯(lián)表查詢

一對多處理

  • 一個老師有多個學生
  • 對于老師這邊而言,集合。即一個老師有很多的學生【一對多】

mapper

//查詢指定老師的信息及其所有的學生
Teacher queryTeaAndStu(@Param("tid") int id);

實體類

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
    private int id;
    private String name;

    //一個老師擁有多個學生
    private List<Student> students;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private int id;
    private String name;
    private int tid;
}

按照查詢嵌套處理

<!--按照結果嵌套查詢-->
<resultMap id="rTeaAndStu" type="teacher">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    <collection property="students" ofType="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <result property="tid" column="tid"/>
    </collection>
</resultMap>
<select id="queryTeaAndStu" resultMap="rTeaAndStu">
    select s.id sid, s.name sname, t.name tname, t.id tid
    from student s, teacher t
    where s.tid = t.id and t.id = #{tid}
</select>

按照查詢嵌套處理

<!--按照查詢嵌套處理-->
<select id="queryTeaAndStu2" resultMap="rTeaAndStu2">
    select * from teacher where id = #{tid}
</select>
<resultMap id="rTeaAndStu2" type="teacher">
    <collection property="students" javaType="ArrayList" ofType="Student"
                select="queryStudentByTeacherId" column="id"/>
</resultMap>
<select id="queryStudentByTeacherId" resultType="Student">
    select * from student where tid = #{tid}
</select>

結果映射

面試高頻點

  • MySQL引擎
  • InnoDB底層原理
  • 索引
  • 索引優(yōu)化

小結

  1. 關聯(lián) - association 【多對一】
  2. 集合 - collection 【一對多】
  3. javaType & ofType
    1. javaType 用來指定實體類中屬性的類型
    2. ofType 用來指定映射到List或者集合中的entity類型,泛型中的約束類型

注意點:

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

總結

到此這篇關于Mybatis一對多和多對一處理的文章就介紹到這了,更多相關Mybatis一對多和多對一處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Spring?MVC??接受請求參數(shù)的方法

    Spring?MVC??接受請求參數(shù)的方法

    了解HTTP請求的GET和POST方法中如何攜帶參數(shù),以及SpringMVC中如何接收這些參數(shù),GET方法通過URL傳遞參數(shù),而POST方法通常在請求體中傳遞,SpringMVC使用注解如@RequestParam和@RequestBody來綁定參數(shù)到控制器方法
    2024-09-09
  • Spring中的bean概念介紹

    Spring中的bean概念介紹

    這篇文章主要介紹了Spring中的bean相關知識,包括基本概念定義控制反轉(zhuǎn)IOC的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • 淺談Hibernate中的三種數(shù)據(jù)狀態(tài)(臨時、持久、游離)

    淺談Hibernate中的三種數(shù)據(jù)狀態(tài)(臨時、持久、游離)

    下面小編就為大家?guī)硪黄獪\談Hibernate中的三種數(shù)據(jù)狀態(tài)(臨時、持久、游離)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • FasfDFS整合Java實現(xiàn)文件上傳下載功能實例詳解

    FasfDFS整合Java實現(xiàn)文件上傳下載功能實例詳解

    這篇文章主要介紹了FasfDFS整合Java實現(xiàn)文件上傳下載功能實例詳解,需要的朋友可以參考下
    2017-08-08
  • java.net.ConnectException異常的正確解決方法(親測有效!)

    java.net.ConnectException異常的正確解決方法(親測有效!)

    java.net.ConnectException異常是與網(wǎng)絡相關的最常見的Java異常之一,建立從客戶端應用程序到服務器的TCP連接時,我們可能會遇到它,這篇文章主要給大家介紹了關于java.net.ConnectException異常的正確解決方法,需要的朋友可以參考下
    2024-01-01
  • Maven的安裝+配置本地倉庫路徑方式

    Maven的安裝+配置本地倉庫路徑方式

    這篇文章主要介紹了Maven的安裝+配置本地倉庫路徑方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • 基于java流實現(xiàn)壓縮圖片過程解析

    基于java流實現(xiàn)壓縮圖片過程解析

    這篇文章主要介紹了基于java流實現(xiàn)壓縮圖片過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10
  • 解析Spring?漏洞及其修復方案

    解析Spring?漏洞及其修復方案

    官宣了最近網(wǎng)傳的Spring漏洞。攻擊者利用該漏洞,可在未授權的情況下遠程執(zhí)行命令,今天通過本文給大家普及下漏洞分析影響范圍及解決方案,感興趣的朋友跟隨小編一起看看吧
    2022-04-04
  • 利用springmvc處理模型數(shù)據(jù)

    利用springmvc處理模型數(shù)據(jù)

    這篇文章主要介紹了如何利用springmvc 處理模型數(shù)據(jù),幫助大家更好的理解和學習使用springmvc,感興趣的朋友可以了解下
    2021-03-03
  • springboot starter自定義實現(xiàn)公共模塊方式

    springboot starter自定義實現(xiàn)公共模塊方式

    這篇文章主要介紹了springboot starter自定義實現(xiàn)公共模塊方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08

最新評論