Mybatis 中获得 connection

转:

Mybatis 中获得 connection

  1. @Autowired
  2. private SqlSession sqlSession;

 

  1. public Connection getConnection(){
  2. Connection conn = null;
  3. try {
  4. conn = sqlSession.getConfiguration().getEnvironment().getDataSource().getConnection();
  5. logger.info("===This Connection isClosed ? "+conn.isClosed());
  6. } catch (Exception e) {
  7. e.printStackTrace();
  8. }
  9. return conn;
  10. }

 

    1. String resource = "SqlMap.xml"; Reader reader = Resources.getResourceAsReader(resource);
    2. SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
    3. Connection db = sqlMap.getDataSource().getConnection();
    4. Statement st = db.createStatement();
    5. ResultSet rs = st.executeQuery("show tables");
posted @ 2019-04-23 16:42  戈博折刀  阅读(2305)  评论(0编辑  收藏  举报