1 @Test 2 public void test1() throws Exception { 3 User user = userService.getUserById(1); 4 System.out.println(user); 5 6 user = userService.getUserById(1); 7 System.out.println(user); 8 9 user = userService.getUserById(1);; 10 System.out.println(user); 11 12 }
如上测试代码,从下Debug钟可以看到依次从数据库拿了3次连接,每次查询完毕,则把数据库连接返回druid连接池,SQL被执行3次
[org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@9e918ed] was not registered for synchronization because synchronization is not active [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource [com.alibaba.druid.pool.DruidDataSource] - {dataSource-1} inited [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.mysql.jdbc.JDBC4Connection@41ee0498] will not be managed by Spring [com.ssmbase.dao.UserMapper.selectByPrimaryKey] - ==> Preparing: select id, user_name, password, age from user_t where id = ? [com.ssmbase.dao.UserMapper.selectByPrimaryKey] - ==> Parameters: 1(Integer) [com.ssmbase.dao.UserMapper.selectByPrimaryKey] - <== Total: 1 [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@9e918ed] [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource com.ssmbase.domain.User@59f52a1d [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6ea0040e] was not registered for synchronization because synchronization is not active [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.mysql.jdbc.JDBC4Connection@41ee0498] will not be managed by Spring [com.ssmbase.dao.UserMapper.selectByPrimaryKey] - ==> Preparing: select id, user_name, password, age from user_t where id = ? [com.ssmbase.dao.UserMapper.selectByPrimaryKey] - ==> Parameters: 1(Integer) [com.ssmbase.dao.UserMapper.selectByPrimaryKey] - <== Total: 1 [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6ea0040e] [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource com.ssmbase.domain.User@37d02427 [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession [org.mybatis.spring.SqlSessionUtils] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@41f59911] was not registered for synchronization because synchronization is not active [org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource [org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection [com.mysql.jdbc.JDBC4Connection@41ee0498] will not be managed by Spring [com.ssmbase.dao.UserMapper.selectByPrimaryKey] - ==> Preparing: select id, user_name, password, age from user_t where id = ? [com.ssmbase.dao.UserMapper.selectByPrimaryKey] - ==> Parameters: 1(Integer) [com.ssmbase.dao.UserMapper.selectByPrimaryKey] - <== Total: 1 [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@41f59911] [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource