Mybatis中ResultMap常用参数详解

ResultMap常用标签

以下例子以Office对象为例

   public class Office {
   private String id;
   private String name;
   private String sort;
   private String code;
   private String type;
   private User createBy;
   private Office parent;
   private List<OfficeReceiver> receiverList;
}

ID

id标签一般设置为一条记录的主键ID

column: sql的字段名

property: 需要映射的对象属性名

需要注意的是: 如果resultMap中设置了id标签,并且查询结果集中存在相同id的数据,
那么,mybatis会自动去重只保留一条

<id column="id" property="id"/>

如果想要避免自动去重,可以根据业务需要多设置一个id标签,如:

/** 两条完全一致的数据无法避免自动去重,应当选择其他返回方式,如Map等 */
<id column="id" property="id"/>
 <id column="name" property="name"/>

result

普通参数标签

column: sql的字段名

property: 需要映射的对象属性名

<result column="id" property="id"/>

association

对象标签(一对一)
用法与ResultMap基本一致,下图中parent.name会自动映射到Office对象中parent子对象的name属性中

property: 需要映射的属性名

javaType: 返回的java类型

<association property="parent" javaType="Office">
   		<id property="id" column="parent.id"/>
   		<result property="name" column="parent.name"/>
</association>

collection

对象标签(一对多)
property: 映射的属性名

javaType: 返回的java类型,collection中一般为List

ofType: 该查询返回的java实体类型

column: 非必填,执行这个子查询时传入的参数,多个参数","分隔,
如:column=“officeId=id,officeName=name” 左边为子查询需要传入的参数名,右侧为本条查询的sql字段名(有别名则为别名)

select: 执行的查询所在路径及id

fetchType: 懒加载模式

<collection property="receiverList" javaType="ArrayList" ofType="com.sys.entity.OfficeReceiver" column="officeId=id" select="com.sys.dao.OfficeReceiverDao.getReceiverById" fetchType="lazy"/>

resultMap的继承

关键字:extends: 需要继承的resultMap的id

应用场景:一个resultMap定义公共的字段参数,再根据的业务需要分别拼接不同的字段参数或子查询等.减少重复代码

   <resultMap id="findByIdResult" type="Office" extends="officeResult">
   	<collection property="receiverList" javaType="ArrayList" ofType="com.sys.entity.OfficeReceiver" column="officeId=id" select="com.sys.dao.OfficeReceiverDao.getReceiverById" fetchType="lazy"/>
   </resultMap>

以下为文中所用到的示例:
OfficeDao.xml

	<resultMap id="officeResult" type="Office">
		<id column="id" property="id"/>
		<result column="code" property="code"/>
		<result column="name" property="name"/>
		<result column="sort" property="sort"/>
		<result column="type" property="type"/>

		<association property="createBy" javaType="User">
			<id property="id" column="createBy.id"/>
		</association>

		<association property="parent" javaType="Office">
			<id property="id" column="parent.id"/>
			<result property="name" column="parent.name"/>
		</association>
	</resultMap>

	<resultMap id="findByIdResult" type="Office" extends="officeResult">
		<collection property="receiverList" javaType="ArrayList" ofType="com.sys.entity.OfficeReceiver" column="officeId=id" select="com.sys.dao.OfficeReceiverDao.getReceiverById" fetchType="lazy"/>
	</resultMap>
	
	<select id="findById" resultMap="findByIdResult">
		SELECT a.* FROM office a
		where a.office_id = #{officeId}
	</select>

OfficeReceiverDao.xml

<select id="getReceiverById" resultType="OfficeReceiver">
		SELECT
			a.*
		FROM office_receiver a
		where
			a.office_id = #{officeId}
</select>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatisresultMap用于自定义查询结果的映射规则。通过resultMap,我们可以将查询结果的列与对象的属性进行映射,实现灵活的结果处理。 resultMap的定义可以在<select>元素使用,也可以在外部进行定义并在<select>元素引用。通过resultMap,我们可以将查询结果的列名与对象的属性名进行映射,甚至可以将不一致的列名映射为对象的属性。这样,我们可以灵活地处理查询结果,满足不同的需求场景。 使用resultMap进行自定义结果映射时,我们可以指定要显示的列,也可以定义映射规则来处理复杂的查询结果。这种方式非常灵活,广泛应用于实际开发。 总结来说,resultMapMyBatis是用来定义查询结果的映射规则的。它可以实现结果与对象属性的灵活映射,可以处理复杂的查询结果,并且应用广泛。在使用resultMap时,需要注意resultType和resultMap属性不能同时存在,只能选择其一种使用。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [MyBatisresultMap详解](https://blog.csdn.net/weixin_49707895/article/details/109564527)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值