欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis?resultMap標(biāo)簽繼承、復(fù)用、嵌套方式

 更新時間:2022年03月10日 14:50:09   作者:王二小丷  
這篇文章主要介紹了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)建三個表 group member score

score 與 member 一對一,通過 score.id 關(guān)聯(lián)

group 與 member 一對多,通過 group.id 關(guān)聯(lián)

create table `score` (
? ? `id` int comment '主鍵',
? ? `math` float comment '數(shù)學(xué)成績',
? ? `history` float comment '歷史成績',
? ? primary key (`id`)
)
create table `member` (
? ? `id` int comment '主鍵',
? ? `name` varchar comment '姓名',
? ? `group_id` int comment '所屬組group表id',
? ? `score_id` int comment '成績Score表id',
? ? primary key (`id`)
)
create table `group` (
? ? `id` int comment '主鍵',
? ? `name` varchar comment '組名',
? ? primary key (`id`)
)

實(shí)體類

創(chuàng)建三個實(shí)體類 Group Member Score

Score 類的對象是 Member 類的成員變量

Member 類的對象集合是 Group 類的成員變量

/** 成績類 */
public class Score {
? ? private Integer id;
? ? /** 數(shù)學(xué)成績 */
? ? private Float math;
? ? /** 歷史成績 */
? ? private Float hitory;
? ? ...getter And setter...
}
/** 成員類 */
public class Member {
? ? private Integer id;
? ? /** 姓名 */
? ? private String name;
? ? /** 分?jǐn)?shù)對象 */
? ? 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)簽時若位于相同mapper文件可直接使用 resultMap 的 id 屬性引用,跨文件時需要指定 namespace 屬性才可正常引用

  • extends: 繼承,可繼承其他 resultMap 并加以擴(kuò)展
  • association: 復(fù)用現(xiàn)存的 resultMap,適用于對應(yīng)的屬性為單JavaBean時,使用 javaType 指定Java類型
  • collection: 復(fù)用現(xiàn)存的 resultMap,適用于對應(yīng)的屬性為JavaBean集合時,使用 ofType 指定Java類型
  • columnPrefix: 只將該屬性指定前綴的屬性賦值給當(dāng)前 resultMap,存在多層嵌套時每進(jìn)入一層就會將本層前綴截取掉。

如下面的mapper文件中,外層的 fullMemberMap 前綴為 member_,經(jīng)本次篩選 member_score_id -> score_id,

內(nèi)層的 scoreMap 前綴為 score_,經(jīng)本次篩選 score_id -> id,最終被賦值給 Score.id

所以只有形如 member_score_id 的字段才會最終進(jìn)入 scoreMap 的取值范圍中

若是不復(fù)用只是單純嵌套,則可以直接將三個類寫在一個 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屬性時使用
? ? ? ? ? ? ? ? ? ? ? ? 使用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集合屬性時使用
? ? ? ? ? ? ? ? ? ? ? ? 使用ofType屬性指定JavaBean的類型
? ? ? ? ? ? ? ? ? ? ? ? 同文件引用無需指定namespace -->
? ? ? ? <!-- columnPrefix: 只從 member_ 開頭的字段為當(dāng)前resultMap取值
? ? ? ? ? ? ? ? ? ? ? ? 進(jìn)入fullMemberMap內(nèi)嵌套的scoreMap時前綴 member_ 會被去除,即 member_score_id 字段才能被scoreMap正確接收 -->
? ? ? ? <collection property="members" ofType="com.example.Member" resultMap="fullMemberMap" columnPrefix="member_"/>
? ? </resultMap>
? ? <!-- 直接引用最終的resultMap,并根據(jù)columnPrefix屬性設(shè)置的前綴為各個字段指定不同的別名 -->
? ? <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ù)用的時候一定要去看所對應(yīng)的有沒有這個類 ,今天弄了幾個dto,還有時間戳的轉(zhuǎn)換,java里面的時間戳是以毫秒來進(jìn)行計算的。

所以說在專用mysql的時候要注意

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java獲取日期的方法

    java獲取日期的方法

    這篇文章介紹了java獲取日期的方法,有需要的朋友可以參考一下
    2013-10-10
  • 深入分析Java內(nèi)存區(qū)域的使用詳解

    深入分析Java內(nèi)存區(qū)域的使用詳解

    本篇文章對Java內(nèi)存區(qū)域的使用進(jìn)行了詳細(xì)的介紹。需要的朋友參考下
    2013-05-05
  • Java mysql特殊形式的查詢語句詳解

    Java mysql特殊形式的查詢語句詳解

    這篇文章主要介紹了Java mysql特殊形式的查詢,包括子查詢和聯(lián)合查詢、自身連接查詢問題,本文通過sql語句給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • 詳解SpringBoot通過restTemplate實(shí)現(xiàn)消費(fèi)服務(wù)

    詳解SpringBoot通過restTemplate實(shí)現(xiàn)消費(fèi)服務(wù)

    本篇文章主要介紹了詳解使用RestTemplate消費(fèi)spring boot的Restful服務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 在Windows系統(tǒng)下安裝Thrift的方法與使用講解

    在Windows系統(tǒng)下安裝Thrift的方法與使用講解

    今天小編就為大家分享一篇關(guān)于在Windows系統(tǒng)下安裝Thrift的方法與使用講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Java之ThreadPoolExecutor類詳解

    Java之ThreadPoolExecutor類詳解

    這篇文章主要介紹了Java之ThreadPoolExecutor類詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • maven 打包時間戳問題

    maven 打包時間戳問題

    這篇文章主要介紹了maven 打包時間戳問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • Springmvc文件上傳實(shí)現(xiàn)流程解析

    Springmvc文件上傳實(shí)現(xiàn)流程解析

    這篇文章主要介紹了Springmvc文件上傳實(shí)現(xiàn)流程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    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)證

    這篇文章主要介紹了Springboot如何實(shí)現(xiàn)Web系統(tǒng)License授權(quán)認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • 解析web.xml中在Servlet中獲取context-param和init-param內(nèi)的參數(shù)

    解析web.xml中在Servlet中獲取context-param和init-param內(nèi)的參數(shù)

    本篇文章是對web.xml中在Servlet中獲取context-param和init-param內(nèi)的參數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-07-07

最新評論