【完美解决】Could not process result for mapping: ResultMapping{property=‘null‘, column=‘xxx‘, javaType=

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Could not process result for mapping: ResultMapping{property=‘null’, column=‘settle_time’, javaType=class java.util.Date, jdbcType=TIMESTAMP, nestedResultMapId=‘null’, nestedQueryId=‘null’, notNullColumns=[], columnPrefix=‘null’, flags=[CONSTRUCTOR], composites=[], resultSet=‘null’, foreignColumn=‘null’, lazy=false}

Caused by: org.apache.ibatis.executor.ExecutorException: Could not process result for mapping: ResultMapping{property=‘null’, column=‘settle_time’, javaType=class java.util.Date, jdbcType=TIMESTAMP, nestedResultMapId=‘null’, nestedQueryId=‘null’, notNullColumns=[], columnPrefix=‘null’, flags=[CONSTRUCTOR], composites=[], resultSet=‘null’, foreignColumn=‘null’, lazy=false}

以上是报错。

这个错误困扰了我一天, 网上应该没有专门针对这个报错的解释,因此做下记录,希望帮助后来者。

一. 背景


  1. 使用mybatis-generator代码自动生成工具, 生成了Mapper文件。
  2. 业务需要, 编写带有MAX函数的SQL语句,mybatis-generator并不支持生成SQL函数, 于是选择手写SQL
  3. 代码提交后频频报上述错误。

二. 原因


ResultMap映射错误。

这里先说一下Mybatis中,两种映射resultMap和resultType的区别

基本映射 :(resultType)使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功。(数据库,实体,查询字段,这些全部都得一一对应)

<select id="selectByParam" parameterType="xxx.xxParam" resultType="xxx.xxDO">

高级映射 :(resultMap) 如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap对列名和pojo属性名之间作一个映射关系。(高级映射,字段名称可以不一致,通过映射来实现

<resultMap id="SumResultMap" type="xxx.xxDO">
    <result column="corp_id" property="corpId" javaType="java.lang.String" jdbcType="VARCHAR" />
</resultMap>
<select id="selectSumByExample" parameterType="xxx.xxExample" resultMap="SumResultMap">

一般来说,涉及到复杂语句的查询,需要用高级映射

mybatis-generator框架生成的resultMap是基于constructor属性的,constructor中的属性值,需要与POJO类唯一对应,不能多,也不能少,顺序也要相同。 但由于我使用了函数, 无法做到一一对应, 因此报错。

三. 解决办法


删除自动生成的constructor属性,使用result属性进行高级映射,将select选中的所有数据库字段与POJO类中的属性一一映射即可。 举例(通过result元素,将表中的dimension_name属性与POJO类中的dimensionName属性映射)

<result column="dimension_name" property="dimensionName" javaType="java.lang.Long" jdbcType="BIGINT" />

列一下最后的效果:

更改前:


<sql id="Sum_Column_List">
  <!-- sumColumnList -->
  MAX(dimension) dimension, MAX(dimension_name) dimension_name,
  MAX(category) category
</sql>

  <resultMap id="BaseResultMap" type="xxx.xxDO">
    <constructor>
      <idArg column="corp_id" javaType="java.lang.String" jdbcType="VARCHAR" />
      <idArg column="id" javaType="java.lang.Long" jdbcType="BIGINT" />
      <arg column="gmt_create" javaType="java.util.Date" jdbcType="TIMESTAMP" />
      <arg column="gmt_modified" javaType="java.util.Date" jdbcType="TIMESTAMP" />
      <arg column="dimension" javaType="java.lang.Long" jdbcType="BIGINT" />
      <arg column="dimension_name" javaType="java.lang.String" jdbcType="VARCHAR" />
      <arg column="category" javaType="java.lang.Long" jdbcType="BIGINT" />
    </constructor>
  </resultMap>

<select id="selectSumGroupByExample" parameterType="com.fliggy.btrip.btripds.dao.domain.entity.tripadb.BtripOverviewAllDayDataDOExample" resultMap="SumResultMap">
  select
  <if test="distinct">
    distinct
  </if>
  'true' as QUERYID,
  <include refid="Sum_Column_List" />
  from 表名
  <if test="_parameter != null">
    <include refid="Example_Where_Clause" />
  </if>
  <if test="groupByClause != null">
    group by ${
    
    groupByClause}
  </if>
  <if test="orderByClause != null">
    order by ${
    
    orderByClause}
  </if>
</select>

更改后:

<sql id="Sum_Column_List">
  <!-- sumColumnList -->
  MAX(dimension) dimension, MAX(dimension_name) dimension_name,
  MAX(category) category
</sql>

<resultMap id="SumResultMap" type="com.fliggy.btrip.btripds.dao.domain.entity.tripadb.BtripOverviewAllDayDataDO">
  <result column="dimension" property="dimension" javaType="java.lang.Long" jdbcType="BIGINT" />
  <result column="dimension_name" property="dimensionName" javaType="java.lang.String" jdbcType="VARCHAR" />
  <result column="category" property="category" javaType="java.lang.Long" jdbcType="BIGINT" />
</resultMap>

<select id="selectSumGroupByExample" parameterType="com.fliggy.btrip.btripds.dao.domain.entity.tripadb.BtripOverviewAllDayDataDOExample" resultMap="SumResultMap">
  select
  <if test="distinct">
    distinct
  </if>
  'true' as QUERYID,
  <include refid="Sum_Column_List" />
  from 表名
  <if test="_parameter != null">
    <include refid="Example_Where_Clause" />
  </if>
  <if test="groupByClause != null">
    group by ${
    
    groupByClause}
  </if>
  <if test="orderByClause != null">
    order by ${
    
    orderByClause}
  </if>
</select>

有不懂可以留言!

猜你喜欢

转载自blog.csdn.net/weixin_43899069/article/details/124024170
今日推荐