MyBatis在非Spring环境下第三方DataSource设置

2016/12/15 20:26
阅读数 181

MyBatis在非Spring环境下第三方DataSource设置

 

Spring环境下, MyBatis可以通过其本身的增强mybatis-spring提供的org.mybatis.spring.SqlSessionFactoryBean来注入第三方DataSource.

但是在非Spring的环境下, MyBatis的加载需要配置其自身的configuration xml文件,

代码段可能是下面这样的:

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

 其中type属性, 只有三个值可以选择, UNPOOLED, POOLED, 和 JNDI

事实上DataSource标签的属性Type注入的不是DataSource对象, 而是DataSourceFactory对象, 所以通过对Type进行DataSource的注入是错误的.

我们首先需要建一个自己的DataSourceFactory类, 用来封装第三方DataSource, 实现代码(以Druid为例)是这样:

package com.java.mybatis.datasource;

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

import com.alibaba.druid.pool.DruidDataSource;

public class DruidDataSourceFactory extends UnpooledDataSourceFactory {
	public DruidDataSourceFactory() {
		this.dataSource = new DruidDataSource();
	}

}

然后就可以在config文件里面注入这个自定义的DataSourceFactory了,

<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="com.java.mybatis.datasource.DruidDataSourceFactory">
				<!--<property name="driverClassName" value="org.h2.Driver" />-->
				<property name="url" value="jdbc:h2:tcp://localhost/C:/java/h2/data/test;MODE=MYSQL;MVCC=TRUE;DB_CLOSE_DELAY=-1;" />
				<property name="username" value="admin" />
				<property name="password" value="admin" />
				<property name="validationQuery" value="select now()" />
				<property name="testWhileIdle" value="true" />
				<property name="testOnBorrow" value="false" />
				<property name="testOnReturn" value="false" />							
		<!--<property name="filters" value="slf4j" />-->
				<property name="poolPreparedStatements" value="false" />
				<property name="maxOpenPreparedStatements" value="-1" />
			</dataSource>
		</environment>
	</environments>

 

附阿里数据源常用配置

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
    <property name="url" value="${jdbc_url}" />
    <property name="username" value="${jdbc_user}" />
    <property name="password" value="${jdbc_password}" />
     
    <property name="filters" value="stat" />
 
    <property name="maxActive" value="20" />
    <property name="initialSize" value="1" />
    <property name="maxWait" value="60000" />
    <property name="minIdle" value="1" />
 
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <property name="minEvictableIdleTimeMillis" value="300000" />
 
    <property name="validationQuery" value="SELECT 'x'" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
     
    <property name="poolPreparedStatements" value="true" />
    <property name="maxPoolPreparedStatementPerConnectionSize" value="50" />
</bean>

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部