Mybatis 中获得 connection

Posted libin6505

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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");

以上是关于Mybatis 中获得 connection的主要内容,如果未能解决你的问题,请参考以下文章