autoMapping和autoMappingBehavior的区别及说明
作者:Androidbo 时间:2023-11-29 06:39:25
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无法正确赋值的问题。
mybaits collection使用autoMapping注意点
mybaits 在resultMap 中使用autoMapping 时出现以下情况
<collection property="persons" ofType="io.ztx.infra.dto.PersonDTO" autoMapping = "true">
<id property="id" column="person_id"/>
<result property="name" column="person_name"/>
</collection>
在collection 中设置了autoMapping,也指定了映射字段的列和属性名,会出现关联查询时collection返回是null,会直接映射成空对象
id : 1,
persons: [
{
personId:null,
personName:null
}
]
实验几次后发现去掉autoMaping就不会出现这种情况
id : 1,
persons:[]
还有一种方法是把<result/> 换成<id/> 同样能够解决
<collection property="persons" ofType="io.ztx.infra.dto.PersonDTO" autoMapping = "true">
<id property="id" column="person_id"/>
<id property="name" column="person_name"/>
</collection>
一般不会出现同时开启autoMapping 又使用指定列和类属性方式的二者取其一就行。
自动映射可以通过columnPrefix指定前缀以及返回在sql中设置别名的方式来映射。这样就可以用手动去collection中写<id/> <resule/>了。
<collection property="persons" ofType="io.ztx.infra.dto.PersonDTO" autoMapping = "true" columnPrefix="p_">
</collection>
select
a.id as id,
p.id as p_id,
p.name as p_name
fron A a
left Join person p on p.id = a.pid
注意:SQL中映射的别名必须以设置好的前缀相同,同时保证别名和类属性名符合映射规则。
来源:https://blog.csdn.net/qq_34199415/article/details/80584452
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot 整合 TKMybatis 二次简化持久层代码的实现
c#实现哈夫曼树算法
Java集合之Comparable和Comparator接口详解
Java 实现判定顺序表中是否包含某个元素(思路详解)
![](https://img.aspxhome.com/file/2023/4/58474_0s.png)
Android编程实现获取当前连接wifi名字的方法
Java 反射调用静态方法的简单实例
Java中Builder模式的实现详解
Android为View添加拖放效果的方法实例
java eclipse 整个项目或包查找只定字符串并替换操作
![](https://img.aspxhome.com/file/2023/1/101781_0s.jpg)
Android使用CardView作为RecyclerView的Item并实现拖拽和左滑删除
![](https://img.aspxhome.com/file/2023/0/138690_0s.png)
Android中如何安全地打印日志详解
![](https://img.aspxhome.com/file/2023/3/120143_0s.png)
Android帧动画、补间动画、属性动画用法详解
Android编程实现调用相册、相机及拍照后直接裁剪的方法
Spring 4.0新功能:@Conditional注解详细介绍
C#把数组中的某个元素取出来放到第一个位置的实现方法
判断java文件类型TikaToolKit的实例
Android—基于微信开放平台v3SDK开发(微信支付填坑)
![](https://img.aspxhome.com/file/2023/6/137346_0s.png)
Java线程公平锁和非公平锁的差异讲解
AjaxControlToolkit AjaxFileUpload 显示英文改成中文的解决方法
![](https://img.aspxhome.com/file/2023/5/106425_0s.gif)