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

MyBatis中ResultMap與多表查詢的處理方法

 更新時(shí)間:2023年09月13日 12:12:44   作者:清河__  
這篇文章主要介紹了MyBatis中ResultMap與多表查詢的處理方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

ResultMap與多表查詢的處理

當(dāng)字段名與實(shí)類名不一致時(shí)

使用別名進(jìn)行處理

字段名:emp_name

實(shí)體類名:empName

映射文件中寫法:

    <select id="getAllEmp" resultType="Emp">
        select eid, emp_name empName, age, sex, email, did from t_emp
    </select>

使用全局配置將下劃線命名映射為駝峰

在mybatis-config.xml文件的properties標(biāo)簽和typeAlias標(biāo)簽之間添加settings標(biāo)簽如下,可以將下劃線式命名映射為駝峰:

    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

使用resultMap創(chuàng)建自定義的映射關(guān)系

在mapper.xml文件中進(jìn)行定義:

定義resultMap:

    <!-- 就算是自定義映射關(guān)系,也需要相對(duì)應(yīng)的實(shí)體類 -->
    <resultMap id="empResultMap" type="Emp">
        <!-- id用來(lái)聲明主鍵,property用來(lái)表示實(shí)體類中的屬性名、column用來(lái)標(biāo)識(shí)數(shù)據(jù)庫(kù)表中的字段名 -->
        <id property="eid" column="eid"></id>
        <!-- result用來(lái)聲明普通字段 -->
        <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>
    </resultMap>

傳入resultMap的id以用來(lái)使用自定義映射

    <select id="getAllEmp" resultMap="empResultMap">
        select * from t_emp
    </select>

注意:resultMap一般用于處理多對(duì)一、一對(duì)多的關(guān)系

一對(duì)多情況的處理

對(duì)于多對(duì)一的情況(一個(gè)員工只會(huì)在一個(gè)部門中)

只需要在員工實(shí)體類中添加一個(gè)部門屬性:

    private Integer eid;
    private String empName;
    private Integer age;
    private String sex;
    private String email;
    private Integer did;
    private Dept dept;

通過(guò)級(jí)聯(lián)屬性賦值resultMap解決多對(duì)一問(wèn)題

在mapper.xml文件中創(chuàng)建resultMap:

    <resultMap id="empAndDeptResultMap" 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>

再將這個(gè)傳入語(yǔ)句進(jìn)行調(diào)用

<!--    Emp getEmpAndDept(@Param("eid") Integer eid);-->
    <select id="getEmpAndDept" resultMap="empAndDeptResultMap">
        select *
        from t_emp left join t_dept on t_emp.did = t_dept.did
        where eid=#{eid}
    </select>

級(jí)聯(lián)屬性賦值的方式一般不使用

使用association解決多對(duì)一問(wèn)題

在mapper.xml文件中創(chuàng)建resultMap:

    <resultMap id="empAndDeptResultMapAsscoiation" 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 property="dept" javaType="Dept">
            <id property="did" column="did"></id>
            <result property="deptName" column="dept_name"></result>
        </association>
    </resultMap>

再將之傳入即可

通過(guò)分步查詢解決多對(duì)一問(wèn)題

在mapper.xml建立如下:

<!--    注意在分步查詢的過(guò)程中,association中的column代表傳入的條件-->
    <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>
        <association property="dept"
                     select="com.qinghe.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="did"></association>
    </resultMap>

Dept的mapper如下:

<!--    Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);-->
    <select id="getEmpAndDeptByStepTwo" resultType="Dept">
        select * from t_dept where did = #{did}
    </select>

調(diào)用:

    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select * from t_emp where eid = #{eid}
    </select>

分布查詢的優(yōu)點(diǎn):延遲加載

當(dāng)mybatis的查詢語(yǔ)句由多步查詢構(gòu)成時(shí),我們可以開(kāi)啟mybatis的懶加載,此時(shí)若我們只需要第一步的某個(gè)屬性,mybatis就不會(huì)去調(diào)用第二步的sql語(yǔ)句,這樣在多種場(chǎng)景下最大限度的保證了性能。

在全局配置(mybatis-config.xml)中添加如下配置

<!--    全局配置:自動(dòng)將下劃線轉(zhuǎn)為駝峰,懶加載-->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="lazyLoadingEnabled" value="true"></setting>
    </settings>

同時(shí),我們也可以在association中使用fetchType標(biāo)簽來(lái)使延遲加載變得可控,eager代表立即加載、lazy代表延遲加載

<!--    注意在分步查詢的過(guò)程中,association中的column代表傳入的條件-->
    <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>
        <association property="dept"
                     select="com.qinghe.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="did"
                     fetchType="eager">
        </association>
    </resultMap>

多對(duì)一情況的處理

在dept實(shí)體類中添加多的的那個(gè)List:

    private List<Emp> empList;

