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

mybatis resultmap 如何為對象賦值的調(diào)用順序

 更新時間:2022年01月28日 15:30:08   作者:qq_34922536  
這篇文章主要介紹了mybatis resultmap 如何為對象賦值的調(diào)用順序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

resultmap 為對象賦值的調(diào)用順序

寫了一個mybatis的mapper映射文件,

java bean定義如下

public class GroupCourseResult extends GroupResult {
    private String cid;
    private String cname;
    public GroupCourseResult(int stuschool, int nian, String stuclass, String cid, String cname) {
        super(stuschool, nian, stuclass);
        this.cid = cid;
        this.cname = cname;
    }
    public String getCid() {
        return cid;
    }
    public void setCid(String cid) {
        this.cid = cid;
    }
    public String getCname() {
        return cname;
    }
    public void setCname(String cname) {
        this.cname = cname;
    }
}

部分mybatis映射文件如下

<select id="selectFailedCourseRationByGroupIdAndCourseName" resultType="GroupCourseResult">
? ? ? ...
? </select>

實體類中的屬性名和查詢的列名完全匹配,但是沒有查詢stuclass,則封裝后的實體類中的stuclass屬性應(yīng)該為空。

然而程序運行后,stuclass屬性不僅不為空,還與cname完全相同,百思不得其解,故翻了翻mybatis的源碼。

在mybatis中的DefaultResultSetHandler類中,

createResultObject方法的代碼如下

  private Object createResultObject(ResultSetWrapper rsw, ResultMap resultMap, List<Class<?>> constructorArgTypes, List<Object> constructorArgs, String columnPrefix)
      throws SQLException {
    final Class<?> resultType = resultMap.getType();
    final MetaClass metaType = MetaClass.forClass(resultType, reflectorFactory);
    final List<ResultMapping> constructorMappings = resultMap.getConstructorResultMappings();
    if (hasTypeHandlerForResultObject(rsw, resultType)) {
      return createPrimitiveResultObject(rsw, resultMap, columnPrefix);
    } else if (!constructorMappings.isEmpty()) {
      return createParameterizedResultObject(rsw, resultType, constructorMappings, constructorArgTypes, constructorArgs, columnPrefix);
    } else if (resultType.isInterface() || metaType.hasDefaultConstructor()) {
      return objectFactory.create(resultType);
    } else if (shouldApplyAutomaticMappings(resultMap, false)) {
      return createByConstructorSignature(rsw, resultType, constructorArgTypes, constructorArgs);
    }
    throw new ExecutorException("Do not know how to create an instance of " + resultType);
  }

經(jīng)過調(diào)試發(fā)現(xiàn),mybatis會先查找該javabean有無默認(rèn)構(gòu)造方法,如果有則采用設(shè)值注入,若沒有,則根據(jù)javabean的有參構(gòu)造方法進(jìn)行設(shè)值,而在8以前的jdk版本中,我們利用反射只能獲取到參數(shù)類型,不能獲取到參數(shù)名稱,這其中設(shè)值可能出現(xiàn)了匹配失誤,將cname的值同時賦給了cname和stuclass。

想要解決這個問題,只須在javabean中添加默認(rèn)構(gòu)造方法即可。

使用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>

如果是實體中是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集成kafka實例代碼

    java集成kafka實例代碼

    文章介紹了如何在Java項目中集成Apache Kafka以實現(xiàn)消息的生產(chǎn)和消費,通過添加Maven依賴、配置生產(chǎn)者和消費者、使用SpringBoot簡化集成以及控制消費者的啟動和停止,可以實現(xiàn)高效的消息處理
    2024-12-12
  • 解決idea使用maven編譯正常但是運行項目時卻提示很多jar包找不到的問題

    解決idea使用maven編譯正常但是運行項目時卻提示很多jar包找不到的問題

    這篇文章主要介紹了解決idea使用maven編譯正常但是運行項目時卻提示很多jar包找不到的問題,本文分多種情形給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • java循環(huán)結(jié)構(gòu)、數(shù)組的使用小結(jié)

    java循環(huán)結(jié)構(gòu)、數(shù)組的使用小結(jié)

    這篇文章主要介紹了java循環(huán)結(jié)構(gòu)、數(shù)組的使用小結(jié),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • spring中REST和RESTful的區(qū)別以及基本實現(xiàn)

    spring中REST和RESTful的區(qū)別以及基本實現(xiàn)

    本文主要介紹了spring中REST和RESTful的區(qū)別以及基本實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Java實現(xiàn)http請求文件流對帶寬限速獲取md5值

    Java實現(xiàn)http請求文件流對帶寬限速獲取md5值

    文章介紹了如何在進(jìn)行HTTP請求下載大數(shù)據(jù)時處理帶寬限制和并發(fā)問題,通過使用緩沖區(qū)和限速邏輯,可以有效控制下載速度,避免掉包和數(shù)據(jù)丟失,核心公式基于帶寬和已下載字節(jié)數(shù)計算預(yù)期耗時,并通過Thread.sleep()進(jìn)行動態(tài)休眠補(bǔ)償,感興趣的朋友一起看看吧
    2025-02-02
  • 模擬Ping操作的一個Java類

    模擬Ping操作的一個Java類

    這篇文章主要為大家詳細(xì)介紹了一個模擬Ping操作的Java類,感興趣的小伙伴們可以參考一下
    2016-03-03
  • java如何保證多個線程按一定順序執(zhí)行

    java如何保證多個線程按一定順序執(zhí)行

    這篇文章主要介紹了java如何保證多個線程按一定順序執(zhí)行問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • java FileOutputStream輸出流的使用解讀

    java FileOutputStream輸出流的使用解讀

    這篇文章主要介紹了java FileOutputStream輸出流的使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java Online Exam在線考試系統(tǒng)的實現(xiàn)

    Java Online Exam在線考試系統(tǒng)的實現(xiàn)

    讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+springboot+vue+jsp+mysql+maven實現(xiàn)Online Exam在線考試系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平
    2021-11-11
  • Java如何在PDF中添加ToolTip工具提示

    Java如何在PDF中添加ToolTip工具提示

    大家好,本篇文章主要講的是Java如何在PDF中添加ToolTip工具提示,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01

最新評論

陕西省| 四子王旗| 专栏| 如东县| 小金县| 普陀区| 涿鹿县| 清镇市| 宁阳县| 云安县| 霍林郭勒市| 安乡县| 遂宁市| 三门县| 大姚县| 额尔古纳市| 丰台区| 来凤县| 梁河县| 商丘市| 招远市| 防城港市| 台中市| 霍林郭勒市| 牡丹江市| 垫江县| 宁明县| 荣成市| 古田县| 象山县| 手机| 湘乡市| 喀喇沁旗| 蕲春县| 云浮市| 达孜县| 岐山县| 卢龙县| 伊金霍洛旗| 浦县| 麦盖提县|