解决问题
1.SqlSessionFactoryBuilder().build(inputStream)参数报错
解决方案:
String resource = "config/mybatis-config.xml";
Reader inputStream = Resources.getResourceAsReader(resource);
//这里使用了 工厂设计模式
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
2.
我的jdk版本是1.7,这种写法 是1.7以及以上写法才有的,属于可以但没必要之举。这种写法实际上 增强了try语句的功能——它允许在try关键字后跟一对圆括号,圆括号可以声明,初始化一个或多个资源,此处的资源指得是那些必须在程序结束时必须关闭的资源(比如数据库连接,网络连接等),try语句在该语句结束时自动关闭这些资源。这种称为try-with-resources语句。
这里requred的参数类型由此变成了“Autocloseable”这下参数类型对不上了,就报错。知道原理后这个问题就好解决了,我们按照老办法来写别让人家自动回收就是了呗:
SqlSession sqlSession = sqlSessionFactory.openSession();
try{
sqlSession.getMapper(UserDao.class);
} catch (Exception e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
3.spring boot Cannot find class: com.mysql.cj.jdbc.Driver
参考资料
springboot 使用select的注解,来查询数据库。
知识分享
写sql语句有两种方法,一种是在xml中写sql:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.studentmanaging.Dao.UserDao">
<select id="selectByuP" resultType="com.example.studentmanaging.entity.UserEntity">
select * from UserEntity where id = #{phone}
</select>
</mapper>
如果没有表关联操作,也可以使用@select注解。