关于mybatis:mybatis-Expected-one-result-but-found

14次阅读

共计 1680 个字符,预计需要花费 5 分钟才能阅读完成。

问题报错:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 3
要害 mapper 代码:

<resultMap id="rebateContractObj" type="study.mybatis.cm.entity.RebateContract"  
 extends="study.mybatis.cm.mapper.ContractDao.contractObj">  
 <!--        <id property="contractId" column="contract_id" jdbcType="INTEGER"/>-->  
 <!--        <id property="contractName" column="contractName" jdbcType="INTEGER"/>--> <result property="contractId" column="contract_id" jdbcType="INTEGER"/>  
 <result property="processMode" column="process_mode" jdbcType="INTEGER"/>  
 <collection property="agents" javaType="ArrayList"  
 ofType="study.mybatis.cm.entity.RebateContractAgent">  
 <result property="id" column="re_id" jdbcType="INTEGER"/>  
 <result property="postCode" column="post_code" jdbcType="VARCHAR"/>  
 <result property="address" column="communicate_address" jdbcType="VARCHAR"/>  
 </collection>  
</resultMap>
继承:<resultMap id="contractObj" type="study.mybatis.cm.entity.Contract">  
 <id property="contractId" column="contract_id" jdbcType="INTEGER"/>  
 <result property="zipCode" column="post_code" jdbcType="VARCHAR"/>  
 <result property="address" column="communicate_address" jdbcType="VARCHAR"/>  
</resultMap>

剖析:
1. 在 rebateContractObj 映射文件中,从新定义了 contractId 为 result 属性,笼罩了父类的 contract_id(id 属性 <id property=”contractId”)
2.rebateContractObj 中有 collection 汇合,外部有属性 communicate_address、post_code 与父 mapper 属性定义雷同
原理:
1. 当定义了 <id property> 时,返回后果集已 <id> 配置作为记录 key
2. 当没有定义 <id> 时,返回后果集以全副 result 属性作为记录 key
数据分析:
1.communicate_address 数据不统一,且没有定义 <id> 属性(父 mapper 的被笼罩),导致以全副 result 作为 key,所以会返回多条数据
解决方案:
1. 去除 rebateContractObj 中定义的 contractId 属性,保留父类 <id property=”contractId” >
2. 更改继承的 contractObj,继承的父 mapper 字段太多且和 collection 雷同了,更改或去除反复字段名

正文完
 0