最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis實(shí)現(xiàn)一對(duì)一查詢(xún)映射處理

 更新時(shí)間:2023年08月01日 15:04:18   作者:_GGBond_  
MyBatis是一種流行的Java持久化框架,它提供了靈活而強(qiáng)大的查詢(xún)映射功能,本文主要介紹了Mybatis實(shí)現(xiàn)一對(duì)一查詢(xún)映射處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、概述

MyBatis是一種流行的Java持久化框架,它提供了靈活而強(qiáng)大的查詢(xún)映射功能。在一些復(fù)雜的數(shù)據(jù)模型中,一對(duì)一查詢(xún)映射是一種常見(jiàn)的需求。本篇博客將詳細(xì)介紹如何在MyBatis中處理一對(duì)一查詢(xún)映射。

二、創(chuàng)建數(shù)據(jù)模型

假設(shè)我們有兩張數(shù)據(jù)表,員工表和部門(mén)表,每個(gè)員工都只屬于一個(gè)部門(mén),我們需要?jiǎng)?chuàng)建對(duì)應(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;
    ...
    }

三、 問(wèn)題

現(xiàn)在我們要查詢(xún)員工信息以及員工所對(duì)應(yīng)的部門(mén)信息,我們應(yīng)該如何做呢?

四、解決方案

1、方案一:級(jí)聯(lián)方式處理映射關(guān)系

EmpMapper

