Mybatis?resultMap標(biāo)簽繼承、復(fù)用、嵌套方式
resultMap標(biāo)簽繼承、復(fù)用、嵌套
記錄演示 Mybatis 中 resultMap 標(biāo)簽繼承、復(fù)用(包括跨文件)以及多層嵌套的使用方法,
- 繼承: 繼承已存在的 resultMap 標(biāo)簽進(jìn)行擴(kuò)展
- 復(fù)用: 跨mapper文件引用現(xiàn)存的 resultMap 標(biāo)簽
- 嵌套: 多層嵌套的JavaBean與 resultMap 映射方法
定義表與實(shí)體類
表
創(chuàng)建三個(gè)表 group member score
score 與 member 一對(duì)一,通過 score.id 關(guān)聯(lián)
group 與 member 一對(duì)多,通過 group.id 關(guān)聯(lián)
create table `score` ( ? ? `id` int comment '主鍵', ? ? `math` float comment '數(shù)學(xué)成績(jī)', ? ? `history` float comment '歷史成績(jī)', ? ? primary key (`id`) ) create table `member` ( ? ? `id` int comment '主鍵', ? ? `name` varchar comment '姓名', ? ? `group_id` int comment '所屬組group表id', ? ? `score_id` int comment '成績(jī)Score表id', ? ? primary key (`id`) ) create table `group` ( ? ? `id` int comment '主鍵', ? ? `name` varchar comment '組名', ? ? primary key (`id`) )
實(shí)體類
創(chuàng)建三個(gè)實(shí)體類 Group Member Score
Score 類的對(duì)象是 Member 類的成員變量
Member 類的對(duì)象集合是 Group 類的成員變量
/** 成績(jī)類 */
public class Score {
? ? private Integer id;
? ? /** 數(shù)學(xué)成績(jī) */
? ? private Float math;
? ? /** 歷史成績(jī) */
? ? private Float hitory;
? ? ...getter And setter...
}
/** 成員類 */
public class Member {
? ? private Integer id;
? ? /** 姓名 */
? ? private String name;
? ? /** 分?jǐn)?shù)對(duì)象 */
? ? private Score score;
? ? ...getter And setter...
}
/** 組類 */
public class Group {
? ? private Integer id;
? ? /** 組名 */
? ? private String groupName;
? ? /** 成員 */
? ? private List<Member> members;
? ? ...getter And setter...
}定義與表映射的 resultMap
在 BeanMapper.xml 定義最基本的與數(shù)據(jù)庫表字段映射的 resultMap 標(biāo)簽
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.BeanMapper"> ? ? <!-- Score實(shí)體類映射 --> ? ? <resultMap id="scoreMap" type="com.example.Score"> ? ? ? ? <id column="id" jdbcType="INTEGER" property="id" /> ? ? ? ? <result column="math" jdbcType="FLOAT" property="math" /> ? ? ? ? <result column="history" jdbcType="FLOAT" property="history" /> ? ? </resultMap> ? ? <!-- Member實(shí)體類映射 --> ? ? <resultMap id="memberMap" type="com.example.Member"> ? ? ? ? <id column="id" jdbcType="INTEGER" property="id" /> ? ? ? ? <result column="name" jdbcType="VARCHAR" property="name" /> ? ? </resultMap> ? ? <!-- Group實(shí)體類映射 --> ? ? <resultMap id="groupMap" type="com.example.Group"> ? ? ? ? <id column="id" jdbcType="INTEGER" property="id" /> ? ? ? ? <result column="name" jdbcType="VARCHAR" property="groupName" /> ? ? </resultMap> </mapper>
繼承、復(fù)用、嵌套
創(chuàng)建 DemoMapper.xml,演示標(biāo)簽的繼承、復(fù)用、嵌套
復(fù)用現(xiàn)存標(biāo)簽時(shí)若位于相同mapper文件可直接使用 resultMap 的 id 屬性引用,跨文件時(shí)需要指定 namespace 屬性才可正常引用
extends: 繼承,可繼承其他 resultMap 并加以擴(kuò)展association: 復(fù)用現(xiàn)存的 resultMap,適用于對(duì)應(yīng)的屬性為單JavaBean時(shí),使用 javaType 指定Java類型collection: 復(fù)用現(xiàn)存的 resultMap,適用于對(duì)應(yīng)的屬性為JavaBean集合時(shí),使用 ofType 指定Java類型columnPrefix: 只將該屬性指定前綴的屬性賦值給當(dāng)前 resultMap,存在多層嵌套時(shí)每進(jìn)入一層就會(huì)將本層前綴截取掉。
如下面的mapper文件中,外層的 fullMemberMap 前綴為 member_,經(jīng)本次篩選 member_score_id -> score_id,
內(nèi)層的 scoreMap 前綴為 score_,經(jīng)本次篩選 score_id -> id,最終被賦值給 Score.id
所以只有形如 member_score_id 的字段才會(huì)最終進(jìn)入 scoreMap 的取值范圍中
若是不復(fù)用只是單純嵌套,則可以直接將三個(gè)類寫在一個(gè) resultMap 標(biāo)簽內(nèi)實(shí)現(xiàn)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.DemoMapper">
? ? <!-- extends: 繼承 -->
? ? <resultMap id="fullMemberMap" extends="com.example.BeanMapper.memberMap" type="com.example.Member">
? ? ? ? <!-- association: 復(fù)用已存在的resultMap,單JavaBean屬性時(shí)使用
? ? ? ? ? ? ? ? ? ? ? ? 使用javaType屬性指定JavaBean的類型
? ? ? ? ? ? ? ? ? ? ? ? 跨文件引用需指定namespace -->
? ? ? ? <!-- columnPrefix: 只從 score_ 開頭的字段為當(dāng)前resultMap取值 -->
? ? ? ? <association ?property="score" resultMap="com.example.BeanMapper.scoreMap" javaType="com.example.Score" columnPrefix="score_" />
? ? </resultMap>
? ??
? ? <resultMap id="fullGroupMap" extends="com.example.BeanMapper.groupMap" type="com.example.Group">
? ? ? ? <!-- collection: 復(fù)用已存在的resultMap,JavaBean集合屬性時(shí)使用
? ? ? ? ? ? ? ? ? ? ? ? 使用ofType屬性指定JavaBean的類型
? ? ? ? ? ? ? ? ? ? ? ? 同文件引用無需指定namespace -->
? ? ? ? <!-- columnPrefix: 只從 member_ 開頭的字段為當(dāng)前resultMap取值
? ? ? ? ? ? ? ? ? ? ? ? 進(jìn)入fullMemberMap內(nèi)嵌套的scoreMap時(shí)前綴 member_ 會(huì)被去除,即 member_score_id 字段才能被scoreMap正確接收 -->
? ? ? ? <collection property="members" ofType="com.example.Member" resultMap="fullMemberMap" columnPrefix="member_"/>
? ? </resultMap>
? ? <!-- 直接引用最終的resultMap,并根據(jù)columnPrefix屬性設(shè)置的前綴為各個(gè)字段指定不同的別名 -->
? ? <select id="selectGroupById" parameterType="java.lang.Integer" resultMap="fullGroupMap">
? ? ? ? select g.id, g.name,
? ? ? ? ? ? ? ?m.id member_id, m.name member_name,
? ? ? ? ? ? ? ?s.id member_score_id, s.math member_score_math, s.history member_score_history
? ? ? ? ? from `group` g
? ? ? ? ? ? left join `member` m on m.group_id = g.id
? ? ? ? ? ? left join `score` s on s.id = m.score_id
? ? ? ? ? where g.id = #{id,jdbcType=INTEGER}
? ? </select>
</mapper>使用resultMap需要注意的地方
今天主要還是根據(jù)需求在進(jìn)行sql的編寫 ,在mybatis里面進(jìn)行復(fù)查和復(fù)用的時(shí)候一定要去看所對(duì)應(yīng)的有沒有這個(gè)類 ,今天弄了幾個(gè)dto,還有時(shí)間戳的轉(zhuǎn)換,java里面的時(shí)間戳是以毫秒來進(jìn)行計(jì)算的。
所以說在專用mysql的時(shí)候要注意
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中計(jì)算集合中元素的出現(xiàn)次數(shù)統(tǒng)計(jì)
本文主要介紹了Java中計(jì)算集合中元素的出現(xiàn)次數(shù)統(tǒng)計(jì),使用Collections類配合HashMap來統(tǒng)計(jì)和java lamb 計(jì)算這兩種方式,具有一定的參考價(jià)值,感興趣可以了解一下2024-02-02
十分鐘速懂java知識(shí)點(diǎn) System類
這篇文章主要介紹了java知識(shí)點(diǎn)System類,根據(jù)一次面試總結(jié)的,可以十分鐘速懂System類,感興趣的小伙伴們可以參考一下2015-12-12
Java優(yōu)先隊(duì)列(PriorityQueue)重寫compare操作
這篇文章主要介紹了Java優(yōu)先隊(duì)列(PriorityQueue)重寫compare操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
springboot+redis實(shí)現(xiàn)簡(jiǎn)單的熱搜功能
這篇文章主要介紹了springboot+redis實(shí)現(xiàn)一個(gè)簡(jiǎn)單的熱搜功能,通過代碼介紹了過濾不雅文字的過濾器,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05

