org.apache.ibatis.exceptions.TooManyResultsException
话不多说,先看报错信息
org.apache.ibatis.exceptions.TooManyResultsException:
Expected one result (or null) to be returned by selectOne(), but found: 3
反馈信息告诉我们,你写的方法要返回的是一个对象的结果,但返回了三个结果,来看看我的源代码:
public interface CommMapper {
public int addCommunity(Community community);
public List<Community> findAll();
public int updateCommunity(Community community);
public Community findById(int id);
public Community findMembers(int id);
public Community findRecord(int id);
}
代码中,我调用findMembers这个方法:
@RequestMapping(value = "/community/findCommunityMembers.do")
public String findCommunityMembers(int id, Model model){
Community community = communityService.findMembers(id);
model.addAttribute("community", community);
return "CommunityMembers";
}
通过model把查询结果传到CommunityMembers.jsp,对应代码
当查询结果只有一个时,代码是不会报错的;当有多个结果时,就应该将mapper接口中的findMembers方法的返回值设置为列表。
解决方法:
public List<Community> findMembers(int id);
对应Controller接受结果改为:
List<Community> communityList = communityService.findMembers(id);
model.addAttribute("communityList", communityList);
CommunityMembers.jsp使用嵌套循环,问题就解决了,如下: