Mybatis使用C3P0数据源

连线北海
连线北海
管理员
201
文章
评论字数 345阅读1分9秒

Mybatis使用C3P0数据源-图片1本篇介绍的是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>

Mybatis使用C3P0数据源-图片2

继续阅读
weinxin
微信公众号
分享IT信息技术、北海生活的网站。提供北海本地化的信息技术服务。
 最后更新:2019-5-7
连线北海
  • 本文由 连线北海 发表于2018年9月4日 11:08:32
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定