实体类:
public class AB {
Integer i;
List<Integer> a;
List<Integer> b;
// getters and setters...
}
映射文件:
<resultMap id="abMap" type="AB">
<result property="i" column="i"/>
<collection property="a" ofType="int">
<constructor>
<arg column="a" javaType="_int"/>
</constructor>
</collection>
<collection property="b" ofType="int">
<constructor>
<arg column="b" javaType="_int"/>
</constructor>
</collection>
</resultMap>
数据库:
SQL 语句:
select i, a, b from ab where i=1
结果报错如下:
Caused by: org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 6
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:434)
... 71 more
虽然不太清楚原因是什么,但是这个报错就非常离谱,你难道还不知道我声明了 <collection> 标签了吗?难道 <collection> 标签中不能使用构造注入?还是构造注入不支持 java.lang.Integer 等包装类?有知道的大佬烦请指导指导,小弟感激不尽。
然后修改映射文件:
<resultMap id="abMap" type="AB">
<result property="i" column="i"/>
<collection property="a" ofType="int">
<result column="a"/>
</collection>
<collection property="b" ofType="int">
<result column="b"/>
</collection>
</resultMap>
确实是可以了,这个 <result> 难道不是通过 set 方法注入的吗??java.lang.Integer 这个类却又没有 setter 呀,即便是支持注入私有属性,我也没有指定要注入 java.lang.Integer 中的哪个属性呀。
莫非 mybatis 对包装类作了特殊处理??