mybatis Expected one result , but found

问题报错:
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相同了,更改或去除重复字段名

阅读 83

推荐阅读