最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java?Mybatis使用resultMap時(shí),屬性賦值順序錯(cuò)誤的巨坑

 更新時(shí)間:2022年01月20日 09:49:25   作者:百事可樂_  
這篇文章主要介紹了Java?Mybatis使用resultMap時(shí),屬性賦值順序錯(cuò)誤的巨坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Mybatis使用resultMap屬性賦值順序錯(cuò)誤

今天發(fā)現(xiàn)個(gè)坑,新建的表使用生成工具生成的mapper文件和實(shí)體類后,發(fā)現(xiàn)少了個(gè)字段就又手動(dòng)加了下,結(jié)果發(fā)現(xiàn)一個(gè)問題

ids是后加入的字段 

@Data
@Builder
public class QueryRecordPo {
?
? ? ?//若干其他屬性....
? ? private String outputField;
? ? ?//后加的
? ? private String ids;
?
? ? //若干其他屬性
? ? //...?
}

resultMap中是這樣寫的

? ? <resultMap id="BaseResultMap" type="....">
? ? ? ? <id column="id" jdbcType="BIGINT" property="id"/>
? ? ? ? ..若干其他屬性
? ? ? ? <result column="ids" jdbcType="VARCHAR" property="ids"/>
? ? ? ? <result column="output_field" jdbcType="VARCHAR" property="outputField"/>
? ? ? ? ..若干其他屬性
? ? </resultMap>

可以發(fā)現(xiàn)ids加的位置是不一樣的,實(shí)體類中在outputField屬性下面,但resultMap中在其上面。然后測(cè)試數(shù)據(jù)中ids字段為null,查詢出來時(shí)卻發(fā)現(xiàn)ids的值和outputField的值是一樣的。但如果ids的字段有值,就可以正確賦值。

mybatis在生成目標(biāo)類進(jìn)行映射時(shí),會(huì)先檢查構(gòu)造函數(shù)聲明情況,但 如果Data注解和Builder注解一塊使用的話就只會(huì)生成全屬性參數(shù)構(gòu)造函數(shù),不會(huì)有默認(rèn)無參構(gòu)造函數(shù)。全屬性構(gòu)造函數(shù)的參數(shù)順序是和類中屬性聲明順序一致的

在把數(shù)據(jù)庫(kù)字段映射到實(shí)體類的時(shí)候發(fā)現(xiàn)實(shí)體類沒有默認(rèn)無參構(gòu)造函數(shù),就會(huì)把數(shù)據(jù)庫(kù)中的字段按照全屬性構(gòu)造函數(shù)參數(shù)的順序依次賦值給實(shí)體類的屬性。但如果實(shí)體類的屬性定義順序與數(shù)據(jù)庫(kù)中字段順序不一致,就會(huì)出現(xiàn)賦值錯(cuò)誤的情況。

然后再為outputField字段賦值時(shí)調(diào)用了set方法 這樣就出現(xiàn)了兩個(gè)不同名但同值的屬性。

解決辦法

1 修改屬性順序保持一致

2 為實(shí)體類加上@NoArgsConstructor和 @AllArgsConstructor注解 使其可以生成無參數(shù)構(gòu)造函數(shù)即可

之前生成時(shí) 順序都保持了一致,還真沒發(fā)現(xiàn)這個(gè)問題

Mybatis使用resultMap時(shí)需注意

    <resultMap id="baseMap" type="com.ei.medical.modules.model.EduEducationKnowledge">
        <id column="id" property="id"/>
        <result column="visitNumber" property="visitNumber"/>
        <result column="patientName" property="patientName"/>
        <result column="sendTime" property="sendTime"/>
        <result column="wardCode" property="wardCode"/>
        <result column="wardName" property="wardName"/>
        <result column="categoryCode" property="categoryCode"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>
        <result column="cover" property="cover"/>
    </resultMap>
    
    <select id="getAllBy" resultMap="baseMap">
        SELECT
            eer.visit_number as visitNumber,
            eer.patient_name as patientName,
            eer.send_time as sendTime,
            eek.id as id,
            eek.ward_code as wardCode,
            eek.ward_name as wardName,
            eek.category_code as categoryCode,
            eek.title as title,
            eek.content as content,
            eek.cover as cover
        FROM
            edu_education_record AS eer,
            edu_education_knowledge AS eek
        WHERE
            eer.education_knowledge_id=eek.id
    </select>

如果是實(shí)體中是直接引用別的對(duì)象的具體參數(shù)字段,直接用原始方式就行

