Java中MyBatis的結(jié)果映射詳解
一、 概述
Java 數(shù)據(jù)持久層實(shí)現(xiàn)了應(yīng)用程序與數(shù)據(jù)源的交互,大多數(shù)時(shí)候需要使用到各種查詢(xún)語(yǔ)句。
MyBatis 支持對(duì)各種單表查詢(xún)、關(guān)聯(lián)查詢(xún)等各種復(fù)雜查詢(xún)的結(jié)果進(jìn)行映射。
二、結(jié)果映射
resultMap 元素是 MyBatis 中最重要最強(qiáng)大的元素,大部分查詢(xún)語(yǔ)句返回的結(jié)果,都能通過(guò)簡(jiǎn)單的配置來(lái)返回映射的 Java 對(duì)象。
假設(shè)存在這樣一個(gè)實(shí)體模型,一個(gè)用戶(hù)對(duì)應(yīng)有一個(gè)基本信息和擴(kuò)展信息,對(duì)應(yīng)有多個(gè)地址。
簡(jiǎn)單講,用戶(hù)基本信息與用戶(hù)擴(kuò)展信息是一對(duì)一的關(guān)系,與用戶(hù)地址是一對(duì)多的關(guān)系。
創(chuàng)建實(shí)體類(lèi)和數(shù)據(jù)訪(fǎng)問(wèn)接口如下:
// 用戶(hù)基本信息實(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; } // 用戶(hù)擴(kuò)展信息實(shí)體 @Data public class UserExt { private Long id; private Long userId; private String phone; private String email; } // 用戶(hù)地址信息實(shí)體 @Data public class Address { private Long id; private Long userId; private String address; } // 用戶(hù)相關(guān)接口 public interface UserExtMapper { // 查詢(xún)用戶(hù)基本信息 User selectById(Long id); // 查詢(xún)用戶(hù)基本信息和擴(kuò)展信息(一對(duì)一) User selectExt(Long id); // 查詢(xún)用戶(hù)基本信息和地址信息(一對(duì)多) User selectAddresses(Long id); }
1. 基本映射
基本映射是對(duì)查詢(xún)返回的結(jié)果映射成一個(gè)最簡(jiǎn)單的 JavaBean 類(lèi),通過(guò)類(lèi)屬性對(duì)應(yīng)數(shù)據(jù)庫(kù)字段進(jìn)行映射。
<id>
和 <result>
元素都將列映射到一個(gè)簡(jiǎn)單的數(shù)據(jù)類(lèi)型(String, int, double
等)的屬性或字段。
其中的 property
對(duì)應(yīng) Java 類(lèi) 屬性名稱(chēng),column
對(duì)應(yīng)數(shù)據(jù)庫(kù)列名稱(chēng)。
<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ì)查詢(xún)返回的結(jié)果映射成一對(duì)一關(guān)系的嵌套類(lèi),例如查詢(xún)同時(shí)返回用戶(hù)基本信息和用戶(hù)擴(kuò)展信息。
使用 <association>
元素來(lái)指定關(guān)聯(lián)映射,它可以映射一個(gè)關(guān)聯(lián)查詢(xún)語(yǔ)句的結(jié)果,也可以映射兩個(gè)查詢(xún)語(yǔ)句結(jié)合返回復(fù)雜的類(lèi)型。
其中的 property
對(duì)應(yīng)主表類(lèi)型的屬性,column
對(duì)應(yīng)主表關(guān)聯(lián)字段,javaType
對(duì)應(yīng)關(guān)聯(lián)表類(lèi)型,select
對(duì)應(yīng)另一個(gè)查詢(xún)語(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ì)查詢(xún)返回的結(jié)果映射成一對(duì)多關(guān)系的嵌套類(lèi),例如查詢(xún)同時(shí)返回用戶(hù)信息和用戶(hù)的地址信息。
使用 <collection>
元素來(lái)指定集合映射,它可以映射一個(gè)關(guān)聯(lián)查詢(xún)語(yǔ)句的結(jié)果,也可以映射兩個(gè)查詢(xún)語(yǔ)句結(jié)合返回復(fù)雜的類(lèi)型。
它的用法與關(guān)聯(lián)查詢(xún) <association>
類(lèi)似,只不過(guò)指定關(guān)聯(lián)表類(lèi)型需要使用 ofType
屬性,為了區(qū)分集合存儲(chǔ)的類(lèi)型。
<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í)指定集合類(lèi)型和 Java 類(lèi)型:
<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 類(lèi)屬性名稱(chēng)會(huì)忽略大小寫(xiě)映射,例如 ID
列會(huì)和 id
屬性進(jìn)行映射。
通常數(shù)據(jù)庫(kù)列名使用下劃線(xiàn)規(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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot?使用AOP?+?Redis?防止表單重復(fù)提交的方法
Spring?Boot是一個(gè)用于構(gòu)建Web應(yīng)用程序的框架,通過(guò)AOP可以實(shí)現(xiàn)防止表單重復(fù)提交,本文介紹了在Spring?Boot應(yīng)用程序中使用AOP和Redis來(lái)防止表單重復(fù)提交的方法,需要的朋友可以參考下2023-04-04mybatis查詢(xún)字段為null設(shè)置為0的操作
這篇文章主要介紹了mybatis查詢(xún)字段為null設(shè)置為0的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02java簡(jiǎn)單實(shí)現(xiàn)斗地主發(fā)牌功能
這篇文章主要為大家詳細(xì)介紹了java簡(jiǎn)單實(shí)現(xiàn)斗地主發(fā)牌功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06java 動(dòng)態(tài)加載的實(shí)現(xiàn)代碼
這篇文章主要介紹了java 動(dòng)態(tài)加載的實(shí)現(xiàn)代碼的相關(guān)資料,Java動(dòng)態(tài)加載類(lèi)主要是為了不改變主程序代碼,通過(guò)修改配置文件就可以操作不同的對(duì)象執(zhí)行不同的功能,需要的朋友可以參考下2017-07-07在SSM中配置了事務(wù)控制但沒(méi)生效的問(wèn)題
這篇文章主要介紹了在SSM中配置了事務(wù)控制但沒(méi)生效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java的Struts框架中配置國(guó)際化的資源存儲(chǔ)的要點(diǎn)解析
這篇文章主要介紹了Java的Struts框架中配置國(guó)際化的資源存儲(chǔ)的要點(diǎn)解析,針對(duì)用戶(hù)所使用的語(yǔ)言來(lái)配置資源文件,需要的朋友可以參考下2016-04-04JAVA中堆、棧,靜態(tài)方法和非靜態(tài)方法的速度問(wèn)題
這篇文章主要介紹了JAVA中堆、棧,靜態(tài)方法和非靜態(tài)方法的速度問(wèn)題,堆和棧得速度性能分析多角度給大家分析,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08解決Java Calendar類(lèi)set()方法的陷阱
這篇文章主要介紹了解決Java Calendar類(lèi)set()方法的陷阱,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03