2020-12-27
今天遇到的问题
依然是mybatis的清理问题,
无论如何都无法加载驱动,这边尝试删除所有包然后重新导入并且重构项目解决了问题
ManyResaultExption
再就ManyResaultExption是问题,
因为配置的sql语句是selectAll执行的也是查询全部,封装为一个对象
所以返回的是一个结果集
这边接收的时候需要以一个集合对象来接收,并且集合类型为bean对应类型
<select id="selectAll" resultType="Bean.Student" >
SELECT * FROM student
</select>
这里定义了语句,resultType定义了接收类型为Student
场景1:
InputStream is = Resources.getResourceAsStream("MyBatiscConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = build.openSession();
Student stu = sqlSession.selectOne("StudentMapper.selectAll");
System.out.println(stu);
用单个对象来接收,所以会出现MannyResault
场景2:
InputStream is = Resources.getResourceAsStream("MyBatiscConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = build.openSession();
List<Student> stu = sqlSession.selectOne("StudentMapper.selectAll");
虽然这里使用的集合进行接收但是
sqlSession却使用了错误的selectOne方法进行执行
获取多个对象返回集合需要使用selectList方法执行
否则依然会ManyResault
for (Student student : stu) {
System.out.println(student);
}