MyBatis 使用阿里的Druid连接池
druid
阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
项目地址:https://gitcode.com/gh_mirrors/druid/druid

·
MyBatis默认提供了一个数据库连接池PooledDataSource,一般我们只需要在配置文件中配置,然后就可以使用
<dataSource type="POOLED" >
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
如果想更换其他的数据库连接池,比如Druid,这里介绍一种方法。
只需要两步:
1、创建连接池工厂-----继承PooledDataSourceFactory
public class DruidDataSourceFactory extends PooledDataSourceFactory {
public DruidDataSourceFactory() {
this.dataSource = new DruidDataSource();
}
}
虽然Druid提供了一个同名的DruidDataSourceFactory类,但是不能在MyBatis中直接使用。
2、修改配置文件
<dataSource type="com.gyb.mybatis.common.DruidDataSourceFactory" >
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
注意:这里的 property配置需要根据你选择的数据库连接池的具体实现做调整, Druid需要改成上面的配置。因为MyBatis时通过调用数据源的具体实现类的属性的setter方法进行值注入的。查看DruidAbstractDataSource源码,你会发现有以下属性
protected volatile String username; protected volatile String password; protected volatile String jdbcUrl; protected volatile String driverClass;
并提供了setter方法。




阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
最近提交(Master分支:7 天前 )
81ebdebf
* fix test checkstyle
* fix testcase
* fix testcase
* fix checkstyle 1 天前
8917bb77 - 6 天前
更多推荐
所有评论(0)