Software Engineering Course III-DAY 3

Solve the problem

1.SqlSessionFactoryBuilder().build(inputStream) parameter error


String resource = "config/mybatis-config.xml";        Reader inputStream = Resources.getResourceAsReader(resource);        //这里使用了 工厂设计模式        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


My jdk version is 1.7. This writing method is only available in 1.7 and above. It is possible but unnecessary. This kind of writing actually enhances the function of the try statement-it allows a pair of parentheses to be followed by the try keyword. The parentheses can declare and initialize one or more resources. The resources here refer to those that must be at the end of the program Resources that must be closed (such as database connections, network connections, etc.), the try statement automatically closes these resources at the end of the statement. This is called a try-with-resources statement.

Here, the parameter type of requred becomes "Autocloseable". If the parameter type is not correct, an error will be reported. After knowing the principle, this problem can be solved. Let's follow the old method to write Don't let people automatically recycle it:

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 uses select annotations to query the database.

Knowledge sharing

There are two ways to write sql statement, one is to write sql in xml:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper        PUBLIC "-// Config 3.0//EN"        "">     <mapper namespace="com.example.studentmanaging.Dao.UserDao">    <select id="selectByuP" resultType="com.example.studentmanaging.entity.UserEntity">            select * from UserEntity where id = #{phone}  </select>    </mapper>

If there is no table association operation, you can also use the @select annotation.