重复的类我们用工具把它包装起来:
public class MybatisUtils {private static SqlSessionFactory factory = null;static{String config = "mybatis.xml";try {InputStream in = Resources.getResourceAsStream(config);factory = new SqlSessionFactoryBuilder().build(in);} catch (IOException e) {throw new RuntimeException(e);}}public static SqlSession getSqlSession(){SqlSession sqlSession = null;if(factory != null){//非自动提交事务sqlSession = factory.openSession();}return sqlSession;}
}
三、JDK动态代理
传统的Dao实现方式如下: