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

mybatis resultmap 如何為對象賦值的調用順序

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

resultmap 為對象賦值的調用順序

寫了一個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屬性應該為空。

然而程序運行后,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);
  }

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

想要解決這個問題,只須在javabean中添加默認構造方法即可。

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

如果是實體中是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 標簽來寫

    <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集合,

應該使用collection標簽

<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的時候,應該直接用as后面的字段名,即自己命的名字

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

resultMap中各個標簽的含義

在這里插入圖片描述

tips:

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

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

相關文章

  • java集成kafka實例代碼

    java集成kafka實例代碼

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

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

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

    java循環(huán)結構、數組的使用小結

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

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

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

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

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

    模擬Ping操作的一個Java類

    這篇文章主要為大家詳細介紹了一個模擬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)

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

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

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

最新評論

南漳县| 南皮县| 崇文区| 景谷| 邳州市| 楚雄市| 玉树县| 道真| 棋牌| 夏河县| 北辰区| 建昌县| 安平县| 四子王旗| 东平县| 湘潭市| 常熟市| 育儿| 商丘市| 明水县| 广水市| 金华市| 三河市| 防城港市| 射洪县| 辉南县| 南投市| 神木县| 河南省| 宽城| 林州市| 西乌| 安阳县| 陆河县| 吉木萨尔县| 婺源县| 称多县| 黔西| 大兴区| 卓资县| 宝清县|