利用mybatis對collection進(jìn)行嵌套時出現(xiàn)報錯如何解決?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

<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>表面來看沒有任何問題 實(shí)際 查詢的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_實(shí)際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>
補(bǔ)充知識: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;
}看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。
分享文章:利用mybatis對collection進(jìn)行嵌套時出現(xiàn)報錯如何解決-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://www.chinadenli.net/article14/dhopge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、企業(yè)網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、微信小程序、網(wǎng)站營銷、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容