集成iBATIS3和bonecp
来源:互联网 发布:冷藏室软件下载 编辑:程序博客网 时间:2024/06/16 00:34
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类的属性。如此就大功告成了。
- 集成iBATIS3和bonecp
- ibatis3
- ibatis2 和 ibatis3的问题
- ibatis3中的一对多和一对一的实现
- iBatis3尝鲜
- Java数据库连接池比较(c3p0,dbcp,proxool和BoneCP)
- ibatis3整合spring
- ibatis3 配置文件实例
- Ibatis3 mapper配置文件实例
- Ibatis3入门学习
- ibatis3整合spring
- iBatis3架构分析
- iBatis3架构分析
- ibatis3.0配置
- ibatis3的下载地址
- 测试ibatis3连接数据
- DBCP、C3P0、Proxool 、 BoneCP开源连接池的比较和属性说明
- DBCP与C3P0的核心区别,以及Proxool和BoneCP连接池介绍
- Superscalar
- Android之HelloWorld
- 在文本中找出重复的数据记录的批处理怎么写
- BusyBox reboot 流程分析
- Samba 3.5.5 发布
- 集成iBATIS3和bonecp
- 终于解决了在控制台不能输入中文输入法的问题了
- 微软IE9意外获得竞争对手Google赞赏
- 如何加速Oracle大批量数据处理
- Mozilla Firefox 4.0 Beta 6 RC发布
- 如何在VMWare Workstation虚拟机中安装Mac OS(整理)
- 最牛B的大学生!~别笑死就行
- 开源模式让木马软件更疯狂
- 运用Server.URLEncode函数要注意