我正在使用 Mybatis 为数据库制作 CRUD 应用程序,并且由于我的所有方法在打开和关闭 SQL 会话时都包含重复代码,因此我想使用调用处理程序来最小化代码重复。几乎我所有的方法看起来都是这样的:
public int deleteDefDialog(DefDialog defDialog) {
SqlSession sqlSession = ConnectionFactory.getSqlSessionFactory()
.openSession();
try {
DialogMapper dialogMapper = sqlSession
.getMapper(DialogMapper.class);
int i = dialogMapper.deleteDefDialog(defDialog);
sqlSession.commit();
return i;
} finally {
sqlSession.close();
}
}
public DefDialog selectDefDialog(BigDecimal i) {
SqlSession sqlSession = ConnectionFactory.getSqlSessionFactory()
.openSession();
try {
DialogMapper dialogMapper = sqlSession
.getMapper(DialogMapper.class);
return dialogMapper.selectDefDialog(i);
} finally {
sqlSession.close();
}
}
我的问题是如何正确编写和调用调用处理程序,记住应用程序保持线程安全?