使用collection標(biāo)簽進(jìn)行一口氣的處理

在deptMapper下進(jìn)行如下操作:

    <resultMap id="deptAndEmpResultMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
<!--        這里的ofType代表傳入的List的泛型-->
        <collection property="empList" 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>
    <select id="getDeptAndEmp" resultMap="deptAndEmpResultMap">
        select *
        from t_emp left join t_dept on t_emp.did = t_dept.did
        where t_dept.did=#{did}
    </select>

通過(guò)分步查詢進(jìn)行處理

在DeptMapper中建立第一步的接口:

    /**
     * 通過(guò)分步查詢部門以及部門中的員工信息
     */
    Dept getDeptAndEmpByStepOne(@Param("did") Integer did);

在EmpMapper中建立第二步的接口:

    /**
     * 分步查詢第二步,根據(jù)did查詢員工信息
     */
    List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);

在EmpMapper.xml文件中定義xml:

<!--    List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);-->
    <select id="getDeptAndEmpByStepTwo" resultType="Emp">
        select * from t_emp
    </select>

在DeptMapper.xml文件中定義xml:

    <resultMap id="deptAndEmpByStepResultMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        <collection property="empList"
                    select="com.qinghe.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                    column="did"
        ></collection>
    </resultMap>
    <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">
        select * from t_dept where did = #{did}
    </select>

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

相關(guān)文章

  • Java并發(fā)教程之Callable和Future接口詳解

    Java并發(fā)教程之Callable和Future接口詳解

    Java從發(fā)布的第一個(gè)版本開(kāi)始就可以很方便地編寫多線程的應(yīng)用程序,并在設(shè)計(jì)中引入異步處理,這篇文章主要給大家介紹了關(guān)于Java并發(fā)教程之Callable和Future接口的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • Java如何自動(dòng)生成api文檔

    Java如何自動(dòng)生成api文檔

    在?Java?開(kāi)發(fā)中,自動(dòng)生成?API?文檔是一項(xiàng)非常實(shí)用的功能,這篇為大家介紹了幾種常見(jiàn)的?Java?自動(dòng)生成?API?文檔的方式,需要的可以參考一下
    2025-02-02
  • Spring IOC創(chuàng)建對(duì)象的兩種方式

    Spring IOC創(chuàng)建對(duì)象的兩種方式

    這篇文章主要給大家介紹了關(guān)于Spring IOC創(chuàng)建對(duì)象的兩種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java利用httpclient通過(guò)get、post方式調(diào)用https接口的方法

    Java利用httpclient通過(guò)get、post方式調(diào)用https接口的方法

    這篇文章主要介紹了Java利用httpclient通過(guò)get、post方式調(diào)用https接口的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • 詳細(xì)總結(jié)IDEA中打jar包的兩種方式

    詳細(xì)總結(jié)IDEA中打jar包的兩種方式

    發(fā)現(xiàn)有很多小伙伴都不會(huì)用IDEA打jar包,今天給大家詳細(xì)總結(jié)了兩種IDEA打jar包的方式,對(duì)正在學(xué)習(xí)IDEA使用的小伙伴很有幫助,需要的朋友可以參考下
    2021-05-05
  • SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list

    SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list

    這篇文章主要介紹了SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 基于java構(gòu)造方法Vector查找元素源碼分析

    基于java構(gòu)造方法Vector查找元素源碼分析

    本篇文章是關(guān)于ava構(gòu)造方法Vector源碼分析系列文章,本文主要介紹了Vector查找元素的源碼分析,有需要的朋友可以借鑒參考下,希望可以有所幫助
    2021-09-09
  • springboot對(duì)接微信支付的完整流程(附前后端代碼)

    springboot對(duì)接微信支付的完整流程(附前后端代碼)

    最近在做支付平臺(tái)的項(xiàng)目,承接公司業(yè)務(wù)系統(tǒng)與第三方支付平臺(tái)的對(duì)接任務(wù),主要涉及微信支付、支付寶支付以及理房通支付等第三方平臺(tái),這篇文章主要給大家介紹了關(guān)于springboot對(duì)接微信支付的完整流程,需要的朋友可以參考下
    2021-08-08
  • Spring條件注解沒(méi)生效該如何解決

    Spring條件注解沒(méi)生效該如何解決

    條件注解相信各位小伙伴都用過(guò),Spring?中的多環(huán)境配置?profile?底層就是通過(guò)條件注解來(lái)實(shí)現(xiàn)的,下面小編就來(lái)為大家介紹一下當(dāng)Spring條件注解沒(méi)生效時(shí)該如何解決,感興趣的可以了解下
    2023-09-09
  • 出現(xiàn)log.info報(bào)紅的解決方案

    出現(xiàn)log.info報(bào)紅的解決方案

    這篇文章主要介紹了出現(xiàn)log.info報(bào)紅的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06

最新評(píng)論