集成iBATIS3和bonecp

bonecp是一款开源的、高效的数据库连接池组件,它号称是现在最快的连接池组件,官网上称是dbcp的25倍,但是iBATIS3并没有为它开发类厂,iBATIS3只支持3中类型的类厂,分别是UNPOOLED,POOLED和JNDI,要想集成只能自己开发了,自己开发也不难,只要实现DataSourceFactory接口就可以了,代码很简单:

 

package com.ibatis.factory;

import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.datasource.DataSourceFactory;
import org.apache.ibatis.reflection.MetaObject;
import com.jolbox.bonecp.BoneCPDataSource;
public class BoneCPFactory implements DataSourceFactory {

private DataSource dataSource;
public BoneCPFactory()
{
// 创建bonecp datasource
dataSource = new BoneCPDataSource();

}
public DataSource getDataSource() {
// TODO Auto-generated method stub
return dataSource;
}
/**
* 配置属性
*/
public void setProperties(Properties properties) {

// TODO Auto-generated method stub
Properties driverProperties = new Properties();
MetaObject metaDataSource = MetaObject.forObject(dataSource);

for (Object key : properties.keySet()) {
String propertyName = (String) key;
// 处理driver.xxx属性,e.g driver.encoding=UTF8
if (propertyName.startsWith(DRIVER_PROPERTY_PREFIX)) {
String value = properties.getProperty(propertyName);
driverProperties.setProperty(propertyName.substring(DRIVER_PROPERTY_PREFIX_LENGTH), value);
}
else
{
// 利用反射技术,初始化datasource属性的值
if (metaDataSource.hasSetter(propertyName)) {
String value = (String) properties.get(propertyName);
Object convertedValue = convertValue(metaDataSource, propertyName, value);
metaDataSource.setValue(propertyName, convertedValue);
}
}
}
// 如果配置driver.xxx属性,初始化
if (driverProperties.size() > 0) {
metaDataSource.setValue("driverProperties", driverProperties);
}
}
/**
* 转换数字和Boolean型
* @param metaDataSource
* @param propertyName
* @param value
* @return
*/
@SuppressWarnings("unchecked")
private Object convertValue(MetaObject metaDataSource, String propertyName,
String value) {
Object convertedValue = value;
Class targetType = metaDataSource.getSetterType(propertyName);
if (targetType == Integer.class || targetType == int.class) {
convertedValue = Integer.valueOf(value);
} else if (targetType == Long.class || targetType == long.class) {
convertedValue = Long.valueOf(value);
} else if (targetType == Boolean.class || targetType == boolean.class) {
convertedValue = Boolean.valueOf(value);
}
return convertedValue;
}
private static final String DRIVER_PROPERTY_PREFIX = "driver.";
private static final int DRIVER_PROPERTY_PREFIX_LENGTH = DRIVER_PROPERTY_PREFIX.length();
}

 

然后在Configuration.xml配置就可以了,首先配置类厂别名:

 

<typeAliases>
<typeAlias type="com.ibatis.factory.BoneCPFactory" alias="BONECP" />
</typeAliases>

 

接着配置environmen:

 

<environments default="defaultEV">
<environment id="defaultEV">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="BONECP">
<property name="driverClass" value="${driver}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bone" />
<property name="idleMaxAge" value="10" />
<property name="partitionCount" value="1" />
<property name="maxConnectionsPerPartition" value="5" />
<property name="minConnectionsPerPartition" value="1" />
<property name="driver.encoding" value="UTF8" />
</dataSource>
</environment>
</environments>

 

需要配置的属性很简单,就是BoneCPDataSource类的属性。如此就大功告成了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值