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í)體類(lèi)
表
創(chuàng)建三個(gè)表 group member score
score 與 member 一對(duì)一,通過(guò) score.id 關(guān)聯(lián)
group 與 member 一對(duì)多,通過(guò) 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í)體類(lèi)
創(chuàng)建三個(gè)實(shí)體類(lèi) Group Member Score
Score 類(lèi)的對(duì)象是 Member 類(lèi)的成員變量
Member 類(lèi)的對(duì)象集合是 Group 類(lèi)的成員變量
/** 成績(jī)類(lèi) */
public class Score {
? ? private Integer id;
? ? /** 數(shù)學(xué)成績(jī) */
? ? private Float math;
? ? /** 歷史成績(jī) */
? ? private Float hitory;
? ? ...getter And setter...
}
/** 成員類(lèi) */
public class Member {
? ? private Integer id;
? ? /** 姓名 */
? ? private String name;
? ? /** 分?jǐn)?shù)對(duì)象 */
? ? private Score score;
? ? ...getter And setter...
}
/** 組類(lèi) */
public class Group {
? ? private Integer id;
? ? /** 組名 */
? ? private String groupName;
? ? /** 成員 */
? ? private List<Member> members;
? ? ...getter And setter...
}定義與表映射的 resultMap
在 BeanMapper.xml 定義最基本的與數(shù)據(jù)庫(kù)表字段映射的 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í)體類(lèi)映射 --> ? ? <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í)體類(lèi)映射 --> ? ? <resultMap id="memberMap" type="com.example.Member"> ? ? ? ? <id column="id" jdbcType="INTEGER" property="id" /> ? ? ? ? <result column="name" jdbcType="VARCHAR" property="name" /> ? ? </resultMap> ? ? <!-- Group實(shí)體類(lèi)映射 --> ? ? <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類(lèi)型collection: 復(fù)用現(xiàn)存的 resultMap,適用于對(duì)應(yīng)的屬性為JavaBean集合時(shí),使用 ofType 指定Java類(lèi)型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è)類(lèi)寫(xiě)在一個(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的類(lèi)型
? ? ? ? ? ? ? ? ? ? ? ? 跨文件引用需指定namespace -->
? ? ? ? <!-- columnPrefix: 只從 score_ 開(kāi)頭的字段為當(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的類(lèi)型
? ? ? ? ? ? ? ? ? ? ? ? 同文件引用無(wú)需指定namespace -->
? ? ? ? <!-- columnPrefix: 只從 member_ 開(kāi)頭的字段為當(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的編寫(xiě) ,在mybatis里面進(jìn)行復(fù)查和復(fù)用的時(shí)候一定要去看所對(duì)應(yīng)的有沒(méi)有這個(gè)類(lèi) ,今天弄了幾個(gè)dto,還有時(shí)間戳的轉(zhuǎn)換,java里面的時(shí)間戳是以毫秒來(lái)進(jìn)行計(jì)算的。
所以說(shuō)在專(zhuān)用mysql的時(shí)候要注意
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解SpringBoot通過(guò)restTemplate實(shí)現(xiàn)消費(fèi)服務(wù)
本篇文章主要介紹了詳解使用RestTemplate消費(fèi)spring boot的Restful服務(wù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
在Windows系統(tǒng)下安裝Thrift的方法與使用講解
今天小編就為大家分享一篇關(guān)于在Windows系統(tǒng)下安裝Thrift的方法與使用講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Java之ThreadPoolExecutor類(lèi)詳解
這篇文章主要介紹了Java之ThreadPoolExecutor類(lèi)詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Springmvc文件上傳實(shí)現(xiàn)流程解析
這篇文章主要介紹了Springmvc文件上傳實(shí)現(xiàn)流程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Springboot如何實(shí)現(xiàn)Web系統(tǒng)License授權(quán)認(rèn)證
這篇文章主要介紹了Springboot如何實(shí)現(xiàn)Web系統(tǒng)License授權(quán)認(rèn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
解析web.xml中在Servlet中獲取context-param和init-param內(nèi)的參數(shù)
本篇文章是對(duì)web.xml中在Servlet中獲取context-param和init-param內(nèi)的參數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07

