`
Caused by: org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 15
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:66)
at sun.reflect.GeneratedMethodAccessor67.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:354)
... 76 more
`根据错误提示是说本应通过selectOne这个方法返回一个结果或是空值,但是却找到了15个结果
网上搜,有的说是返回结果应该改为list集合类型,有的说检查Mybatis配置文件中返回的结果类型与Java中返回类型是否一致,检查传递参数的个数,可是我发现这些办法在我这里并不适用
相关代码如下:
mybatis的映射文件:
<!-- 通过ID获取数据 -->
<select id="findById" parameterType="pd" resultType="pd">
select
<include refid="Field"></include>
from
<include refid="tableName"></include>
<where>
<if test="KWORDS_ID!= null and KWORDS_ID != ''">
KWORDS_ID = #{KWORDS_ID}
</if>
<if test="KWORDID!= null and KWORDID != ''">
and KWORDID = #{KWORDID}
</if>
<if test="KWORDNAME!= null and KWORDNAME != ''">
and KWORDNAME = #{KWORDNAME}
</if>
<if test="KWORDNAME!=null and KWORDNAME !=''">
and PARENTID = #{PARENTID}
</if>
</where>
</select>
接口:
/**通过id获取数据
* @param pd
* @throws Exception
*/
public PageData findById(PageData pd)throws Exception;
实现类:
/**通过id获取数据
* @param pd
* @throws Exception
*/
public PageData findById(PageData pd)throws Exception{
return (PageData)dao.findForObject("KwordsMapper.findById", pd);
}
其中findForObject返回的就是select One
public Object findForObject(String str, Object obj) throws Exception {
return sqlSessionTemplate.selectOne(str, obj);
}
Controller:
/**
* 校验父类下唯一的代码和名字
* @return
* @throws Exception
*/
@RequestMapping(value="/validate")
@ResponseBody
public Object validateKwords() throws Exception {
PageData pd = new PageData();
Map<String,Object> map = new HashMap<String,Object>();
pd = this.getPageData();
if (kwordsService.findById(pd)!=null) {
map.put("msg", "true");
}else{
map.put("msg", "false");
}
return AppUtil.returnObject(pd, map);
}
看一下,是不是这个地方写的有问题,导致你的sql查询结果返回多条了。。