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

Java?Mybatis使用resultMap時,屬性賦值順序錯誤的巨坑

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

Mybatis使用resultMap屬性賦值順序錯誤

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

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

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

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

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

解決辦法

1 修改屬性順序保持一致

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

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

Mybatis使用resultMap時需注意

    <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ù)字段,直接用原始方式就行

如果是實體中是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>

如果實體中引用的是別的對象,可以使用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>

如果實體中是引用的別的對象的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的時候,應(yīng)該直接用as后面的字段名,即自己命的名字

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

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

在這里插入圖片描述

tips:

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

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

您可能感興趣的文章:

相關(guān)文章

  • Java Web 簡單的分頁顯示實例代碼

    Java Web 簡單的分頁顯示實例代碼

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

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

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

    JAVA實現(xiàn)簡單系統(tǒng)登陸注冊模塊

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

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

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

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

    這篇文章主要給大家介紹了關(guān)于Java中private關(guān)鍵字詳細(xì)用法實例以及解釋的相關(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來實現(xiàn)攻擊,而是針對程序員編程時的疏忽,通過SQL語句,實現(xiàn)無帳號登錄,甚至篡改數(shù)據(jù)庫,這篇文章主要給大家介紹了關(guān)于Java如何避免SQL注入漏洞的兩種方法,需要的朋友可以參考下
    2022-01-01
  • SpringCloud遠(yuǎn)程服務(wù)調(diào)用實戰(zhàn)筆記

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

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

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

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

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

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

最新評論

丰宁| 上杭县| 句容市| 东乌珠穆沁旗| 民县| 隆回县| 海兴县| 明溪县| 灵武市| 古蔺县| 贺兰县| 耿马| 呼和浩特市| 陆丰市| 永胜县| 临洮县| 德化县| 社旗县| 仙桃市| 图木舒克市| 建平县| 韶关市| 舒兰市| 南宁市| 武鸣县| 苏尼特左旗| 宁都县| 吉安县| 墨脱县| 汉寿县| 和静县| 浏阳市| 观塘区| 娱乐| 汶川县| 淮南市| 湟源县| 蓝山县| 疏附县| 平顺县| 农安县|