1. 通过官方文档发现 , mybatis使用内置连接池是这样配置的
<dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource>
2. 查看 PooledDataSourceFactory 源码发现 是继承 UnpooledDataSourceFactory 类,UnpooledDataSourceFactory类是实现 DataSourceFactory接口的
public class PooledDataSourceFactory extends UnpooledDataSourceFactory { public PooledDataSourceFactory() { this.dataSource = new PooledDataSource(); } }
3. 所以mybatis如果需要使用外部的连接池,需要继承 UnpooledDataSourceFactory 或者实现 DataSourceFactory
public class DBCP2DataSourceFactory extends UnpooledDataSourceFactory { public DataSource getDataSource() { return new BasicDataSource(); } }
然后在mybatis-config.xml中type需要配置自定类的路径
<dataSource type="com.applications.memobook.db.DBCP2DataSourceFactory"> <property name="driver" value="${driverClassName}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource>