Mybatis工具类(自动提交事务)

public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            //获取工具类SqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //sqlSessionFactory获取session实例
    //自动提交事务
    public static SqlSession getSqlSession() {

        return sqlSessionFactory.openSession(true);
    }
}

 

上一篇:MyBatis 生命周期


下一篇:mybatis的数据库连接池