如果是實(shí)體中是list集合

    <resultMap id="baseMap" type="com.ei.medical.modules.model.EduEducationKnowledge">
    	<id column="id" property="id"/>
        <result column="visitNumber" property="visitNumber"/>
        <result column="patientName" property="patientName"/>
        <result column="sendTime" property="sendTime"/>
        <result column="wardCode" property="wardCode"/>
        <result column="wardName" property="wardName"/>
        <result column="categoryCode" property="categoryCode"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>
        <result column="cover" property="cover"/>
        
        <collection property="pic" ofType="string">
            <result column="pic"/>
        </collection>
    </resultMap>
    
    <select id="getAllBy" resultMap="baseMap">
        SELECT
            eer.visit_number as visitNumber,
            eer.patient_name as patientName,
            eer.send_time as sendTime,
            eek.id as id,
            eek.ward_code as wardCode,
            eek.ward_name as wardName,
            eek.category_code as categoryCode,
            eek.title as title,
            eek.content as content,
            eek.cover as cover
        FROM
            edu_education_record AS eer,
            edu_education_knowledge AS eek
        WHERE
            eer.education_knowledge_id=eek.id
    </select>

如果實(shí)體中引用的是別的對(duì)象,可以使用association 標(biāo)簽來寫

    <resultMap id="baseMap" type="com.ei.medical.modules.model.EduEducationKnowledge">
        <id column="id" property="id"/>
        <result column="wardCode" property="wardCode"/>
        <result column="wardName" property="wardName"/>
        <result column="categoryCode" property="categoryCode"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>
        <result column="cover" property="cover"/>
        
        <association property="eduEducationRecord" javaType="com.ei.medical.modules.model.EduEducationRecord">
	        <result column="visitNumber" property="visitNumber"/>
	        <result column="patientName" property="patientName"/>
	        <result column="sendTime" property="sendTime"/>
        </association>
    </resultMap>
    
    <select id="getAllBy" resultMap="baseMap">
        SELECT
            eer.visit_number as visitNumber,
            eer.patient_name as patientName,
            eer.send_time as sendTime,
            eek.id as id,
            eek.ward_code as wardCode,
            eek.ward_name as wardName,
            eek.category_code as categoryCode,
            eek.title as title,
            eek.content as content,
            eek.cover as cover
        FROM
            edu_education_record AS eer,
            edu_education_knowledge AS eek
        WHERE
            eer.education_knowledge_id=eek.id
    </select>

如果實(shí)體中是引用的別的對(duì)象的list集合,應(yīng)該使用collection 標(biāo)簽

<resultMap id="baseMap" type="com.ei.medical.modules.model.EduEducationKnowledge">
        <id column="id" property="id"/>
        <result column="wardCode" property="wardCode"/>
        <result column="wardName" property="wardName"/>
        <result column="categoryCode" property="categoryCode"/>
        <result column="title" property="title"/>
        <result column="content" property="content"/>
        <result column="cover" property="cover"/>
        
        <collection property="eduEducationRecordList" ofType="com.ei.medical.modules.model.EduEducationRecord">
            <result column="visitNumber" property="visitNumber"/>
            <result column="patientName" property="patientName"/>
            <result column="sendTime" property="sendTime"/>
        </collection>
    </resultMap>
    <select id="getAllBy" resultMap="baseMap">
        SELECT
            eer.visit_number as visitNumber,
            eer.patient_name as patientName,
            eer.send_time as sendTime,
            eek.id as id,
            eek.ward_code as wardCode,
            eek.ward_name as wardName,
            eek.category_code as categoryCode,
            eek.title as title,
            eek.content as content,
            eek.cover as cover
        FROM
            edu_education_record AS eer,
            edu_education_knowledge AS eek
        WHERE
            eer.education_knowledge_id=eek.id
    </select>

tips:

使用resultMap的時(shí)候,應(yīng)該直接用as后面的字段名,即自己命的名字

如果沒有使用as的話,直接使用數(shù)據(jù)庫(kù)中原本的名字

resultMap中各個(gè)標(biāo)簽的含義

在這里插入圖片描述

tips:

在一個(gè) resultMap 元素中,這些子元素出現(xiàn)的先后順序是有嚴(yán)格規(guī)定的,它們從前到后依次是:constructor–>id --> result–> association–>collection -->discriminator, 不然就會(huì)報(bào)錯(cuò)。

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

您可能感興趣的文章:

