jeesite简介
JeeSite是基于多个优秀的开源项目,高度整合封装而成的高效,高性能,强安全性的 开源 Java EE快速开发平台。 JeeSite本身是以Spring Framework为核心容器,Spring MVC为模型视图控制器,MyBatis为数据访问层, Apache Shiro为权限授权层,Ehcahe对常用数据进行缓存,Activit为工作流引擎。
JeeSite主要定位于企业信息化领域,
最近在使用jeesite进行项目开发,真实项目中取其他项目的数据,使用多个数据源的情况比较多,本次有引用多个数据源的情况,做了多数据源的配置如下:
jeesite.properties 添加数据源信息,(url,username,pawwword)
使用 BoneCP 数据库连接池,网上说比P3OP快25倍左右。
#mysql database setting
jdbc.type=mysql
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://***.***.***.***:3306/fsapp_erp?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=****
#另一个数据源 database setting
jdbc2.type=mysql
jdbc2.driver=com.mysql.jdbc.Driver
jdbc2.url=jdbc:mysql://***.***.***.***:3306/fsapp_tyre?useUnicode=true&characterEncoding=utf-8
jdbc2.username=root
jdbc2.password=****
配置连接池大小
#pool settings 配置初始化大小、最小、最大
jdbc.pool.init=1
jdbc.pool.minIdle=3
jdbc.pool.maxActive=20
修改spring-context.xml配置文件,3处需要修改/添加
第一处数据源配置
第二处数据源配置,新增,和第一个一样配置
配置动态数据源,同时指定默认数据源
spring-context.xml:sqlSessionFactory修改为dynamicDataSource
事务切换,修改为dynamicDataSource
配置完成,修改java文件引用数据源
添加java文件,DynamicDataSource.java
package com.its.common.db;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* Mysql 多数据源切换
*
* @version V1.0
* @Description:
* @date 2015/10/09
*/
public class DynamicDataSource extends AbstractRoutingDataSource
{
private static final ThreadLocal contextHolder = new ThreadLocal();
/*
* (non-Javadoc)
*
* @see
* org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource#
* determineCurrentLookupKey()
*/
@Override
protected Object determineCurrentLookupKey() {
return getCurrentLookupKey();
}
/**
*
* @author sa
* @date 2012-5-18 下午4:06:44
* @return the currentLookupKey
*/
public static String getCurrentLookupKey() {
return (String) contextHolder.get();
}
/**
*
* @author sa
* @date 2012-5-18 下午4:06:44
* @param currentLookupKey
* the currentLookupKey to set
*/
public static void setCurrentLookupKey(String currentLookupKey) {
contextHolder.set(currentLookupKey);
}
}
在Controller中切换controle 中应用
Tyre entity = null;
if (StringUtils.isNotBlank(tyreNo)){
//切换数据源dataSource2,默认数据源dataSource
DynamicDataSource.setCurrentLookupKey("dataSource2");
entity = tyreService.getByNo(tyreNo);
}
DynamicDataSource.setCurrentLookupKey("dataSource");
完成。
注意切换完数据源,使用非默认的数据源的时候,使用完成要切换回来,否则有可能会发生错误。在其他services层切换的没有使用成功,先这样使用。