广告
返回顶部
首页 > 资讯 > 精选 >mybatis resultmap怎么为对象赋值的调用顺序
  • 386
分享到

mybatis resultmap怎么为对象赋值的调用顺序

2023-06-29 00:06:36 386人浏览 八月长安
摘要

这篇文章主要介绍“mybatis resultmap怎么为对象赋值的调用顺序”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“mybatis resultmap怎么为对象赋值的调用顺

这篇文章主要介绍“mybatis resultmap怎么为对象赋值的调用顺序”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“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);  }

经过调试发现,mybatis会先查找该javabean有无默认构造方法,如果有则采用设值注入,若没有,则根据javabean的有参构造方法进行设值,而在8以前的jdk版本中,我们利用反射只能获取到参数类型,不能获取到参数名称,这其中设值可能出现了匹配失误,将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中各个标签的含义

mybatis resultmap怎么为对象赋值的调用顺序

tips:

在一个 resultMap 元素中,这些子元素出现的先后顺序是有严格规定的,它们从前到后依次是:constructor&ndash;>id --> result&ndash;> association&ndash;>collection -->discriminator, 不然就会报错。

关于“mybatis resultmap怎么为对象赋值的调用顺序”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网精选频道,小编每天都会为大家更新不同的知识点。

--结束END--

本文标题: mybatis resultmap怎么为对象赋值的调用顺序

本文链接: https://www.lsjlt.com/news/321742.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • mybatis resultmap怎么为对象赋值的调用顺序
    这篇文章主要介绍“mybatis resultmap怎么为对象赋值的调用顺序”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“mybatis resultmap怎么为对象赋值的调用顺...
    99+
    2023-06-29
  • mybatisresultmap如何为对象赋值的调用顺序
    目录resultmap 为对象赋值的调用顺序java bean定义如下部分mybatis映射文件如下createResultObject方法的代码如下使用resultMap时需注意的...
    99+
    2022-11-13
  • Java Mybatis使用resultMap时,属性赋值顺序错误的巨坑
    目录Mybatis使用resultMap属性赋值顺序错误ids是后加入的字段 resultMap中是这样写的解决办法Mybatis使用resultMap时需注意Mybati...
    99+
    2022-11-13
  • vue怎么将对象中所有的key赋为空值
    这篇文章主要介绍“vue怎么将对象中所有的key赋为空值”,在日常操作中,相信很多人在vue怎么将对象中所有的key赋为空值问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”vue怎么将对象中所有的key赋为空值...
    99+
    2023-06-29
  • Jpa怎么使用@EntityListeners实现实体对象的自动赋值
    这篇文章主要介绍“Jpa怎么使用@EntityListeners实现实体对象的自动赋值”,在日常操作中,相信很多人在Jpa怎么使用@EntityListeners实现实体对象的自动赋值问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作...
    99+
    2023-06-20
  • 怎么用vue的$set实现给数组集合对象赋值
    这篇“怎么用vue的$set实现给数组集合对象赋值”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看...
    99+
    2022-10-19
  • ES6中怎么用解构赋值获取嵌套对象的属性
    小编给大家分享一下ES6中怎么用解构赋值获取嵌套对象的属性,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!用解构赋值获取嵌套对象的...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作