Java中MyBatis的結(jié)果映射詳解
一、 概述
Java 數(shù)據(jù)持久層實現(xiàn)了應(yīng)用程序與數(shù)據(jù)源的交互,大多數(shù)時候需要使用到各種查詢語句。
MyBatis 支持對各種單表查詢、關(guān)聯(lián)查詢等各種復(fù)雜查詢的結(jié)果進行映射。
二、結(jié)果映射
resultMap 元素是 MyBatis 中最重要最強大的元素,大部分查詢語句返回的結(jié)果,都能通過簡單的配置來返回映射的 Java 對象。
假設(shè)存在這樣一個實體模型,一個用戶對應(yīng)有一個基本信息和擴展信息,對應(yīng)有多個地址。
簡單講,用戶基本信息與用戶擴展信息是一對一的關(guān)系,與用戶地址是一對多的關(guān)系。
創(chuàng)建實體類和數(shù)據(jù)訪問接口如下:
// 用戶基本信息實體 @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; } // 用戶擴展信息實體 @Data public class UserExt { private Long id; private Long userId; private String phone; private String email; } // 用戶地址信息實體 @Data public class Address { private Long id; private Long userId; private String address; } // 用戶相關(guān)接口 public interface UserExtMapper { // 查詢用戶基本信息 User selectById(Long id); // 查詢用戶基本信息和擴展信息(一對一) User selectExt(Long id); // 查詢用戶基本信息和地址信息(一對多) User selectAddresses(Long id); }
1. 基本映射
基本映射是對查詢返回的結(jié)果映射成一個最簡單的 JavaBean 類,通過類屬性對應(yīng)數(shù)據(jù)庫字段進行映射。
<id>
和 <result>
元素都將列映射到一個簡單的數(shù)據(jù)類型(String, int, double
等)的屬性或字段。
其中的 property
對應(yīng) Java 類 屬性名稱,column
對應(yīng)數(shù)據(jù)庫列名稱。
<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)映射是對查詢返回的結(jié)果映射成一對一關(guān)系的嵌套類,例如查詢同時返回用戶基本信息和用戶擴展信息。
使用 <association>
元素來指定關(guān)聯(lián)映射,它可以映射一個關(guān)聯(lián)查詢語句的結(jié)果,也可以映射兩個查詢語句結(jié)合返回復(fù)雜的類型。
其中的 property
對應(yīng)主表類型的屬性,column
對應(yīng)主表關(guān)聯(lián)字段,javaType
對應(yīng)關(guān)聯(lián)表類型,select
對應(yīng)另一個查詢語句。
如果映射一個關(guān)聯(lián)語句的結(jié)果,則不會使用 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. 集合映射
集合映射是對查詢返回的結(jié)果映射成一對多關(guān)系的嵌套類,例如查詢同時返回用戶信息和用戶的地址信息。
使用 <collection>
元素來指定集合映射,它可以映射一個關(guān)聯(lián)查詢語句的結(jié)果,也可以映射兩個查詢語句結(jié)合返回復(fù)雜的類型。
它的用法與關(guān)聯(lián)查詢 <association>
類似,只不過指定關(guān)聯(lián)表類型需要使用 ofType
屬性,為了區(qū)分集合存儲的類型。
<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>
同時指定集合類型和 Java 類型:
<collection property="addresses" javaType="ArrayList" column="id" ofType="Address" select="selectAddressById"/>
4. 自動映射
MyBatis 支持在簡單的場景下,可以自動映射結(jié)果,在復(fù)雜的場景下,只需描述語句之間的關(guān)系就行。
當配置自動映射結(jié)果時,數(shù)據(jù)庫列名與 Java 類屬性名稱會忽略大小寫映射,例如 ID
列會和 id
屬性進行映射。
通常數(shù)據(jù)庫列名使用下劃線規(guī)范,而 Java 屬性遵循駝峰命名,將 mapUnderscoreToCamelCase
設(shè)置為 true
時,會啟用自動映射。
例如前面的基本映射、關(guān)聯(liá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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot?使用AOP?+?Redis?防止表單重復(fù)提交的方法
Spring?Boot是一個用于構(gòu)建Web應(yīng)用程序的框架,通過AOP可以實現(xiàn)防止表單重復(fù)提交,本文介紹了在Spring?Boot應(yīng)用程序中使用AOP和Redis來防止表單重復(fù)提交的方法,需要的朋友可以參考下2023-04-04JAVA中堆、棧,靜態(tài)方法和非靜態(tài)方法的速度問題
這篇文章主要介紹了JAVA中堆、棧,靜態(tài)方法和非靜態(tài)方法的速度問題,堆和棧得速度性能分析多角度給大家分析,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08