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

Mybatis一對(duì)多和多對(duì)一處理的深入講解

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

建表

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);

多對(duì)一處理

  • 多個(gè)學(xué)生對(duì)應(yīng)一個(gè)老師
  • 對(duì)于學(xué)生這邊而言,關(guān)聯(lián)。即多個(gè)學(xué)生關(guān)聯(lián)一個(gè)老師【多對(duì)一】
  • 對(duì)于老師這邊而言,集合。即一個(gè)老師有很多的學(xué)生【一對(duì)多】

mapper

//查詢所有的學(xué)生信息以及對(duì)應(yīng)的老師的信息
List<Student> queryStudentAndTeacher();

實(shí)體類

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

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

按照查詢嵌套處理

<!--思路:
	1.查詢所有的學(xué)生
	2.根據(jù)查詢出來的學(xué)生的tid尋找對(duì)應(yīng)的老師  尋找對(duì)應(yīng)的老師,子查詢
-->
<resultMap id="rStuAndTea" type="student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <!-- 復(fù)雜的屬性我們需要單獨(dú)處理  
							指定屬性的類型
		 對(duì)象使用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>

按照結(jié)果嵌套處理

<!--方式二  按照結(jié)果嵌套處理-->
<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多對(duì)一查詢方式

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

一對(duì)多處理

  • 一個(gè)老師有多個(gè)學(xué)生
  • 對(duì)于老師這邊而言,集合。即一個(gè)老師有很多的學(xué)生【一對(duì)多】

mapper

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

實(shí)體類

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

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

按照查詢嵌套處理

<!--按照結(jié)果嵌套查詢-->
<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>

結(jié)果映射

面試高頻點(diǎn)

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

小結(jié)

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

注意點(diǎn):

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

總結(jié)

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

相關(guān)文章

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

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

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

    Spring中的bean概念介紹

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

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

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

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

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

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

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

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

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

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

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

    解析Spring?漏洞及其修復(fù)方案

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

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

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

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

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

最新評(píng)論