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

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

 更新時(shí)間:2023年09月13日 12:12:44   作者:清河__  
這篇文章主要介紹了MyBatis中ResultMap與多表查詢的處理方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(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)系,也需要相對應(yīng)的實(shí)體類 -->
    <resultMap id="empResultMap" type="Emp">
        <!-- id用來聲明主鍵,property用來表示實(shí)體類中的屬性名、column用來標(biāo)識數(shù)據(jù)庫表中的字段名 -->
        <id property="eid" column="eid"></id>
        <!-- result用來聲明普通字段 -->
        <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以用來使用自定義映射

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

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

一對多情況的處理

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

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

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

通過級聯(lián)屬性賦值resultMap解決多對一問題

在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è)傳入語句進(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>

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

使用association解決多對一問題

在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>

再將之傳入即可

通過分步查詢解決多對一問題

在mapper.xml建立如下:

<!--    注意在分步查詢的過程中,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的查詢語句由多步查詢構(gòu)成時(shí),我們可以開啟mybatis的懶加載,此時(shí)若我們只需要第一步的某個(gè)屬性,mybatis就不會去調(diào)用第二步的sql語句,這樣在多種場景下最大限度的保證了性能。

在全局配置(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)簽來使延遲加載變得可控,eager代表立即加載、lazy代表延遲加載

<!--    注意在分步查詢的過程中,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>

多對一情況的處理

在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>

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

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

    /**
     * 通過分步查詢部門以及部門中的員工信息
     */
    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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Logback的使用及如何配置

    Logback的使用及如何配置

    這篇文章主要介紹了Logback的使用及如何配置,幫助大家更好的理解和學(xué)習(xí)使用Logback,感興趣的朋友可以了解下
    2021-03-03
  • Java ForkJoinPool線程池的使用之并行計(jì)算數(shù)組求和實(shí)例

    Java ForkJoinPool線程池的使用之并行計(jì)算數(shù)組求和實(shí)例

    這篇文章主要介紹了Java ForkJoinPool線程池的使用之并行計(jì)算數(shù)組求和實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-05-05
  • Spring boot 跳轉(zhuǎn)到j(luò)sp頁面的實(shí)現(xiàn)方法

    Spring boot 跳轉(zhuǎn)到j(luò)sp頁面的實(shí)現(xiàn)方法

    本篇文章主要介紹了Spring boot 跳轉(zhuǎn)到j(luò)sp頁面的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • java.sql.SQLRecoverableException關(guān)閉的連接異常問題及解決辦法

    java.sql.SQLRecoverableException關(guān)閉的連接異常問題及解決辦法

    當(dāng)數(shù)據(jù)庫連接池中的連接被創(chuàng)建而長時(shí)間不使用的情況下,該連接會自動(dòng)回收并失效,就導(dǎo)致客戶端程序報(bào)“ java.sql.SQLException: Io 異常: Connection reset” 或“java.sql.SQLException 關(guān)閉的連接”異常問題,下面給大家分享解決方案,一起看看吧
    2024-03-03
  • 詳解Java注解的實(shí)現(xiàn)與使用方法

    詳解Java注解的實(shí)現(xiàn)與使用方法

    這篇文章主要介紹了詳解Java注解的實(shí)現(xiàn)與使用方法的相關(guān)資料,希望通過本文大家能夠理解掌握J(rèn)ava注解的知識,需要的朋友可以參考下
    2017-09-09
  • Javassist用法詳解

    Javassist用法詳解

    這篇文章主要介紹了Javassist用法的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-02-02
  • Spring Boot中使用Spring-data-jpa的配置方法詳解

    Spring Boot中使用Spring-data-jpa的配置方法詳解

    今天小編就為大家分享一篇關(guān)于Spring Boot中使用Spring-data-jpa的配置方法詳解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Spring Boot2.X國際化文件編寫配置

    Spring Boot2.X國際化文件編寫配置

    這篇文章主要介紹了Spring Boot2.X國際化文件編寫配置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • jdk8的datetime時(shí)間函數(shù)使用示例

    jdk8的datetime時(shí)間函數(shù)使用示例

    這篇文章主要介紹了jdk8的datetime時(shí)間函數(shù)使用示例,需要的朋友可以參考下
    2014-03-03
  • Java 通配符詳解:?、? extends、? super 一篇搞懂

    Java 通配符詳解:?、? extends、? super 一篇搞懂

    本文深入解析Java泛型中的通配符(Wildcard)機(jī)制,重點(diǎn)講解無界通配符(?)、上界通配符(? extends T)和下界通配符(? super T)的用法與區(qū)別,感興趣的可以了解一下
    2025-10-10

最新評論

泗水县| 千阳县| 波密县| 商丘市| 阳谷县| 开阳县| 体育| 蓝山县| 阳城县| 调兵山市| 沈丘县| 正安县| 岳西县| 兴城市| 定日县| 克什克腾旗| 昔阳县| 甘洛县| 时尚| 华阴市| 清水河县| 桃江县| 黄龙县| 宜川县| 荔波县| 南雄市| 句容市| 江口县| 来宾市| 康乐县| 壶关县| 龙游县| 肇庆市| 万荣县| 疏附县| 大庆市| 依兰县| 通化市| 乐山市| 民丰县| 安西县|