/**
  * @description:獲取指定員工的信息(包括部門(mén))
  * @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:獲取指定員工的信息(包括部門(mén))
? ? ?* @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:獲取指定員工的信息(包括部門(mén))
  * @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:處理多對(duì)一的映射關(guān)系
? ? ? ? ? ? property:需要處理多對(duì)的映射關(guān)系的屬性名
? ? ? ? ? ? javaType:該屬性的類(lèi)型
? ? ? ? ? ? 過(guò)程:通過(guò)javaType,運(yùn)用反射,確定其所有屬性,再將column一一準(zhǔn)確賦值
? ? ? ? ? ? 給指定的屬性,這樣就得出了一個(gè)實(shí)體類(lèi)對(duì)象,再將這個(gè)對(duì)象賦值給property
? ? ? ? ? ? 中的對(duì)象名
? ? ? ? -->
? ? ? ? <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:獲取指定員工的信息(包括部門(mén))
? ? ?* @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、方案三:分步查詢(xún)

mybatis-config.xml

?<!--設(shè)置MyBatis的全局配置-->
? ? <settings>
? ? ? ? <!--將_自動(dòng)映射為駝峰,emp_name:empName-->
? ? ? ? <setting name="mapUnderscoreToCamelCase" value="true"/>
? ? ? ? ?<!--開(kāi)啟延遲加載-->
? ? ? ? <setting name="lazyLoadingEnabled" value="true"/>
? ? </settings>

EmpMapper

/**
     * @description:通過(guò)分步查詢(xún)查詢(xún)員工以及員工所對(duì)應(yīng)的部門(mén)信息
     *              分步查詢(xún)第一步:查詢(xún)員工信息
     * @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è)置分步查詢(xún)的sql的唯一標(biāo)識(shí)(namespace.SQLId或mapper接口的全類(lèi)名.方法名)
? ? ? ? ? ? column:設(shè)置分布查詢(xún)的條件:根據(jù)員工的部門(mén)的did去查詢(xún)?cè)搯T工所屬部門(mén)的信息
? ? ? ? ? ? fetchType:當(dāng)開(kāi)啟了全局的延遲加載之后,可通過(guò)此屬性手動(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:通過(guò)分步查詢(xún)查詢(xún)部門(mén)以及部門(mén)中所有的員工信息
     *              分步查詢(xún)第二步:根據(jù)did查詢(xún)員工信息
     * @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:通過(guò)分步查詢(xún)查詢(xún)部門(mén)以及部門(mén)中所有的員工信息
? ? ?* @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)一對(duì)一查詢(xún)映射處理的文章就介紹到這了,更多相關(guān)Mybatis 查詢(xún)映射內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • IDEA報(bào)錯(cuò)內(nèi)存溢出(java.lang.OutOfMemoryError)解決辦法

    IDEA報(bào)錯(cuò)內(nèi)存溢出(java.lang.OutOfMemoryError)解決辦法

    在Java開(kāi)發(fā)過(guò)程中,IntelliJ IDEA作為主流的集成開(kāi)發(fā)環(huán)境,其穩(wěn)定性與性能直接關(guān)系到開(kāi)發(fā)效率與項(xiàng)目推進(jìn)節(jié)奏,這篇文章主要介紹了IDEA報(bào)錯(cuò)內(nèi)存溢出(java.lang.OutOfMemoryError)解決辦法的相關(guān)資料,需要的朋友可以參考下
    2026-03-03
  • Java 8 Stream.distinct() 列表去重的操作

    Java 8 Stream.distinct() 列表去重的操作

    這篇文章主要介紹了Java 8 Stream.distinct() 列表去重的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • 關(guān)于ThreadLocal的用法和說(shuō)明及注意事項(xiàng)

    關(guān)于ThreadLocal的用法和說(shuō)明及注意事項(xiàng)

    這篇文章主要介紹了關(guān)于ThreadLocal的用法和說(shuō)明及注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • spring cloud gateway如何獲取請(qǐng)求的真實(shí)地址

    spring cloud gateway如何獲取請(qǐng)求的真實(shí)地址

    這篇文章主要介紹了spring cloud gateway如何獲取請(qǐng)求的真實(shí)地址問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 一文分享Java日期解析的完整方案(覆蓋50+種格式自動(dòng)識(shí)別)

    一文分享Java日期解析的完整方案(覆蓋50+種格式自動(dòng)識(shí)別)

    寫(xiě)后端接口,日期解析是個(gè)躲不過(guò)去的坎,本文主要和大家分享一下Java中日期解析的完整方案,一個(gè)方法可以覆蓋50+種格式自動(dòng)識(shí)別,希望對(duì)大家有所幫助
    2026-06-06
  • Spring MVC整合FreeMarker的示例

    Spring MVC整合FreeMarker的示例

    這篇文章主要介紹了Spring MVC整合FreeMarker的示例,幫助大家更好的理解和使用Spring MVC,感興趣的朋友可以了解下
    2020-12-12
  • 使用jenkins+maven+git發(fā)布jar包過(guò)程詳解

    使用jenkins+maven+git發(fā)布jar包過(guò)程詳解

    這篇文章主要介紹了使用jenkins+maven+git發(fā)布jar包過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • SpringBoot Event實(shí)現(xiàn)異步消費(fèi)機(jī)制的示例代碼

    SpringBoot Event實(shí)現(xiàn)異步消費(fèi)機(jī)制的示例代碼

    這篇文章主要介紹了SpringBoot Event實(shí)現(xiàn)異步消費(fèi)機(jī)制,ApplicationEvent以及Listener是Spring為我們提供的一個(gè)事件監(jiān)聽(tīng)、訂閱的實(shí)現(xiàn),內(nèi)部實(shí)現(xiàn)原理是觀察者設(shè)計(jì)模式,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2024-04-04
  • 通過(guò)Java實(shí)現(xiàn)RSA加密與驗(yàn)證的方法詳解

    通過(guò)Java實(shí)現(xiàn)RSA加密與驗(yàn)證的方法詳解

    RSA是一種非對(duì)稱(chēng)加密算法,是目前廣泛應(yīng)用于加密和數(shù)字簽名領(lǐng)域的一種加密算法,本文主要講述如何通過(guò)Java實(shí)現(xiàn)RSA加密與驗(yàn)證,應(yīng)用場(chǎng)景為與其他平臺(tái)對(duì)接接口時(shí),通過(guò)RSA加密和解密驗(yàn)證請(qǐng)求的有效性,在對(duì)接時(shí)雙方互換公鑰,需要的朋友可以參考下
    2023-12-12
  • Java使用Redis實(shí)現(xiàn)消息訂閱/發(fā)布的幾種方式

    Java使用Redis實(shí)現(xiàn)消息訂閱/發(fā)布的幾種方式

    Redis 提供了 Pub/Sub (發(fā)布/訂閱) 模式,允許客戶(hù)端訂閱頻道并接收發(fā)布到這些頻道的消息,以下是 Java 中使用 Redis 實(shí)現(xiàn)消息訂閱的幾種方式,需要的朋友可以參考下
    2025-08-08

最新評(píng)論

温州市| 梅河口市| 西峡县| 济南市| 全椒县| 万载县| 息烽县| 灌南县| 河西区| 岳池县| 当阳市| 北宁市| 上蔡县| 东光县| 永平县| 美姑县| 朝阳县| 晋宁县| 金平| 宜章县| 长汀县| 逊克县| 崇礼县| 子长县| 苍南县| 翁源县| 安仁县| 浪卡子县| 论坛| 枣庄市| 高邑县| 新和县| 永泰县| 呈贡县| 大邑县| 千阳县| 信丰县| 台中县| 香格里拉县| 漠河县| 防城港市|