以前写一对多用的都是注解形式的,所以XML形式的不太熟悉,出现这个错的时候真是让我头秃啊。
大概知道这个错的意思是说,我设置的接受数据的类型是一个实体类,期待的是返回一条数据,然而找到了两条数据,于是映射失败。
想过把实体类改为List,但不符合我的需求啊,于是开始找解决办法。
原本需求是这样的:有一个视频类video,它有很多的类别标签category,video里有一个属性categoryList接收该视频的若干个标签,希望用collection标签实现一对多映射。
原本代码是这样的:
(1)实体类
(2)mapper里的SQL
(3)测试代码
(4)报错
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 2
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:78)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
at com.sun.proxy.$Proxy27.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:159)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:87)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:57)
at com.sun.proxy.$Proxy38.findVideoWithCatrgory(Unknown Source)
at com.example.service.impl.VideoServiceImpl.getVideoWithCategory(VideoServiceImpl.java:127)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke