MyBaits 使用Lambda方式连接MySQL

public class MyBatisUtils {
    // 利用static属于类不属于对象,且全局唯一
    private static SqlSessionFactory sqlSessionFactory = null;

    // 利用静态块在初始化类时实例化sqlSessionFactory
    static {
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
            // 初始化错误时抛出异常ExceptionInInitializerError通知调用者
            throw new ExceptionInInitializerError();
        }
    }

    public static Object executeQuery(Function<SqlSession, Object> func) {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        Object obj = null;
        try {
            obj = func.apply(sqlSession);
        } finally {
            sqlSession.close();
        }
        return obj;
    }
}