Mybatis實(shí)現(xiàn)一對一查詢映射處理
一、概述
MyBatis是一種流行的Java持久化框架,它提供了靈活而強(qiáng)大的查詢映射功能。在一些復(fù)雜的數(shù)據(jù)模型中,一對一查詢映射是一種常見的需求。本篇博客將詳細(xì)介紹如何在MyBatis中處理一對一查詢映射。
二、創(chuàng)建數(shù)據(jù)模型
假設(shè)我們有兩張數(shù)據(jù)表,員工表和部門表,每個(gè)員工都只屬于一個(gè)部門,我們需要?jiǎng)?chuàng)建對應(yīng)的Java數(shù)據(jù)模型。
Emp.java
public class Emp {
private Integer eid;
private String empName;
private Integer age;
private String sex;
private String email;
private Dept dept;
...
}Dept.java
public class Dept {
private Integer did;
private String deptName;
private List<Emp> emps;
...
}三、 問題
現(xiàn)在我們要查詢員工信息以及員工所對應(yīng)的部門信息,我們應(yīng)該如何做呢?
四、解決方案
1、方案一:級聯(lián)方式處理映射關(guān)系
EmpMapper
/**
* @description:獲取指定員工的信息(包括部門)
* @author: Hey
* @date: 2022/7/4 8:58
* @param: [id]
* @return: com.ir.mybatis.pojo.Emp
**/
Emp getAllEmpAndDept(@Param("eid") Integer eid);EmpMapper.xml
<resultMap id="title1" type="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>
<result property="dept.did" column="did"></result>
<result property="dept.deptName" column="dept_name"></result>
</resultMap>
<select id="getAllEmpAndDept" resultMap="title1">
select * from t_emp left join t_dept on t_emp.did = t_dept .did where t_emp.eid = #{eid}
</select>ResultTest
/**
? ? ?* @description:獲取指定員工的信息(包括部門)
? ? ?* @author: Hey
? ? ?* @date: 2022/7/4 8:56
? ? ?* @param: []
? ? ?* @return: void
? ? ?**/
? ? @Test
? ? public void getAllEmpAndDept(){
? ? ? ? SqlSession sqlSession = SqlSessionUtils.getSqlSession();
? ? ? ? EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
? ? ? ? Emp emp = mapper.getAllEmpAndDept(2);
? ? ? ? System.out.println(emp);//Emp{eid=2, empName='美羊羊', age=32, sex='女', email='123@qq.com'}
? ? }2、方案二:使用association處理映射關(guān)系
EmpMapper
/**
* @description:獲取指定員工的信息(包括部門)
* @author: Hey
* @date: 2022/7/4 8:58
* @param: [id]
* @return: com.ir.mybatis.pojo.Emp
**/
Emp getAllEmpAndDept(@Param("eid") Integer eid);EmpMapper.xml
? ?<resultMap id="title1" type="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>
? ? ? ?<!--
? ? ? ? ? ? association:處理多對一的映射關(guān)系
? ? ? ? ? ? property:需要處理多對的映射關(guān)系的屬性名
? ? ? ? ? ? javaType:該屬性的類型
? ? ? ? ? ? 過程:通過javaType,運(yùn)用反射,確定其所有屬性,再將column一一準(zhǔn)確賦值
? ? ? ? ? ? 給指定的屬性,這樣就得出了一個(gè)實(shí)體類對象,再將這個(gè)對象賦值給property
? ? ? ? ? ? 中的對象名
? ? ? ? -->
? ? ? ? <association property="dept" javaType="Dept">
? ? ? ? ? ? <id property="did" column="did"></id>
? ? ? ? ? ? <result property="deptName" column="dept_name"></result>
? ? ? ? </association>
? ? </resultMap>
? ? <select id="getAllEmpAndDept" resultMap="title1">
? ? ? ? select * from t_emp left join t_dept on t_emp.did = t_dept .did where t_emp.eid = #{eid}
? ? </select>ResultTest
/**
? ? ?* @description:獲取指定員工的信息(包括部門)
? ? ?* @author: Hey
? ? ?* @date: 2022/7/4 8:56
? ? ?* @param: []
? ? ?* @return: void
? ? ?**/
? ? @Test
? ? public void getAllEmpAndDept(){
? ? ? ? SqlSession sqlSession = SqlSessionUtils.getSqlSession();
? ? ? ? EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
? ? ? ? Emp emp = mapper.getAllEmpAndDept(3);
? ? ? ? System.out.println(emp);//Emp{eid=3, empName='懶洋洋', age=34, sex='男', email='123@qq.com'}
? ? }3、方案三:分步查詢
mybatis-config.xml
?<!--設(shè)置MyBatis的全局配置--> ? ? <settings> ? ? ? ? <!--將_自動(dòng)映射為駝峰,emp_name:empName--> ? ? ? ? <setting name="mapUnderscoreToCamelCase" value="true"/> ? ? ? ? ?<!--開啟延遲加載--> ? ? ? ? <setting name="lazyLoadingEnabled" value="true"/> ? ? </settings>
EmpMapper
/**
* @description:通過分步查詢查詢員工以及員工所對應(yīng)的部門信息
* 分步查詢第一步:查詢員工信息
* @author: Hey
* @date: 2022/7/4 9:41
* @param: [eid]
* @return: com.ir.mybatis.pojo.Emp
**/
Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);EmpMapper.xml
<resultMap id="empAndDeptByStepResultMap" type="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>
? ? ? ? <!--
? ? ? ? ? ? select:設(shè)置分步查詢的sql的唯一標(biāo)識(namespace.SQLId或mapper接口的全類名.方法名)
? ? ? ? ? ? column:設(shè)置分布查詢的條件:根據(jù)員工的部門的did去查詢該員工所屬部門的信息
? ? ? ? ? ? fetchType:當(dāng)開啟了全局的延遲加載之后,可通過此屬性手動(dòng)控制延遲加載的效果
? ? ? ? ? ? fetchType="lazy|eager":lazy表示延遲加載,eager表示立即加載
? ? ? ? -->
? ? ? ? <association property="dept"
? ? ? ? ? ? ? ? ? ? ?select="com.ir.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
? ? ? ? ? ? ? ? ? ? ?column="did"
? ? ? ? ? ? ? ? ? ? ?>
? ? ? ? </association>
</resultMap>
? ? <!--Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);-->
? ? <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
? ? ? ? select * from t_emp where eid = #{eid}
? ? </select>DeptMapper
/**
* @description:通過分步查詢查詢部門以及部門中所有的員工信息
* 分步查詢第二步:根據(jù)did查詢員工信息
* @author: Hey
* @date: 2022/7/4 9:42
* @param: [did]
* @return: java.util.List<com.ir.mybatis.pojo.Emp>
**/
List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);DeptMapper.xml
<!--Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);-->
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
select * from t_dept where did = #{did}
</select>ResultTest
/**
? ? ?* @description:通過分步查詢查詢部門以及部門中所有的員工信息
? ? ?* @author: Hey?
? ? ?* @date: 2022/7/4 9:53
? ? ?* @param: []
? ? ?* @return: void
? ? ?**/
? ? @Test
? ? public void testGetEmpAndDeptByStep(){
? ? ? ? SqlSession sqlSession = SqlSessionUtils.getSqlSession();
? ? ? ? EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
? ? ? ? Emp emp = mapper.getEmpAndDeptByStepOne(3);
? ? ? ? System.out.println(emp);//Emp{eid=3, empName='懶洋洋', age=34, sex='男', email='123@qq.com'}
? ? }到此這篇關(guān)于Mybatis實(shí)現(xiàn)一對一查詢映射處理的文章就介紹到這了,更多相關(guān)Mybatis 查詢映射內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis注解開發(fā)之實(shí)現(xiàn)自定義映射關(guān)系和關(guān)聯(lián)查詢
- MyBatis自定義映射關(guān)系和關(guān)聯(lián)查詢實(shí)現(xiàn)方法詳解
- 基于mybatis查詢結(jié)果映射不到對象的處理
- mybatis框架的xml映射文件常用查詢指南
- Mybatis模糊查詢及自動(dòng)映射實(shí)現(xiàn)詳解
- 基于mybatis高級映射多對多查詢的實(shí)現(xiàn)
- mybatis高級映射一對多查詢實(shí)現(xiàn)代碼
- MyBatis中多對多關(guān)系的映射和查詢
- MyBatis高級映射和查詢緩存
相關(guān)文章
Java將String字符串帶括號轉(zhuǎn)成List的簡單方法
Java中我們有時(shí)需要對現(xiàn)有的字符串進(jìn)行切割并轉(zhuǎn)化成一個(gè)List集合,這篇文章主要給大家介紹了關(guān)于Java將String字符串帶括號轉(zhuǎn)成List的簡單方法,需要的朋友可以參考下2023-03-03
RabbitMq報(bào)錯(cuò)reply-code=406 reply-text=PRECONDITION_FAILED
這篇文章主要為大家介紹了RabbitMq報(bào)錯(cuò)reply-code=406 reply-text=PRECONDITION_FAILED分析解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
java實(shí)現(xiàn)人工智能化屏幕監(jiān)控窗口
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)人工智能化屏幕監(jiān)控窗口,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Mybatis如何使用動(dòng)態(tài)語句實(shí)現(xiàn)批量刪除(delete結(jié)合foreach)
這篇文章主要介紹了Mybatis如何使用動(dòng)態(tài)語句實(shí)現(xiàn)批量刪除(delete結(jié)合foreach),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Spring事務(wù)@Transactional注解四種不生效案例場景分析
這篇文章主要為大家介紹了Spring事務(wù)@Transactional注解四種不生效的案例場景示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
springBoot 整合ModBus TCP的詳細(xì)過程
ModBus是一種串行通信協(xié)議,用于從儀器和控制設(shè)備傳輸信號到主控制器或數(shù)據(jù)采集系統(tǒng),它分為主站和從站,主站獲取和編寫數(shù)據(jù),從站則是設(shè)備,本文給大家介紹springBoot 整合ModBus TCP的詳細(xì)過程,感興趣的朋友一起看看吧2025-01-01
利用Java寫一個(gè)學(xué)生管理系統(tǒng)
今天這篇文章就給給大家分享利用Java寫一個(gè)學(xué)生管理系統(tǒng)吧,先寫一個(gè)簡單的用List來實(shí)現(xiàn)學(xué)生管理系統(tǒng):2021-09-09

