autoMapping和autoMappingBehavior的区别

autoMappingBehavior

mybatis核心配置文件中settings中配置,指定 MyBatis 应如何自动映射列到字段或属性。 NONE 表示取消自动映射;PARTIAL 只会自动映射没有定义嵌套结果集映射的结果集。 FULL 会自动映射任意复杂的结果集(无论是否嵌套)。默认是partial,这是一种全局设置

autoMapping

在resultMap或者association,collections中使用,是一个局部开关,开启后会自动设置嵌套查询中的属性,局部开关优先级大于全部开关,当全部开关开启FULL映射时,局部开关关闭,这时候仍然不会进行映射。

例子

配置信息,mybatis的Settings全部为默认配置,我们测试局部自动映射的结果

	<select id="findCustomerByIdResultMap" parameterType="int" resultMap="CustomerResultMap">
	SELECT
		id,
		username,
		jobs,
		phone,
		idCard.cardId as cardId,
		idcard.address as address
		FROM
		t_customer ,
		idcard
		WHERE t_customer.cardId=idcard.cardId and t_customer.id=#{id}
	</select>
	
	<resultMap type="cn.edu.huel.po.Customer" id="CustomerResultMap">
	<id column="id" property="id"/>
	<result column="username" property="username"/>
	<result column="jobs" property="jobs"/>
	<result column="phone" property="phone"/>
	<association  property="card"  javaType="cn.edu.huel.po.IdCard">
		<id column="cardId" property="cardId"/>
		<result column="address" property="address"/>
	</association>

测试结果

DEBUG [main] - ==>  Preparing: SELECT id, username, jobs, phone, idCard.cardId as cardId, idcard.address as address FROM t_customer , idcard WHERE t_customer.cardId=idcard.cardId and t_customer.id=? 
DEBUG [main] - ==> Parameters: 2(Integer)
DEBUG [main] - <==      Total: 1
Customer [id=2, username=李四, jobs=采购, phone=222, card=IdCard [cardId=2222, address=安阳]]

去掉restult,不开启autoMapping

<select id="findCustomerByIdResultMap" parameterType="int" resultMap="CustomerResultMap">
	SELECT
		id,
		username,
		jobs,
		phone,
		idCard.cardId as cardId,
		idcard.address as address
		FROM
		t_customer ,
		idcard
		WHERE t_customer.cardId=idcard.cardId and t_customer.id=#{id}
	</select>
	
	<resultMap type="cn.edu.huel.po.Customer" id="CustomerResultMap">
	<id column="id" property="id"/>
	<association  property="card"  javaType="cn.edu.huel.po.IdCard">
		<id column="cardId" property="cardId"/>
	</association>
	
	</resultMap>


结果,可以看出在嵌套查询中,mybatis默认设置嵌套查询不自动映射,必须的有result

DEBUG [main] - ==>  Preparing: SELECT id, username, jobs, phone, idCard.cardId as cardId, idcard.address as address FROM t_customer , idcard WHERE t_customer.cardId=idcard.cardId and t_customer.id=? 
DEBUG [main] - ==> Parameters: 2(Integer)
DEBUG [main] - <==      Total: 1
Customer [id=2, username=null, jobs=null, phone=null, card=IdCard [cardId=2222, address=null]]

加上autoMapping为ture进行测试

<select id="findCustomerByIdResultMap" parameterType="int" resultMap="CustomerResultMap">
	SELECT
		id,
		username,
		jobs,
		phone,
		idCard.cardId as cardId,
		idcard.address as address
		FROM
		t_customer ,
		idcard
		WHERE t_customer.cardId=idcard.cardId and t_customer.id=#{id}
	</select>
	
	<resultMap type="cn.edu.huel.po.Customer" autoMapping="true" id="CustomerResultMap">
		<id column="id" property="id"/>
		<association  property="card" autoMapping="true"  javaType="cn.edu.huel.po.IdCard">
			<id column="cardId" property="cardId"/>
		</association>
	</resultMap>

结果,没有result,结果照样映射

DEBUG [main] - ==>  Preparing: SELECT id, username, jobs, phone, idCard.cardId as cardId, idcard.address as address FROM t_customer , idcard WHERE t_customer.cardId=idcard.cardId and t_customer.id=? 
DEBUG [main] - ==> Parameters: 2(Integer)
DEBUG [main] - <==      Total: 1
Customer [id=2, username=李四, jobs=采购, phone=222, card=IdCard [cardId=2222, address=安阳]]

总结:

autoMappingBehavior是<settings>里面的,是全局总开关。autoMapping是<resultMap>里面的,是局部select语句映射开关。
局部开关优先级大于全局开关。

如上resultMap配置了autoMapping, 那么mybatis会自动把查询出来的name、id、cartid都赋值给customer, 如果autoMappng设为false, 则不会自动映射, 需要你在resultMap中手动配置result
, 它的作用在collection和association标签中作用是一样的。
此外, 配置autoMapping这个属性的优先级高于autoMappingBehavior, 也就是即使你autoMappingBehavior配置为FULL, 但是autoMapping配置为false, 那么依旧不会自动映射。

在嵌套影射中通常会同时配置上columnPrefix属性, 这样的话可以在一定程度上避免因为实体属性名相同导致mybatis无法正确赋值的问题。




  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值