Java中MyBatis的結(jié)果映射詳解
一、 概述
Java 數(shù)據(jù)持久層實(shí)現(xiàn)了應(yīng)用程序與數(shù)據(jù)源的交互,大多數(shù)時(shí)候需要使用到各種查詢語(yǔ)句。
MyBatis 支持對(duì)各種單表查詢、關(guān)聯(lián)查詢等各種復(fù)雜查詢的結(jié)果進(jìn)行映射。
二、結(jié)果映射
resultMap 元素是 MyBatis 中最重要最強(qiáng)大的元素,大部分查詢語(yǔ)句返回的結(jié)果,都能通過簡(jiǎn)單的配置來(lái)返回映射的 Java 對(duì)象。
假設(shè)存在這樣一個(gè)實(shí)體模型,一個(gè)用戶對(duì)應(yīng)有一個(gè)基本信息和擴(kuò)展信息,對(duì)應(yīng)有多個(gè)地址。
簡(jiǎn)單講,用戶基本信息與用戶擴(kuò)展信息是一對(duì)一的關(guān)系,與用戶地址是一對(duì)多的關(guān)系。

創(chuàng)建實(shí)體類和數(shù)據(jù)訪問接口如下:
// 用戶基本信息實(shí)體
@Data
public class User {
private Long id;
private String name;
private String username;
private String password;
private Integer status;
private UserExt extend;
private List<Address> addresses;
}
// 用戶擴(kuò)展信息實(shí)體
@Data
public class UserExt {
private Long id;
private Long userId;
private String phone;
private String email;
}
// 用戶地址信息實(shí)體
@Data
public class Address {
private Long id;
private Long userId;
private String address;
}
// 用戶相關(guān)接口
public interface UserExtMapper {
// 查詢用戶基本信息
User selectById(Long id);
// 查詢用戶基本信息和擴(kuò)展信息(一對(duì)一)
User selectExt(Long id);
// 查詢用戶基本信息和地址信息(一對(duì)多)
User selectAddresses(Long id);
}1. 基本映射
基本映射是對(duì)查詢返回的結(jié)果映射成一個(gè)最簡(jiǎn)單的 JavaBean 類,通過類屬性對(duì)應(yīng)數(shù)據(jù)庫(kù)字段進(jìn)行映射。
<id> 和 <result> 元素都將列映射到一個(gè)簡(jiǎn)單的數(shù)據(jù)類型(String, int, double 等)的屬性或字段。
其中的 property 對(duì)應(yīng) Java 類 屬性名稱,column 對(duì)應(yīng)數(shù)據(jù)庫(kù)列名稱。
<mapper namespace="cn.codeartist.mybatis.mapper.UserExtMapper">
<resultMap id="baseMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="status" column="status"/>
</resultMap>
<select id="selectById" resultMap="baseMap">
SELECT * FROM t_user WHERE id = #{id}
</select>
</mapper>2. 關(guān)聯(lián)映射
關(guān)聯(lián)映射是對(duì)查詢返回的結(jié)果映射成一對(duì)一關(guān)系的嵌套類,例如查詢同時(shí)返回用戶基本信息和用戶擴(kuò)展信息。
使用 <association> 元素來(lái)指定關(guān)聯(lián)映射,它可以映射一個(gè)關(guān)聯(lián)查詢語(yǔ)句的結(jié)果,也可以映射兩個(gè)查詢語(yǔ)句結(jié)合返回復(fù)雜的類型。
其中的 property 對(duì)應(yīng)主表類型的屬性,column 對(duì)應(yīng)主表關(guān)聯(lián)字段,javaType 對(duì)應(yīng)關(guān)聯(lián)表類型,select 對(duì)應(yīng)另一個(gè)查詢語(yǔ)句。
如果映射一個(gè)關(guān)聯(lián)語(yǔ)句的結(jié)果,則不會(huì)使用 select 屬性。
<mapper namespace="cn.codeartist.mybatis.mapper.UserExtMapper">
<resultMap id="associationMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="status" column="status"/>
<association property="extend" column="id" javaType="UserExt" select="selectUserExtById">
<id property="id" column="id"/>
<result property="userId" column="user_id"/>
<result property="phone" column="phone"/>
<result property="email" column="email"/>
</association>
</resultMap>
<select id="selectExt" resultMap="associationMap">
SELECT * FROM t_user WHERE id = #{id}
</select>
<select id="selectUserExtById" resultType="UserExt">
SELECT * FROM t_user_ext WHERE user_id = #{id}
</select>
</mapper>3. 集合映射
集合映射是對(duì)查詢返回的結(jié)果映射成一對(duì)多關(guān)系的嵌套類,例如查詢同時(shí)返回用戶信息和用戶的地址信息。
使用 <collection> 元素來(lái)指定集合映射,它可以映射一個(gè)關(guān)聯(lián)查詢語(yǔ)句的結(jié)果,也可以映射兩個(gè)查詢語(yǔ)句結(jié)合返回復(fù)雜的類型。
它的用法與關(guān)聯(lián)查詢 <association> 類似,只不過指定關(guān)聯(lián)表類型需要使用 ofType 屬性,為了區(qū)分集合存儲(chǔ)的類型。
<mapper namespace="cn.codeartist.mybatis.mapper.UserExtMapper">
<resultMap id="collectionMap" type="User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="status" column="status"/>
<collection property="addresses" column="id" ofType="Address" select="selectAddressById">
<id property="id" column="id"/>
<result property="userId" column="user_id"/>
<result property="address" column="address"/>
</collection>
</resultMap>
<select id="selectAddresses" resultMap="collectionMap">
SELECT * FROM t_user WHERE id = #{id}
</select>
<select id="selectAddressById" resultType="Address">
SELECT * FROM t_address WHERE user_id = #{id}
</select>
</mapper>同時(shí)指定集合類型和 Java 類型:
<collection property="addresses" javaType="ArrayList" column="id" ofType="Address" select="selectAddressById"/>
4. 自動(dòng)映射
MyBatis 支持在簡(jiǎn)單的場(chǎng)景下,可以自動(dòng)映射結(jié)果,在復(fù)雜的場(chǎng)景下,只需描述語(yǔ)句之間的關(guān)系就行。
當(dāng)配置自動(dòng)映射結(jié)果時(shí),數(shù)據(jù)庫(kù)列名與 Java 類屬性名稱會(huì)忽略大小寫映射,例如 ID 列會(huì)和 id 屬性進(jìn)行映射。
通常數(shù)據(jù)庫(kù)列名使用下劃線規(guī)范,而 Java 屬性遵循駝峰命名,將 mapUnderscoreToCamelCase 設(shè)置為 true 時(shí),會(huì)啟用自動(dòng)映射。
例如前面的基本映射、關(guān)聯(lián)映射和集合映射可以簡(jiǎn)化成下面配置:
<mapper namespace="cn.codeartist.mybatis.mapper.UserExtMapper">
<resultMap id="associationMap" type="User">
<association property="extend" column="id" javaType="UserExt" select="selectUserExtById"/>
</resultMap>
<resultMap id="collectionMap" type="User">
<collection property="addresses" column="id" ofType="Address" select="selectAddressById"/>
</resultMap>
<select id="selectById" resultType="User">
SELECT * FROM t_user WHERE id = #{id}
</select>
<select id="selectExt" resultMap="associationMap">
SELECT * FROM t_user WHERE id = #{id}
</select>
<select id="selectUserExtById" resultType="UserExt">
SELECT * FROM t_user_ext WHERE user_id = #{id}
</select>
<select id="selectAddresses" resultMap="collectionMap">
SELECT * FROM t_user WHERE id = #{id}
</select>
<select id="selectAddressById" resultType="Address">
SELECT * FROM t_address WHERE user_id = #{id}
</select>
</mapper>到此這篇關(guān)于Java中MyBatis的結(jié)果映射詳解的文章就介紹到這了,更多相關(guān)MyBatis的結(jié)果映射內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis延遲加載、關(guān)聯(lián)查詢與結(jié)果映射的實(shí)現(xiàn)原理解析
- 深度分析MybatisPlus查詢結(jié)果映射失敗@TableField失效解決辦法
- MyBatis 結(jié)果映射的兩種方式
- MyBatis結(jié)果映射(ResultMap)的使用
- MyBatis動(dòng)態(tài)SQL、模糊查詢與結(jié)果映射操作過程
- MyBatis中的SQL映射文件配置結(jié)果映射的操作指南
- 關(guān)于MyBatis結(jié)果映射的實(shí)例總結(jié)
- 基于mybatis查詢結(jié)果映射不到對(duì)象的處理
- MyBatis 結(jié)果映射的幾種實(shí)現(xiàn)方式
相關(guān)文章
完美解決idea創(chuàng)建文件時(shí),文件不分級(jí)展示的情況
這篇文章主要介紹了完美解決idea創(chuàng)建文件時(shí),文件不分級(jí)展示的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-02-02
springboot啟動(dòng)feign項(xiàng)目報(bào)錯(cuò):Service id not legal hostnam的解決
這篇文章主要介紹了springboot啟動(dòng)feign項(xiàng)目報(bào)錯(cuò):Service id not legal hostnam的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
springboot解決java.lang.ArrayStoreException異常
這篇文章介紹了springboot解決java.lang.ArrayStoreException異常的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
SpringBoot?Profiles?多環(huán)境配置及切換
大部分情況下,我們開發(fā)的產(chǎn)品應(yīng)用都會(huì)根據(jù)不同的目的,所以需要支持不同的環(huán)境,本文主要介紹了SpringBoot?Profiles?多環(huán)境配置及切換,感興趣的可以了解一下2021-12-12
SpringBoot 返回Json實(shí)體類屬性大小寫的解決
這篇文章主要介紹了SpringBoot 返回Json實(shí)體類屬性大小寫的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java根據(jù)前端返回的字段名進(jìn)行查詢數(shù)據(jù)的實(shí)現(xiàn)方法
在Java后端開發(fā)中,我們經(jīng)常需要根據(jù)前端傳遞的參數(shù)(如字段名)來(lái)動(dòng)態(tài)查詢數(shù)據(jù)庫(kù)中的數(shù)據(jù),這種需求通常出現(xiàn)在需要實(shí)現(xiàn)通用查詢功能或者復(fù)雜查詢接口的場(chǎng)景中,所以本文介紹了Java根據(jù)前端返回的字段名進(jìn)行查詢數(shù)據(jù)的實(shí)現(xiàn)方法,需要的朋友可以參考下2024-12-12
springboot配置數(shù)據(jù)庫(kù)密碼特殊字符報(bào)錯(cuò)的解決
這篇文章主要介紹了springboot配置數(shù)據(jù)庫(kù)密碼特殊字符報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
FastJson實(shí)現(xiàn)駝峰下劃線相互轉(zhuǎn)換方法詳解
這篇文章主要介紹了使用FastJson進(jìn)行駝峰下劃線相互轉(zhuǎn)換寫法及誤區(qū),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01

