日期:2014-05-16  浏览次数:20424 次

dbutils封装ORM 实现BaseDAO
Configurations.java

package com.dbutils.common;

import java.sql.*;

import java.util.*;
import java.lang.reflect.*;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* 数据库管理
*
*   */
@SuppressWarnings("unchecked")
public class Configurations {

private final static Log logger = LogFactory.getLog(Configurations.class);
private final static ThreadLocal<Connection> conns = new ThreadLocal<Connection>();
private static DataSource dataSource;
private static boolean show_sql = true;
private static BasicDataSource dbcpDataSource = null;
private static ResourceBundle res = null;
static {
initDataSource(null);
}

/**
* 初始化连接池
*
* @param props
* @param show_sql
*/
private final static void initDataSource(Properties dbProperties) {
try {
// 配置dbcp数据源

if (dbcpDataSource == null) {
dbcpDataSource = new BasicDataSource();
res = ResourceBundle.getBundle("oracleConfig");
}
String driverClassName = res.getString("driverClassName");
int initialSize = Integer.valueOf(res.getString("initialSize"));
int maxActive = Integer.valueOf(res.getString("maxActive"));
int maxIdle = Integer.valueOf(res.getString("maxIdle"));
int minIdle = Integer.valueOf(res.getString("minIdle"));
int maxWait = Integer.valueOf(res.getString("maxWait"));
Boolean logAbandoned = Boolean.valueOf(res
.getString("logAbandoned"));
Boolean removeAbandoned = Boolean.valueOf(res
.getString("removeAbandoned"));
int removeAbandonedTimeout = Integer.valueOf(res
.getString("removeAbandonedTimeout"));
Boolean testWhileIdle = Boolean.valueOf(res
.getString("testWhileIdle"));
Boolean testOnBorrow = Boolean.valueOf(res
.getString("testOnBorrow"));
Boolean testOnReturn = Boolean.valueOf(res
.getString("testOnReturn"));
int timeBetweenEvictionRunsMillis = Integer.valueOf(res
.getString("timeBetweenEvictionRunsMillis"));
int numTestsPerEvictionRun = Integer.valueOf(res
.getString("numTestsPerEvictionRun"));
int minEvictableIdleTimeMillis = Integer.valueOf(res
.getString("minEvictableIdleTimeMillis"));
String validationQuery = res.getString("validationQuery");
String url = res.getString("oracleurl");
String userName = res.getString("oracleuser");
String password = res.getString("oraclepassword");
if ("show_sql".equalsIgnoreCase("true")) {
// show_sql =
// "true".equalsIgnoreCase(dbProperties.getProperty(skey));
// dbcpDataSource.
}
dbcpDataSource.setUrl(url);
dbcpDataSource.setDriverClassName(driverClassName);
dbcpDataSource.setUsername(userName);
dbcpDataSource.setPassword(password);
dbcpDataSource.setDefaultAutoCommit(true);
dbcpDataSource.setInitialSize(initialSize);
dbcpDataSource.setMaxActive(maxActive);
dbcpDataSource.setMinIdle(minIdle);
dbcpDataSource.setMaxIdle(maxIdle);
dbcpDataSource.setMaxWait(maxWait);
dbcpDataSource.setLogAbandoned(logAbandoned);
dbcpDataSource.setRemoveAbandoned(removeAbandoned);
dbcpDataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
dbcpDataSource.setTestOnBorrow(testOnBorrow);
dbcpDataSource.setTestOnReturn(testOnReturn);
dbcpDataSource.setTestWhileIdle(testWhileIdle);
dbcpDataSource
.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
dbcpDataSource.setNumTestsPerEvictionRun(numT