SpringMVC+myBatis3 obtains SqlSession from SqlSessionFactory and obtains Connection

SpringMVC+myBatis3 obtains SqlSession from SqlSessionFactory and obtains Connection

SpringMVC+myBatis3 obtains SqlSessionFactory

wrote
@javax.annotation.Resource
private SqlSessionFactory sqlSessionFactory;

 

In this way, the sqlsessionfactory can be taken by springMVC.

Now that we know how to get the SqlSessionFactory object, based on the same revelation, we can get an instance of SqlSession. The SqlSession object completely contains all the methods for performing SQL operations in the context of the database. You can use a SqlSession instance to obtain a Connection and execute native SQL directly.

    public Connection getCon() {
        Connection connection = null;
        SqlSession sqlSession = sqlSessionFactory.openSession();
        connection = sqlSession.getConnection();

        return connection;
    }

 

wrote
/**
* 执行SQL语句
*
* @param conn
* @param sqls
*/
private void executeSql(Connection conn, List<String> sqls) {
java.sql.Statement execSql = null;
try {
execSql = conn.createStatement();
for (String sql : sqls) {
execSql.executeUpdate(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
}

 

Mapped SQL statements can also be run directly.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327007573&siteId=291194637