最近开发项目使用Ibatis+Lucene,使用轻量框架实例自己管理,不使用Spring,Ibatis数据源原来使用JNDI,为统一配置加入dbconfig.properties,并使用boneCP,Ibatis2数据源只提供JNDI、SIMPLE、DBCP,无法对BoneCP提供支持,故参考DbcpDataSourceFactory自己创建工厂类,发布博客,以作备份.
dbconfig.properties
###============搜索数据库配置=============##
search.db.url=jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST =*)(PORT = 1521)))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = wcsdb)))
search.db.username=xiu_search
search.db.password=*
##bonecp
##每60秒检查所有连接池中的空闲连接
search.db.idleConnectionTestPeriod=20
##设置连接空闲时间
search.db.idleMaxAge=20
##设置连接池在每个分区中的最大连接数
search.db.maxConnectionsPerPartition=15
##设置连接池设在每个分区中的最小连接数
search.db.minConnectionsPerPartition=10
##设置分区(设置 2个分区)
search.db.partitionCount=2
##连接池中的连接耗尽的时候 BoneCP一次同时获取的连接数
search.db.acquireIncrement=5
##每个分区释放链接助理进程的数量,默认值:3,除非你的一个数据库连接的时间内做了很多工作,不然过多的助理进程会影响你的性能
search.db.releaseHelperThreads=3
search.db.statementsCachedPerConnection=100
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<properties resource="dbconfig.properties"/>
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" maxRequests="10" maxSessions="20"
maxTransactions="50" useStatementNamespaces="true"
defaultStatementTimeout="30" statementCachingEnabled="true"
classInfoCacheEnabled="true" errorTracingEnabled="true" />
<typeAlias alias="BONECP" type="com.pltfm.sys.util.BoneCPDataSourceFactory"/>
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="BONECP">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<property name="jdbcUrl" value="${search.db.url}" />
<property name="username" value="${search.db.username}" />
<property name="password" value="${search.db.password}" />
<property name="idleMaxAge" value="${search.db.idleMaxAge}" />
<property name="partitionCount" value="${search.db.partitionCount}" />
<property name="maxConnectionsPerPartition" value="${search.db.maxConnectionsPerPartition}" />
<property name="minConnectionsPerPartition" value="${search.db.minConnectionsPerPartition}" />
<property name="driver.encoding" value="UTF8" />
<property name="Driver.releaseHelperThreads" value="${search.db.releaseHelperThreads}" />
<property name="Driver.statementsCachedPerConnection" value="${search.db.statementsCachedPerConnection}" />
</dataSource>
</transactionManager>
<sqlMap resource="com/pltfm/sys/sqlmap/sys_param_SqlMap.xml" />
</sqlMapConfig>
package com.pltfm.sys.util;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.sql.DataSource;
import com.ibatis.common.beans.Probe;
import com.ibatis.common.beans.ProbeFactory;
import com.ibatis.sqlmap.engine.datasource.DataSourceFactory;
import com.jolbox.bonecp.BoneCPDataSource;
/**
* <h1>支持Ibatis2 BoneCP连接池工厂</h2><br/>
* <h2>默认提供参数如下:</h2><br/>
* #driverClass<br/>
* #jdbcUrl<br/>
* #username<br/>
* #password<br/>
* #每60秒检查所有连接池中的空闲连接
* idleConnectionTestPeriod=20<br/>
* ##设置连接空闲时间<br/>
* idleMaxAge=20<br/>
* ##设置连接池在每个分区中的最大连接数<br/>
* maxConnectionsPerPartition=15<br/>
* ##设置连接池设在每个分区中的最小连接数<br/>
* minConnectionsPerPartition=10<br/>
* ##设置分区(设置 2个分区)<br/>
* partitionCount=2<br/>
* ##连接池中的连接耗尽的时候 BoneCP一次同时获取的连接数<br/>
* acquireIncrement=5<br/>
* <span style="color:red">其他自定义参数需以Driver.打头才可注入</span>
* @see com.ibatis.sqlmap.engine.datasource.DbcpDataSourceFactory
* @author Lipsion
*
*/
public class BoneCPDataSourceFactory implements DataSourceFactory {
private static final Probe PROBE = ProbeFactory.getProbe();
private static final String ADD_DRIVER_PROPS_PREFIX = "Driver.";
private static final int ADD_DRIVER_PROPS_PREFIX_LENGTH = ADD_DRIVER_PROPS_PREFIX
.length();
private DataSource dataSource;
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void initialize(Map map) {
try {
dataSource = legacyBoneCPConfiguration(map);
if (dataSource == null) {
dataSource = newBoneCPConfiguration(map);
}
} catch (Exception e) {
throw new RuntimeException(
"Error initializing BoneCPDataSourceFactory. Cause: " + e,
e);
}
}
public DataSource getDataSource() {
return dataSource;
}
private BoneCPDataSource legacyBoneCPConfiguration(Map<String, String> map) {
BoneCPDataSource dataSource = null;
if (map.containsKey("driverClass")) {
dataSource = new BoneCPDataSource();
String driver = map.get("driverClass");
String url = map.get("jdbcUrl");
String username = map.get("username");
String password = map.get("password");
String validationQuery = map.get("connectionTestStatement");
// 最大连接数
String maxActive = map.get("maxConnectionsPerPartition");
// 最大空闲数(最小连接数)
String maxIdle = map.get("minConnectionsPerPartition");
String maxWait = map.get("idleMaxAge");
// 连接池中的连接耗尽的时候 BoneCP一次同时获取的连接数 ,默认为2
String acquireIncrement = map.get("acquireIncrement");
if (!notEmpty(url)) {
throw new RuntimeException(
"Error initializing configuration. cause: jdbcUrl is empty");
}
if (!isNumeric(maxActive)) {
throw new RuntimeException(
"Error initializing configuration. cause: maxConnectionsPerPartition is not numeric");
}
if (!isNumeric(maxIdle)) {
throw new RuntimeException(
"Error initializing configuration. cause: minConnectionsPerPartition is not numeric");
}
if (!isNumeric(maxIdle)) {
throw new RuntimeException(
"Error initializing configuration. cause: idleMaxAge is not numeric");
}
if (!isNumeric(maxWait)) {
throw new RuntimeException(
"Error initializing configuration. cause: idleMaxAge is not numeric");
}
//可选项
if (notEmpty(acquireIncrement)){
dataSource.setAcquireIncrement(Integer
.parseInt(acquireIncrement));
}
dataSource.setJdbcUrl(url);
dataSource.setDriverClass(driver);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setConnectionTestStatement(validationQuery);
dataSource.setMaxConnectionsPerPartition(Integer
.parseInt(maxActive));
dataSource.setMinConnectionsPerPartition(Integer.parseInt(maxIdle));
dataSource.setMaxConnectionAge(Long.parseLong(maxWait),
TimeUnit.SECONDS);
Iterator<String> props = map.keySet().iterator();
while (props.hasNext()) {
String propertyName = (String) props.next();
if (propertyName.startsWith(ADD_DRIVER_PROPS_PREFIX)) {
String value = (String) map.get(propertyName);
// 映射返回对应的类型
Object convertedValue = convertValue(dataSource,
propertyName.substring(ADD_DRIVER_PROPS_PREFIX_LENGTH), value);
PROBE.setObject(dataSource, propertyName.substring(ADD_DRIVER_PROPS_PREFIX_LENGTH), convertedValue);
}
}
}
return dataSource;
}
private BoneCPDataSource newBoneCPConfiguration(Map<String, String> map) {
BoneCPDataSource dataSource = null;
Iterator<String> props = map.keySet().iterator();
while (props.hasNext()) {
String propertyName = (String) props.next();
if (propertyName.startsWith(ADD_DRIVER_PROPS_PREFIX)) {
String value = (String) map.get(propertyName);
PROBE.setObject(dataSource,
propertyName.substring(ADD_DRIVER_PROPS_PREFIX_LENGTH),
value);
} else if (PROBE.hasWritableProperty(dataSource, propertyName)) {
String value = (String) map.get(propertyName);
Object convertedValue = convertValue(dataSource, propertyName,
value);
PROBE.setObject(dataSource, propertyName, convertedValue);
}
}
return dataSource;
}
private boolean notEmpty(String s) {
return s != null && s.length() > 0;
}
private boolean isNumeric(String str) {
//如果为空直接返回验证不通过
if(null==str||"".equals(str)){
return false;
}
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(str);
return m.matches();
}
@SuppressWarnings("rawtypes")
private Object convertValue(Object object, String propertyName, String value) {
Object convertedValue = value;
Class targetType = PROBE.getPropertyTypeForSetter(object, 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;
}
}
分享到:
相关推荐
5. 整合过程:通常,整合这三大框架需要配置Struts2的struts.xml、Spring的applicationContext.xml以及iBatis的sqlMapConfig.xml等文件。还需要在Struts2的Action类中注入Spring管理的bean,以便于调用业务服务。...
在本项目中,"springboot+mysql+ibatis完整整合案例"是一个针对初学者的教程,旨在演示如何将Spring Boot、MySQL数据库和MyBatis框架有效地集成在一起,创建一个可运行的应用程序。以下是对这些技术及其整合过程的...
### Spring对IBatis的整合 #### 一、Spring与IBatis整合概述 Spring框架与IBatis(现称为MyBatis)的整合为开发者提供了一种更简洁、更强大的数据库访问方式。Spring通过其内置的支持机制极大地简化了原有的IBatis...
在"Struts2+Spring+iBatis整合的小例子"中,开发者通常会做以下工作: 1. **环境配置**:首先,确保JDK、Tomcat、MySQL等基础环境的安装和配置。然后,将Struts2、Spring、iBatis的相关jar包添加到项目的类路径中。...
总的来说,Spring、Struts2和iBatis的整合为Java Web开发提供了一个强大、灵活的解决方案,让开发者能够更专注于业务逻辑,而不是框架的底层实现。通过合理的配置和使用这个jar包,开发者可以快速构建出稳定、高性能...
在这个“struts2+spring3+ibatis项目整合案例”中,我们将深入探讨这三个框架如何相互配合,实现项目的集成。 Struts2作为MVC(Model-View-Controller)架构的实现,主要负责处理用户请求,控制应用的流程。它提供...
由于最大上传被限制在 20M,所以只能分开上传。 分为:struts2.1.8 + spring 2.5 + ibatis 2 整合开发包_ _01部分 ...只要将这两个包全下载下来,就可以搭建struts2.1.8 + spring 2.5 + ibatis2整合开发的 包环境。
由于最大上传被限制在 20M,所以只能分开上传。 分为:struts2.1.8 + spring 2.5 + ibatis 2 整合开发包_ _01部分 ...只要将这两个包全下载下来,就可以搭建struts2.1.8 + spring 2.5 + ibatis2整合开发的 包环境。
8. 整合测试:编写测试类,通过@Autowired注解注入DAO接口,进行数据库操作的测试,确保Spring和iBatis的整合成功。 通过以上步骤,Spring和iBatis的整合就完成了。这种整合方式允许开发者充分利用Spring的高级特性...
Spring 能够很好地整合 Struts 和 iBatis,通过 Spring 的 AOP 特性,可以方便地管理 Struts 的 Action 类以及 iBatis 的 SqlSessionFactory 和 SqlSession,同时 Spring 还能管理 BoneCP 数据库连接池,提高数据库...
总的来说,这个项目整合了Struts2、Spring和iBatis,形成了一套强大的Java Web开发解决方案。它展示了如何将MVC框架、业务管理和数据访问层有效结合,提供了一种灵活、可扩展的架构,便于开发人员进行复杂的企业级...
**Spring、Struts2 和 iBatis 整合详解** 在Java Web开发中,Spring、Struts2 和 iBatis 是三个非常重要的框架。它们分别负责不同层面的任务:Spring 提供了全面的依赖注入(DI)和面向切面编程(AOP),用于管理...
通过Struts2、Spring和iBatis的整合,我们可以实现模型、视图和控制的分离,提高代码的可读性和可维护性。同时,Spring的DI和AOP特性使得对象管理更加灵活,iBatis则提供了方便的数据库操作方式。这样的组合在企业级...
通过以上对Struts2和iBatis的介绍以及整合过程的讲解,我们可以理解这两个框架如何协同工作,提高开发效率并提供更优秀的Web应用程序。在实际项目中,开发者可以根据需求选择合适的整合策略,以实现高效、可维护的...
Spring iBatis WebWork 整合
在"struts2 spring ibatis2.3整合"中,我们将看到如何将这三个框架结合在一起,实现一个完整的Java Web应用架构。首先,Struts2和Spring的整合主要是通过Struts2的Spring插件来完成的,这个插件允许我们在Struts2的...
"Struts2+Spring+iBatis整合"是一个典型的MVC(Model-View-Controller)架构实现,适用于构建大型企业级应用。下面将详细介绍这三个框架以及它们整合的关键点。 **Struts2** 是一个基于MVC设计模式的Web应用框架,...
struts2、spring、ibatis整合实例 struts2、spring、ibatis整合实例 struts2、spring、ibatis整合实例 struts2、spring、ibatis整合实例
2. iBatis的SQL映射机制和动态SQL功能。 3. 如何在Struts1的Action中调用iBatis执行数据库操作。 4. ActionForm在请求数据传递中的作用。 5. 整合后的项目结构,包括配置文件(struts-config.xml和SqlMapConfig.xml...
iBatis和Spring整合 iBatis和Spring整合