解决mybatis 中collection嵌套collection引发的bug
作者:AndyMocan 时间:2023-03-20 20:55:39
我就废话不多说了,大家还是直接看代码吧~
<resultMap id="ParentMap" type="org.example.mybatis.Parent">
<id column="Id" jdbcType="VARCHAR" property="id" />
<result column="Name" jdbcType="VARCHAR" property="name" />
<result column="SurName" jdbcType="VARCHAR" property="surName" />
<collection property="children"
javaType="ArrayList" ofType="org.example.mybatis.Child"
resultMap="ChildMap" columnPrefix="c_"/>
</resultMap>
<resultMap id="ChildMap" type="org.example.mybatis.Child">
<id column="Id" jdbcType="VARCHAR" property="id" />
<result column="ParentId" jdbcType="VARCHAR" property="parentId" />
<result column="Name" jdbcType="VARCHAR" property="name" />
<result column="SurName" jdbcType="VARCHAR" property="surName" />
<result column="Age" jdbcType="INTEGER" property="age" />
<collection property="toys"
javaType="ArrayList" ofType="org.example.mybatis.Toy"
resultMap="ToyMap" columnPrefix="t_"/>
</resultMap>
<resultMap id="ToyMap" type="org.example.mybatis.Toy">
<id column="Id" jdbcType="VARCHAR" property="id" />
<result column="ChildId" jdbcType="VARCHAR" property="childId" />
<result column="Name" jdbcType="VARCHAR" property="name" />
<result column="Color" jdbcType="VARCHAR" property="color" />
</resultMap>
<sql id="Parent_Column_List">
p.Id, p.Name, p.SurName,
</sql>
<sql id="Child_Column_List">
c.Id as c_Id, c.ParentId as c_ParentId, c.Name as c_Name, c.SurName as c_Surname, c.Age as c_Age,
</sql>
<sql id="Toy_Column_List">
t.Id as t_Id, t.Name as t_Name, t.Color as t_Color
</sql>
<select id="getParent" parameterType="java.lang.String" resultMap="ParentMap" >
select
<include refid="Parent_Column_List"/>
<include refid="Child_Column_List" />
<include refid="Toy_Column_List" />
from Parent p
left outer join Child c on p.Id = c.ParentId
left outer join Toy t on c.Id = t.ChildId
where p.id = #{id,jdbcType=VARCHAR}
</select>
表面来看没有任何问题 实际 查询的child对象中的toys一直是空
类关系介绍:
Parent类有属性ArrayList<Child> children
Child类有属性ArrayList<Toy> toys
Toy是一个普通的类
原因在于:
<collection property="toys"
javaType="ArrayList" ofType="org.example.mybatis.Toy"
resultMap="ToyMap" columnPrefix="t_"/>
columnPrefix配置的是t_实际mybatis处理后是 c_t_
解决办法:
只需要修改 sql 修改前缀为 c_t_ 即可
<sql id="Toy_Column_List">
t.Id as c_t_Id, t.Name as c_t_Name, t.Color as c_t_Color
</sql>
补充知识:mybatis 嵌套的结果集不能被安全的转为自定义ResultHandler 的解决办法
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Mapped Statements with nested result mappings cannot be safely used with a custom ResultHandler. Use safeResultHandlerEnabled=false setting to bypass this check.
问题描述
session.select("dao.ArticleMapper.selectAll", null, new RowBounds(1, 2),resultHandler);
会报不安全, 查询Configuration 源码发现里面有一个常量是
public Configuration() {
this.safeRowBoundsEnabled = false;
this.safeResultHandlerEnabled = true;//意思是不允许自定义ResultHand 处理器,
this.mapUnderscoreToCamelCase = false;
this.aggressiveLazyLoading = true;
解决办法
public static SqlSession getsqlSession(){
SqlSession session = sqlSessionFactory.openSession(ExecutorType.REUSE);
Configuration configuration = session.getConfiguration(); //反射得到configuration ,然后
configuration.setSafeResultHandlerEnabled(false); // 设置为false
return session;
}
这样就可以了。
来源:https://blog.csdn.net/AndyMocan/article/details/83059459
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
android判断手机是否安装地图应用实现跳转到该地图应用
![](https://img.aspxhome.com/file/2023/3/138573_0s.png)
Struts2修改上传文件大小限制方法解析
Java基础之static关键字的使用讲解
![](https://img.aspxhome.com/file/2023/9/66039_0s.jpg)
Android Handler 机制实现原理分析
![](https://img.aspxhome.com/file/2023/1/91041_0s.jpg)
Java日期时间类(Date、DateFormat、Calendar)解析
C#编程获取各种电脑硬件信息的方法示例
Kotlin中的5种单例模式示例详解
![](https://img.aspxhome.com/file/2023/6/139446_0s.jpg)
android使用SwipeRefreshLayout实现ListView下拉刷新上拉加载
![](https://img.aspxhome.com/file/2023/7/137567_0s.gif)
C#根据反射和特性实现ORM映射实例分析
C#开发Winform实现文件操作案例
![](https://img.aspxhome.com/file/2023/3/102173_0s.jpg)
springboot 打包部署 共享依赖包(分布式开发集中式部署微服务)
![](https://img.aspxhome.com/file/2023/5/88175_0s.png)
UnityUI中绘制线状统计图
![](https://img.aspxhome.com/file/2023/6/82146_0s.jpg)
SpringBoot整合dataworks的实现过程
详解Spring关于@Resource注入为null解决办法
![](https://img.aspxhome.com/file/2023/7/82017_0s.png)
分析JAVA中几种常用的RPC框架
![](https://img.aspxhome.com/file/2023/9/68509_0s.jpg)
腾讯开源消息中间件TubeMQ总体介绍分析
![](https://img.aspxhome.com/file/2023/0/83050_0s.png)
java实现时间与字符串之间转换
C# 文字代码页 文字编码的代码页名称速查表
并行Stream与Spring事务相遇会发生什么?
![](https://img.aspxhome.com/file/2023/4/85944_0s.webp)