解决mybatis 中collection嵌套collection引发的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一直是空

类关系介绍:

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;
}

这样就可以了。

以上这篇解决mybatis 中collection嵌套collection引发的bug就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • myBatis实现三级嵌套复杂对象的赋值问题

    平常我们工作中基本最多两级嵌套,但是有时候难免会遇到三级嵌套的业务场景,笔者最近就碰到了,使用一般的嵌套发现赋值为空,这可难倒了菜逼的我,后来在stackoverflow的帮助下终于搜到了解决办法,完美解决了问题 ,总结一下,方便有需要的同学,下面直接上栗子: 首先上实体类:三级嵌套如下 (电站 -----> 电桩 ---->电枪) 电站实体类 (实体为JPA写法,不影响mybatis的使用) package com.weima.cecapp.entities; import lombok.D

  • mybatis多层嵌套resultMap及返回自定义参数详解

    1.两层嵌套,一个list中加另外一个list data:[ {a:123,b:456,c:[{d:7,e:8}]} ] xml文件定义的sql select * from zhy z LEFT JOIN wl w on z.id = w.zid resultMap可以定义: <resultMap id="zhyResultMap" type="zhy的doman实体" extends="zhy自动生成的BaseResultMap">

  • Mybatis批量修改时出现报错问题解决方案

    批量修改代码如下 <update id="UPDATE_HOTEL_REAL_TIME_PRICE" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" separator=";"> UPDATE VST_HOTEL_REAL_TIME_PRICE

  • 解决Mybatis中foreach嵌套使用if标签对象取值的问题

    目录 foreach嵌套使用if标签对象取值问题 大体格式 解决办法 代码如下 Mybatis if 语句嵌套 要求 foreach嵌套使用if标签对象取值问题 最近做项目过程中,涉及到需要在 Mybatis 中 使用 foreach 进行循环读取传入的查询条件,动态拼接SQL语句,接口传入的查询条件格式:{"advanceSearchList":[{"searchType":10,"searchText":"12"}]} ,

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

    我就废话不多说了,大家还是直接看代码吧~ <resultMap id="ParentMap" type="org.example.mybatis.Parent"> <id column="Id" jdbcType="VARCHAR" property="id" /> <result column="Name" jdbcType="VARCHAR&q

  • 解决Mybatis中mapper.xml文件update,delete及insert返回值问题

    最近写了几个非常简单的接口(CRUD),在单元测试的时候却出了问题,报错如下: Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'messageListener': Unsatisfied dependency expressed through field 'reviewCheckInfoService'; nested exce

  • 解决myBatis中删除条件的拼接问题

    今天刚刚学习了mybatis,做了简单的对数据库的增删改查.在进行删除操作时,单条删除时很简单,但是批量删除的时候拼接删除条件却有些麻烦,现记录一下做法. Sql语句中,当删除条件并不唯一的时候,我们有两种删除的sql语句,一种使用or拼接where中的条件,例如delete from 表名where 条件1 or 条件2,另一种是使用in 例如delete from 表名where 元素in( ) 利用第一种删除语句在mybatis中的mapping.xml中进行拼接: 利用第二种删除语句在m

  • 解决MyBatis中为类配置别名,列名与属性名不对应的问题

    在传参与接收返回结果的时候,咱们一直是使用的全限定名.但是MyBatis自己在使用很多类型的时候(如Integer,Boolean)却可以直接使用别名.那么,咱们自己的写的类能不能使用别名呢?可以.需要配置. mybatis配置文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//

  • 解决mybatis中order by排序无效问题

    1.#将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #{user_id},如果传入的值是111,那么解析成sql时的值为order by "111", 如果传入的值是id,则解析成的sql为order by "id". 2.$将传入的数据直接显示生成在sql中.如:order by ${user_id},如果传入的值是111,那么解析成sql时的值为order by 111, 如果传入的值是id,则解析成的sql为order b

  • 解决MyBatis中Enum字段参数解析问题

    目录 基础Class和TypeHandler 请求参数解析问题 问题解决 基础Class和TypeHandler MyBatis操作的基本User对象结构如下: @Data @Alias(value = "user") public class User implements Serializable { private static final long serialVersionUID = -4947062488310146862L; private Long id; @NotNu

  • 解决mybatis中的mapper命名问题

    mybatis mapper命名问题 mapper文件中id命名最好首字母小写,避免让mybatis认为是一个类 <!--获取供应商列表--> <resultMap id="ProviderList" type="Provider"> <result property="id" column="id"/> <result property="proCode" col

  • 解决MyBatis中模糊搜索使用like匹配带%字符时失效问题

    目录 1.问题背景 2.解决方法 2.1.在入参SearchVo上进行特殊符号relpace转换 2.2.使用ESCAPE 2.3.总结 1.问题背景 Mybatis是我们日常项目中经常使用的框架,在项目中我们一般会使用like查询作为模糊匹配字符进行搜索匹配,下面的Mapper.xml是我们使用like在项目中进行模糊匹配的常用方式: <sql id="searchCondition"> <trim prefix="where" prefixOv

  • 解决mybatis中resultType取出数据顺序不一致的问题

    目录 mybatis resultType取出数据顺序不一致 解决方法 mybatis中resultType问题 mybatis resultType取出数据顺序不一致 之前做一个页面的动态表格,希望表格的列能根据数据库查出来的数据保持一致,但是返回页面的结果集是无序的在网上查了一下原因. 原来我的查询返回resultType = "map",也就是这个map,打乱了顺序.因为map并不能保证存入取出数据一致. 解决方法 <select id="test" r

随机推荐