【持久化框架MyBatis3六】MyBatis3集成第三方DataSource

MyBatis内置了数据源的支持,如:

 

	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${jdbc.driverClassName}" />
				<property name="url" value="${jdbc.url}" />
				<property name="username" value="${jdbc.username}" />
				<property name="password" value="${jdbc.password}" />
			</dataSource>
		</environment>

 type为POOLED的数据源是MyBatis内置的数据源类型(它是一个类型别名)。POOLED对应的数据库连接池工厂是org.apache.ibatis.datasource.pooled.PooledDataSourceFactory,它必须实现接口 org.apache.ibatis.datasource.DataSourceFactory

 

package org.apache.ibatis.datasource.pooled;

import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;

public class PooledDataSourceFactory extends UnpooledDataSourceFactory {

  public PooledDataSourceFactory() {
    this.dataSource = new PooledDataSource();
  }

}

 

目前有各种各样的数据源实现,如果在项目中要使用第三方的数据源,如何将它配置到MyBatis中呢?通过实现 org.apache.ibatis.datasource.DataSourceFactory将数据源集成到MyBatis中。本文以Druid数据源为例。

 

 实现DataSourceFactory接口

 

package com.mybatis3.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.datasource.DataSourceFactory;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;

public class DruidDataSourceFactory implements DataSourceFactory {
    private  Properties props;
    @Override
    public void setProperties(Properties props) {
        this.props = props;
    }

    @Override
    public DataSource getDataSource() {
        DruidDataSource dds =  new DruidDataSource();
        dds.setUrl(this.props.getProperty("url"));
        dds.setPassword(this.props.getProperty("password"));
        dds.setUsername(this.props.getProperty("username"));
        dds.setDriverClassName("com.mysql.jdbc.Driver");
        //其他配置可以根据MyBatis主配置文件进行配置
        try {
            dds.init();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dds;
    }
}

 

 

 

主配置文件配置:

1. DRUID类型别名:

 

    <typeAliases>
        <typeAlias type="com.mybatis3.datasource.DruidDataSourceFactory" alias="DRUID"/>
   </typeAliases>

 

2. 数据源配置

 

<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="DRUID">
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
                <property name="username" value="root" />
                <property name="password" value="root" />

                <property name="maxActive" value="20" />
                <property name="initialSize" value="1" />
                <property name="maxWait" value="60000" />
                <property name="minIdle" value="1" />

                <property name="timeBetweenEvictionRunsMillis" value="3000" />
                <property name="minEvictableIdleTimeMillis" value="300000" />

                <property name="validationQuery" value="SELECT 'x' FROM DUAL" />
                <property name="testWhileIdle" value="true" />
                <property name="testOnBorrow" value="false" />
                <property name="testOnReturn" value="false" />
                <!-- mysql 不支持 poolPreparedStatements-->
                <!--<property name="poolPreparedStatements" value="true" />-->
                <!--<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />-->

                <!-- 开启Druid的监控统计功能 -->
                <property name="filters" value="stat" />
			</dataSource>
		</environment>

  

 

 

 

 

你可能感兴趣的:(dataSource)