Expected one result (or null) to be returned by selectOne(), but found: 5
1.原因分析
myabtis的xml配置bean数据库等均无错误,当 select返回单个结果集时,不会报错,多个结果集时则报错。
2.报错现象
·dao定义如下
/**
* 获取datagrid数据
* @param example
* @param daaId
* @return
*/
Page<DaaOrginfo> getDataGridByExample(@Param("example")Example example, @Param("daaId")String daaId);
·通过mapper调用dao
Page<DaaOrginfo> daaOrginfoPage = daaOrginfoxMapper.getDataGridByExample(example, daaId);
返回结果如:Expected one result (or null) to be returned by selectOne(), but found: 5
这是因为:会自动选择selectOne()方法,由于只能返回一个对象的结果,当返回到多个对象的结果集时报错。getUser是一个student类的bean无法接受多个结果集,则改用List就可以接受多个结果集
3.解决方法
·dao定义返回值修改为List
/**
* 获取datagrid数据(dao接口)
* @param example
* @param daaId
* @return
*/
List<DaaOrginfo> getDataGridByExample(@Param("example")Example example, @Param("daaId")String daaId);
·service实现类返回值修改为List
/**
* 获取datagrid数据(service实现类)
* @param example
* @param daaId
* @return
*/
List<DaaOrginfo> daaOrginfoPage = daaOrginfoxMapper.getDataGridByExample(example, daaId);
4.注意事项
1.注意spring参数传递2个及以上时,请使用@Param定义参数,否则参数无法正确绑定到sql上
2.注意mapper文件中的namespace要正确, 请通过输入加提示自动完成,如:输入com后输入点,请等待提示,并选择,防止手动输入错误。
如果你看到了这里,觉得文章写得不错就给个赞,关注公众号,可订阅更多干货?如果你觉得那里值得改进的,请给我留言,一定会认真查询,修正不足,谢谢!