解決mybatis 中collection嵌套collection引發(fā)的bug
我就廢話不多說了,大家還是直接看代碼吧~
<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一直是空
類關(guān)系介紹:
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 嵌套的結(jié)果集不能被安全的轉(zhuǎn)為自定義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 源碼發(fā)現(xiàn)里面有一個常量是
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); // 設(shè)置為false
return session;
}
這樣就可以了。
以上這篇解決mybatis 中collection嵌套collection引發(fā)的bug就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- MyBatis中的collection兩種使用方法及效率比較
- mybatis?實現(xiàn)多層級collection嵌套
- Mybatis中Collection集合標(biāo)簽的使用詳解
- 詳解mybatis中association和collection的column傳入多個參數(shù)問題
- mybatis利用association或collection傳遞多參數(shù)子查詢
- Mybatis中collection和association的使用區(qū)別詳解
- Mybatis使用collection標(biāo)簽進行樹形結(jié)構(gòu)數(shù)據(jù)查詢時攜帶外部參數(shù)查詢
相關(guān)文章
java框架基礎(chǔ)之SPI機制實現(xiàn)及源碼解析
這篇文章主要為大家介紹了java框架基礎(chǔ)之SPI機制實現(xiàn)及源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
java進制轉(zhuǎn)換工具類實現(xiàn)減少參數(shù)長度
這篇文章主要為大家介紹了java進制轉(zhuǎn)換工具類實現(xiàn)減少參數(shù)長度示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
RocketMQ4.5.2 修改mqnamesrv 和 mqbroker的日志路徑操作
這篇文章主要介紹了RocketMQ 4.5.2 修改mqnamesrv 和 mqbroker的日志路徑操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Java數(shù)據(jù)結(jié)構(gòu)中雙向鏈表的實現(xiàn)
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)中雙向鏈表的實現(xiàn),雙向鏈表是一種常見的數(shù)據(jù)結(jié)構(gòu),它允許在鏈表中的任意位置進行高效的插入和刪除操作,需要的朋友可以參考下2022-05-05
JVM內(nèi)存溢出和內(nèi)存泄漏的區(qū)別及說明
這篇文章主要介紹了JVM內(nèi)存溢出和內(nèi)存泄漏的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02

