Mybatis實現(xiàn)一對多映射處理
一、概述
一對多關(guān)系表示一個實體對象(一)可以擁有多個關(guān)聯(lián)對象(多)。
例如,一個用戶可以有多個訂單,或者一個部門可以有多個員工。
在數(shù)據(jù)庫中,一對多關(guān)系通常通過外鍵來實現(xiàn)。
二、創(chuàng)建數(shù)據(jù)模型
定義實體類:定義主表實體類和從表實體類,主表實體類 中包含 從表實體類 的List集合屬性。
現(xiàn)在我們以此來創(chuàng)建面向?qū)ο笳Z言的對象數(shù)據(jù)模型:
Dept.java
public class Dept { private Integer did; private String deptName; private List<Emp> emps;//用于表示數(shù)據(jù)庫一對多的關(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方法 }
三、問題
假設(shè)我們現(xiàn)在又兩張表,一張員工表,一張部門表,現(xiàn)在我們要獲取部門以及該部門中所有的員工信息
四、解決方案
1、方案一:collection(嵌套結(jié)果)
嵌套結(jié)果:使用嵌套結(jié)果的方式,可以在查詢主實體對象的同時,通過嵌套的方式將關(guān)聯(lián)實體對象的屬性嵌套到主實體對象的屬性中。這種方式的實現(xiàn)需要在Mapper.xml
文件中定義一個SQL語句,使用嵌套的方式將關(guān)聯(lián)實體對象的屬性映射到主實體對象的屬性中。在定義映射關(guān)系時,需要使用resultMap
標簽來定義主實體對象和關(guān)聯(lián)實體對象的映射關(guān)系,使用collection
標簽來嵌套關(guān)聯(lián)實體對象的屬性。
DeptMapper.java
/** * @description:獲取部門以及部門中所有的員工信息 * @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:處理一對多的映射關(guān)系 ofType:表示該屬性所對應(yīng)的集合中存儲數(shù)據(jù)的類型 --> <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:獲取部門以及部門中所有的員工信息 * @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、方案二:分步查詢(嵌套查詢)
嵌套查詢:使用嵌套查詢的方式,可以在查詢主實體對象的同時,通過查詢關(guān)聯(lián)實體對象的方式獲取相關(guān)聯(lián)的多個實體對象。這種方式的實現(xiàn)需要在Mapper.xml
文件中定義兩個獨立的SQL語句,一個用于查詢主實體對象,另一個用于查詢關(guān)聯(lián)實體對象。在查詢主實體對象時,通過使用select
標簽的子標簽來執(zhí)行關(guān)聯(lián)實體對象的查詢,并將查詢結(jié)果映射到主實體對象的屬性中。
DeptMapper
/** * @description:通過分步查詢查詢部門以及部門中所有的員工信息 * 分步查詢第一步:查詢部門信息 * @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:通過分步查詢查詢部門以及部門中所有的員工信息 * 分步查詢第二步:根據(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:通過分步查詢查詢部門以及部門中所有的員工信息 * @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()); }
無論是使用嵌套查詢還是嵌套結(jié)果的方式,都需要在Mapper.xml文件中定義相應(yīng)的SQL語句和映射關(guān)系。同時,為了提高查詢性能,可以使用Mybatis的延遲加載機制來減少查詢次數(shù),提高數(shù)據(jù)訪問效率。
到此這篇關(guān)于Mybatis實現(xiàn)一對多映射處理的文章就介紹到這了,更多相關(guān)Mybatis映射內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java使用Nagao算法實現(xiàn)新詞發(fā)現(xiàn)、熱門詞的挖掘
這篇文章主要介紹了java使用Nagao算法實現(xiàn)新詞發(fā)現(xiàn)、熱門詞的挖掘的思路和詳細代碼,需要的朋友可以參考下2015-07-07java實現(xiàn)簡單學(xué)生成績檔案管理系統(tǒng)
這篇文章主要為大家詳細介紹了java實現(xiàn)簡單學(xué)生成績檔案管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05SpringBoot快速構(gòu)建應(yīng)用程序方法介紹
這篇文章主要介紹了SpringBoot快速構(gòu)建應(yīng)用程序方法介紹,涉及SpringBoot默認的錯誤頁面,嵌入式Web容器層面的約定和定制等相關(guān)內(nèi)容,具有一定借鑒價值,需要的朋友可以參考下。2017-11-11Mybatis-Plus中update()和updateById()將字段更新為null
本文主要介紹了Mybatis-Plus中update()和updateById()將字段更新為null,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Springboot整合多數(shù)據(jù)源代碼示例詳解
這篇文章主要介紹了Springboot整合多數(shù)據(jù)源代碼示例詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08SpringBoot整合Swagger頁面禁止訪問swagger-ui.html方式
本文介紹了如何在SpringBoot項目中通過配置SpringSecurity和創(chuàng)建攔截器來禁止訪問SwaggerUI頁面,此外,還提供了禁用SwaggerUI和Swagger資源的配置方法,以確保這些端點和頁面對外部用戶不可見或無法訪問2025-02-02