本篇介绍的是Mybatis集成C3P0数据源的方法,思路就是直接继承UnpooledDataSourceFactory类然后给dataSource赋值(这里C3P0当然就是是ComboPooledDataSource),
然后在配置文件中指定该数据源<dataSource type="com.mybatis.bean.C3P0DataSourceFactory">,具体看下面代码:
<!-- c3p0连接池 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency>
package com.dhcc.imessage.json; import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3P0DataSourceFactory extends UnpooledDataSourceFactory { public C3P0DataSourceFactory() { this.dataSource = new ComboPooledDataSource(); } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias alias="BlogCategory" type="com.mybatis.bean.BlogCategory" /> <typeAlias alias="BlogArticle" type="com.mybatis.bean.BlogArticle" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="com.mybatis.bean.C3P0DataSourceFactory"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/study" /> <property name="user" value="root" /> <property name="password" value="abcde2008" /> <property name="idleConnectionTestPeriod" value="60" /> <property name="maxPoolSize" value="20" /> <property name="maxIdleTime" value="600" /> <property name="preferredTestQuery" value="SELECT 1" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/mybatis/mapper/BlogCategoryMapper.xml" /> <mapper resource="com/mybatis/mapper/BlogArticleMapper.xml" /> </mappers> </configuration>
继续阅读
微信公众号
分享IT信息技术、北海生活的网站。提供北海本地化的信息技术服务。