相關(guān)文章

  • Java Web 簡(jiǎn)單的分頁(yè)顯示實(shí)例代碼

    Java Web 簡(jiǎn)單的分頁(yè)顯示實(shí)例代碼

    這篇文章主要介紹了Java Web 簡(jiǎn)單的分頁(yè)顯示實(shí)例代碼的相關(guān)資料,本文通過,計(jì)算總的頁(yè)數(shù)和查詢指定頁(yè)數(shù)據(jù)兩個(gè)方法實(shí)現(xiàn)分頁(yè)效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • javaweb實(shí)現(xiàn)簡(jiǎn)易郵件發(fā)送

    javaweb實(shí)現(xiàn)簡(jiǎn)易郵件發(fā)送

    這篇文章主要為大家詳細(xì)介紹了javaweb實(shí)現(xiàn)簡(jiǎn)易郵件發(fā)送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • JAVA實(shí)現(xiàn)簡(jiǎn)單系統(tǒng)登陸注冊(cè)模塊

    JAVA實(shí)現(xiàn)簡(jiǎn)單系統(tǒng)登陸注冊(cè)模塊

    這篇文章主要介紹了一個(gè)簡(jiǎn)單完整的登陸注冊(cè)模塊的實(shí)現(xiàn)過程,文章條理清晰,在實(shí)現(xiàn)過程中加深了對(duì)相關(guān)概念的理解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2015-07-07
  • Java微信公眾號(hào)開發(fā)之通過微信公眾號(hào)獲取用戶信息

    Java微信公眾號(hào)開發(fā)之通過微信公眾號(hào)獲取用戶信息

    這篇文章主要介紹了Java微信公眾號(hào)開發(fā)之通過微信公眾號(hào)獲取用戶信息,需要的朋友可以參考下
    2017-05-05
  • Java中private關(guān)鍵字詳細(xì)用法實(shí)例以及解釋

    Java中private關(guān)鍵字詳細(xì)用法實(shí)例以及解釋

    這篇文章主要給大家介紹了關(guān)于Java中private關(guān)鍵字詳細(xì)用法實(shí)例以及解釋的相關(guān)資料,在Java中private是一種訪問修飾符,它可以用來控制類成員的訪問權(quán)限,文中將用法介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • 詳解如何在Spring?Security中自定義權(quán)限表達(dá)式

    詳解如何在Spring?Security中自定義權(quán)限表達(dá)式

    這篇文章主要和大家詳細(xì)介紹一下如何在Spring?Security中自定義權(quán)限表達(dá)式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-07-07
  • Java如何有效避免SQL注入漏洞的方法總結(jié)

    Java如何有效避免SQL注入漏洞的方法總結(jié)

    SQL注入是比較常見的網(wǎng)絡(luò)攻擊方式之一,它不是利用操作系統(tǒng)的BUG來實(shí)現(xiàn)攻擊,而是針對(duì)程序員編程時(shí)的疏忽,通過SQL語句,實(shí)現(xiàn)無帳號(hào)登錄,甚至篡改數(shù)據(jù)庫(kù),這篇文章主要給大家介紹了關(guān)于Java如何避免SQL注入漏洞的兩種方法,需要的朋友可以參考下
    2022-01-01
  • SpringCloud遠(yuǎn)程服務(wù)調(diào)用實(shí)戰(zhàn)筆記

    SpringCloud遠(yuǎn)程服務(wù)調(diào)用實(shí)戰(zhàn)筆記

    本文給大家介紹SpringCloud遠(yuǎn)程服務(wù)調(diào)用實(shí)戰(zhàn)筆記,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-11-11
  • SpringCloud使用logback日志框架教程詳解

    SpringCloud使用logback日志框架教程詳解

    Logback是一個(gè)功能強(qiáng)大的日志框架,它是一個(gè)基于slf4j的日志系統(tǒng),提供了可靠的日志服務(wù),比log4j更快,更靈活,更容易使用。本文將教會(huì)你快速讓你的項(xiàng)目集成logback日志框架,需要的朋友可以參考下
    2023-05-05
  • Java正則表達(dá)式實(shí)現(xiàn)在文本中匹配查找換行符的方法【經(jīng)典實(shí)例】

    Java正則表達(dá)式實(shí)現(xiàn)在文本中匹配查找換行符的方法【經(jīng)典實(shí)例】

    這篇文章主要介紹了Java正則表達(dá)式實(shí)現(xiàn)在文本中匹配查找換行符的方法,結(jié)合具體實(shí)例分析了java正則匹配查找換行符的實(shí)現(xiàn)技巧與匹配模式相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2017-04-04

最新評(píng)論

安塞县| 五寨县| 怀安县| 武汉市| 孙吴县| 阿荣旗| 卢氏县| 洛隆县| 乐安县| 临泽县| 津南区| 甘德县| 福清市| 榕江县| 景谷| 郎溪县| 达日县| 金沙县| 崇礼县| 贵德县| 前郭尔| 阿勒泰市| 元氏县| 海淀区| 松原市| 伊春市| 泸溪县| 新蔡县| 横峰县| 永新县| 运城市| 乌什县| 遵义县| 巴塘县| 江北区| 正安县| 时尚| 镇平县| 麻江县| 辉南县| 晋城|