Mybatis實(shí)現(xiàn)一對(duì)多映射處理
一、概述
一對(duì)多關(guān)系表示一個(gè)實(shí)體對(duì)象(一)可以擁有多個(gè)關(guān)聯(lián)對(duì)象(多)。
例如,一個(gè)用戶可以有多個(gè)訂單,或者一個(gè)部門(mén)可以有多個(gè)員工。
在數(shù)據(jù)庫(kù)中,一對(duì)多關(guān)系通常通過(guò)外鍵來(lái)實(shí)現(xiàn)。
二、創(chuàng)建數(shù)據(jù)模型
定義實(shí)體類(lèi):定義主表實(shí)體類(lèi)和從表實(shí)體類(lèi),主表實(shí)體類(lèi) 中包含 從表實(shí)體類(lèi) 的List集合屬性。
現(xiàn)在我們以此來(lái)創(chuàng)建面向?qū)ο笳Z(yǔ)言的對(duì)象數(shù)據(jù)模型:
Dept.java
public class Dept {
private Integer did;
private String deptName;
private List<Emp> emps;//用于表示數(shù)據(jù)庫(kù)一對(duì)多的關(guān)系
// 省略構(gòu)造函數(shù)和getter/setter方法
}Emp.java
public class Emp {
private Integer eid;
private String empName;
private Integer age;
private String sex;
private String email;
// 省略構(gòu)造函數(shù)和getter/setter方法
}三、問(wèn)題
假設(shè)我們現(xiàn)在又兩張表,一張員工表,一張部門(mén)表,現(xiàn)在我們要獲取部門(mén)以及該部門(mén)中所有的員工信息
四、解決方案
1、方案一:collection(嵌套結(jié)果)
嵌套結(jié)果:使用嵌套結(jié)果的方式,可以在查詢主實(shí)體對(duì)象的同時(shí),通過(guò)嵌套的方式將關(guān)聯(lián)實(shí)體對(duì)象的屬性嵌套到主實(shí)體對(duì)象的屬性中。這種方式的實(shí)現(xiàn)需要在Mapper.xml文件中定義一個(gè)SQL語(yǔ)句,使用嵌套的方式將關(guān)聯(lián)實(shí)體對(duì)象的屬性映射到主實(shí)體對(duì)象的屬性中。在定義映射關(guān)系時(shí),需要使用resultMap標(biāo)簽來(lái)定義主實(shí)體對(duì)象和關(guān)聯(lián)實(shí)體對(duì)象的映射關(guān)系,使用collection標(biāo)簽來(lái)嵌套關(guān)聯(lián)實(shí)體對(duì)象的屬性。
DeptMapper.java
/**
* @description:獲取部門(mén)以及部門(mén)中所有的員工信息
* @author: Hey
* @date: 2022/7/4 10:46
* @param: [did]
* @return: com.ir.mybatis.pojo.Dept
**/
Dept getDeptAndEmp(@Param("did") Integer did);DeptMapper.xml
<resultMap id="deptAndEmpResultMap" type="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
<!--
collection:處理一對(duì)多的映射關(guān)系
ofType:表示該屬性所對(duì)應(yīng)的集合中存儲(chǔ)數(shù)據(jù)的類(lèi)型
-->
<collection property="emps" ofType="Emp">
<id property="eid" column="eid"></id>
<result property="empName" column="emp_name"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
<result property="email" column="email"></result>
</collection>
</resultMap>
<!--Dept getDeptAndEmp(@Param("did") Integer did);-->
<select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">
select * from t_dept left join t_emp on t_dept.did = t_emp.did where t_dept.did = #{did}
</select>ResultTest.java
/**
* @description:獲取部門(mén)以及部門(mén)中所有的員工信息
* @author: Hey
* @date: 2022/7/4 10:54
* @param: []
* @return: void
**/
@Test
public void testGetDeptAndEmp(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
Dept dept = mapper.getDeptAndEmp(1);
System.out.println(dept);
/**
* Dept{
* did=1, deptName='A',
* emps=[
* Emp{eid=1, empName='喜羊羊', age=34, sex='男', email='123@qq.com'},
* Emp{eid=4, empName='沸羊羊', age=23, sex='男', email='123@qq.com'}
* ]
* }
*/
}2、方案二:分步查詢(嵌套查詢)
嵌套查詢:使用嵌套查詢的方式,可以在查詢主實(shí)體對(duì)象的同時(shí),通過(guò)查詢關(guān)聯(lián)實(shí)體對(duì)象的方式獲取相關(guān)聯(lián)的多個(gè)實(shí)體對(duì)象。這種方式的實(shí)現(xiàn)需要在Mapper.xml文件中定義兩個(gè)獨(dú)立的SQL語(yǔ)句,一個(gè)用于查詢主實(shí)體對(duì)象,另一個(gè)用于查詢關(guān)聯(lián)實(shí)體對(duì)象。在查詢主實(shí)體對(duì)象時(shí),通過(guò)使用select標(biāo)簽的子標(biāo)簽來(lái)執(zhí)行關(guān)聯(lián)實(shí)體對(duì)象的查詢,并將查詢結(jié)果映射到主實(shí)體對(duì)象的屬性中。
DeptMapper
/**
* @description:通過(guò)分步查詢查詢部門(mén)以及部門(mén)中所有的員工信息
* 分步查詢第一步:查詢部門(mén)信息
* @author: Hey
* @date: 2022/7/4 12:31
* @param: [did]
* @return: com.ir.mybatis.pojo.Dept
**/
Dept getDeptAndEmpByStepOne(@Param("did") Integer did);DeptMapper.xml
<resultMap id="deptAndEmpByStepResultMap" type="Dept">
<id property="did" column="did"></id>
<result property="deptName" column="dept_name"></result>
<collection property="emps"
select="com.ir.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
column="did" fetchType="eager">
</collection>
</resultMap>
<select id="getDeptAndEmpByStepOne" resultType="deptAndEmpByStepResultMap">
select * from t_dept where did = #{did}
</select>EmpMapper
/**
* @description:通過(guò)分步查詢查詢部門(mén)以及部門(mén)中所有的員工信息
* 分步查詢第二步:根據(jù)did查詢員工信息
* @author: Hey
* @date: 2022/7/4 12:36
* @param: [did]
* @return: java.util.List<com.ir.mybatis.pojo.Emp>
**/
List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);EmpMapper.xml
<select id="getDeptAndEmpByStepTwo" resultType="Emp">
select * from t_emp where did = #{did}
</select>ResultTest
/**
* @description:通過(guò)分步查詢查詢部門(mén)以及部門(mén)中所有的員工信息
* @author: Hey
* @date: 2022/7/4 12:40
* @param: []
* @return: void
**/
@Test
public void testGetDeptAndEmpByStep(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
Dept dept = mapper.getDeptAndEmpByStepOne(1);
System.out.println(dept.getDeptName());
}無(wú)論是使用嵌套查詢還是嵌套結(jié)果的方式,都需要在Mapper.xml文件中定義相應(yīng)的SQL語(yǔ)句和映射關(guān)系。同時(shí),為了提高查詢性能,可以使用Mybatis的延遲加載機(jī)制來(lái)減少查詢次數(shù),提高數(shù)據(jù)訪問(wèn)效率。
到此這篇關(guān)于Mybatis實(shí)現(xiàn)一對(duì)多映射處理的文章就介紹到這了,更多相關(guān)Mybatis映射內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中BigDecimal的equals方法和compareTo方法的區(qū)別詳析
這篇文章主要給大家介紹了關(guān)于Java中BigDecimal的equals方法和compareTo方法區(qū)別的相關(guān)資料,對(duì)于BigDecimal的大小比較,用equals方法的話會(huì)不僅會(huì)比較值的大小,還會(huì)比較兩個(gè)對(duì)象的精確度,而compareTo方法則不會(huì)比較精確度,只比較數(shù)值的大小,需要的朋友可以參考下2023-11-11
使用Java開(kāi)發(fā)實(shí)現(xiàn)OAuth安全認(rèn)證的應(yīng)用
這篇文章主要介紹了使用Java開(kāi)發(fā)實(shí)現(xiàn)OAuth安全認(rèn)證的應(yīng)用的方法,OAuth安全認(rèn)證經(jīng)常出現(xiàn)于社交網(wǎng)絡(luò)API應(yīng)用的相關(guān)開(kāi)發(fā)中,需要的朋友可以參考下2015-11-11
springMVC的RequestMapping請(qǐng)求不到路徑的解決
這篇文章主要介紹了springMVC的RequestMapping請(qǐng)求不到路徑的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
基于JAVA中使用Axis發(fā)布/調(diào)用Webservice的方法詳解
如果初識(shí)axis發(fā)布/調(diào)用WS,建議先讀上面的參考文件,本文對(duì)于發(fā)布/調(diào)用WS的主要步驟只是簡(jiǎn)單文字描述,沒(méi)有它寫(xiě)的詳盡2013-05-05
JavaMail發(fā)送(帶圖片和附件)和接收郵件實(shí)現(xiàn)詳解(四)
這篇文章主要為大家詳細(xì)介紹了JavaMail帶圖片和附件的發(fā)送和接收郵件實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

