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类的属性。如此就大功告成了。
相关资源:Office2016专业增强版中文免费正式版(附安装教程)64位