問題描述
Expected one result (or null) to be returned by selectOne(), but found: 3
@Test public void findAllScoreByCourseName(){ SqlSession session = MyBatisUtils.getSession(); Object student = session.selectList("mapper.ScoreMapper.findAllScoreByCourseName", "JavaEE程序設計"); System.out.println(student); session.close(); }
原因分析:
Expected one result (or null) to be returned by selectOne(), but found: 3
意思是“期望selectOne()返回一個結(jié)果(或null),但發(fā)現(xiàn)3個”這說明你的返回結(jié)果有三個對象,但是selectOne只能返回一個。
解決方案:
第一種方案:可以把selectOne更換為selectList,這樣就可以返回多個對象了。
第二種方案:可以把多個對象封裝到一個類中,比如把student類封裝把score類中
public class Score { private String sno; //學號 private String cno; //課程號 private int score; //成績 private String number; //第幾次選修,默認值為1 private List<Student> studentList;然后,直接用selectOne返回Score類就可以了。文章來源:http://www.zghlxwxcb.cn/news/detail-411946.html
第三種方案:就是修改sql語句,例如取別名等等,這個方法不推薦。文章來源地址http://www.zghlxwxcb.cn/news/detail-411946.html
到了這里,關于Expected one result (or null) to be returned by selectOne(), but found: 3報錯解決方案的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!