String resource = "sqlMapConfig.xml"; InputStream inputStream = null; SqlSession sqlSession = null; try { inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() .build(inputStream); sqlSession = sqlSessionFactory.openSession(); DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class); deptMapper.deleteById(3000); sqlSession.commit(); } catch(Exception e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); sqlSession = null; } } |
首先回顾一下我们使用Mybatis来操作数据库的步骤:
1、加载核心配置文件sqlMapConfig.xml(名字可以自己随便取);
2、通过 SqlSessionFactoryBuilder来得到SqlSessionFactory ;
3、通过SqlSessionFactory 来创建SqlSession;
4、通过SqlSession来调用增删该查的方法或者获取Mapper对象来调用相应的方法去操作数据库;
5、提交事务;
6、关闭SqlSession。
但是每次操作数据库都要重复上述步骤,为什么不像封装jdbcUtil一样封装一个工具类呢?好吧,说干就干吧。
/** * Mybatis的工具类 * * @author THINK * */ public class MybatisUtil { private static final String resource = "sqlMapConfig.xml"; private static SqlSessionFactory sqlSessionFactory; /** * 初始化sqlSessionFactory,只需要一个对象,所以是静态的 */ static { InputStream inputStream; try { inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder() .build(inputStream); } catch (IOException e) { e.printStackTrace(); } } /** * 获取SqlSession对象 * @return */ public static SqlSession getSqlSession() { return sqlSessionFactory.openSession(); } /** * 查询一条记录 * @param statement * @param parameter * @return */ public static Object selectOne(String statement, Object parameter) { SqlSession sqlSession = null; Object object = null; try { sqlSession = getSqlSession(); object = sqlSession.selectOne(statement, parameter); } catch (Exception e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); sqlSession = null; } } return object; } /** * 根据条件查询多条记录 * @param statement * @param parameter * @return */ public static List<Object> selectList(String statement, Object parameter) { SqlSession sqlSession = null; List<Object> list = null; try { sqlSession = getSqlSession(); list = sqlSession.selectList(statement, parameter); } catch (Exception e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); sqlSession = null; } } return list; } /** * 插入一条记录 * @param statement * @param parameter * @return */ public int insert(String statement, Object parameter) { SqlSession sqlSession = null; int rows = 0; try { sqlSession = getSqlSession(); rows = sqlSession.insert(statement, parameter); sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); sqlSession = null; } } return rows; } /** * 删除一条记录 * @param statement * @param parameter * @return */ public int delete(String statement, Object parameter) { SqlSession sqlSession = null; int rows = 0; try { sqlSession = getSqlSession(); rows = sqlSession.delete(statement, parameter); sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); sqlSession = null; } } return rows; } /** * 更新一条记录 * @param statement * @param parameter * @return */ public int update(String statement, Object parameter) { SqlSession sqlSession = null; int rows = 0; try { sqlSession = getSqlSession(); rows = sqlSession.update(statement, parameter); sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); sqlSession = null; } } return rows; } } |
以上就是对mybatis操作数据库方法的封装,以后每次只需调用即可,不用再去重复编写代码加载配置文件、创建sqlsession了。同时数据库操作完后也将sqlsession关闭了。