解决mybatis 中collection嵌套collection引发的bug

网友投稿 1212 2023-03-05

解决mybatis 中collection嵌套collection引发的bug

解决mybatis 中collection嵌套collection引发的bug

我就废话不多说了,大家还是直接看代码吧~

javaType="ArrayList" ofType="org.example.mybatis.Child"

resultMap="ChildMap" columnPrefix="c_"/>

<collection property="toys"

javaType="ArrayList" ofType="org.example.mybatis.Toy"

resultMap="ToyMap" columnPrefix="t_"/>

p.Id, p.Name, p.SurName,

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,

t.Id as t_Id, t.Name as t_Name, t.Color as t_Color

select

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}

表面来看没有任何问题 实际 查询的child对象中的toys一直是空

类关系介绍:

Parent类有属性ArrayList children

Child类有属性ArrayList<Toy> toys

Toy是一个普通的类

原因在于:

javaType="ArrayList" ofType="org.example.mybatis.Toy"

resultMap="ToyMap" columnPrefix="t_"/>

columnPrefix配置的是t_实际mybatis处理后是 c_t_

解决办法:

只需要修改 sql 修改前缀为 c_t_ 即可

t.Id as c_t_Id, t.Name as c_t_Name, t.Color as c_t_Color

补充知识: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;

}

这样就可以了。

javaType="ArrayList" ofType="org.example.mybatis.Child"

resultMap="ChildMap" columnPrefix="c_"/>

<collection property="toys"

javaType="ArrayList" ofType="org.example.mybatis.Toy"

resultMap="ToyMap" columnPrefix="t_"/>

p.Id, p.Name, p.SurName,

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,

t.Id as t_Id, t.Name as t_Name, t.Color as t_Color

select

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}

表面来看没有任何问题 实际 查询的child对象中的toys一直是空

类关系介绍:

Parent类有属性ArrayList children

Child类有属性ArrayList<Toy> toys

Toy是一个普通的类

原因在于:

javaType="ArrayList" ofType="org.example.mybatis.Toy"

resultMap="ToyMap" columnPrefix="t_"/>

columnPrefix配置的是t_实际mybatis处理后是 c_t_

解决办法:

只需要修改 sql 修改前缀为 c_t_ 即可

t.Id as c_t_Id, t.Name as c_t_Name, t.Color as c_t_Color

补充知识: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;

}

这样就可以了。

javaType="ArrayList" ofType="org.example.mybatis.Toy"

resultMap="ToyMap" columnPrefix="t_"/>

columnPrefix配置的是t_实际mybatis处理后是 c_t_

解决办法:

只需要修改 sql 修改前缀为 c_t_ 即可

t.Id as c_t_Id, t.Name as c_t_Name, t.Color as c_t_Color

补充知识: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;

}

这样就可以了。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:vue3 开发小程序(vue框架做小程序)
下一篇:IDEA一键完成格式化、去除无用引用、编译的操作
相关文章

 发表评论

暂时没有评论,来抢沙发吧~