1.配置多数据源连接信息(jdbc.properties)
- jdbc.properties
# oracle 连接信息
jdbc.oracle.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.oracle.url=jdbc:oracle:thin:@127.0.0.1:6694/{database}
jdbc.oracle.username={userName}
jdbc.oracle.password={passwork}
# mysql 连接信息
jdbc.mysql.driver=com.mysql.jdbc.Driver
jdbc.mysql.url=jdbc:mysql://127.0.0.1:3306/{database}
jdbc.mysql.username={userName}
jdbc.mysql.password={passwork}
2.配置xml文件(springmvc.xml)
#oracle
<bean id="oracleDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
#mysql
<bean id="mysqlDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close">
<property name="driverClassName" value="${jdbc.mysql.driver}"/>
<property name="url" value="${jdbc.mysql.url}"/>
<property name="username" value="${jdbc.mysql.username}"/>
<property name="password" value="${jdbc.mysql.password}"/>
</bean>
#自定义动态数据源实体配置
<bean id="dataSource" class="cn.xdf.amoy.datasource.DynamicDataSource"><!--注意: 这里写选择数据源的类地址 下面跟着给出-->
<property name="defaultTargetDataSource" ref="oracleDataSource"/><!-- 设置默认为此mySqlDataSource数据源-->
<property name="targetDataSources">
<map>
<entry key="oracleDataSource" value-ref="oracleDataSource"/>
<entry key="mysqlDataSource" value-ref="mysqlDataSource"/>
</map>
</property>
</bean>
3.创建动态数据源配置类
package cn.cxqnet.datasource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* @author MyIreland
* @description 动态数据源实体
* @date 2018/10/19
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
public static final String ORACLE_DATASOURCE = "oracleDataSource";
public static final String MY_SQL_DATASOURCE = "mysqlDataSource";
//本地线程,获取当前正在执行的currentThread
public static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setCurrentDatasource(String dataSourceType){
contextHolder.set(dataSourceType);
}
public static String getCurrentDatasource(){
return contextHolder.get();
}
public static void clearCurrentDatasource(){
contextHolder.remove();
}
@Override
protected Object determineCurrentLookupKey() {
return getCurrentDatasource();
}
}
4.创建数据源注解
package cn.cxqnet.datasource.annotation;
import java.lang.annotation.*;
/**
* @author MyIreland
* @description 动态数据源注解
* @date 2018/10/19
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
String value() default "";
}
5.创建动态数据源切面(注解方式)
package cn.cxqnet.datasource.aspect;
import cn.cxqnet.datasource.DynamicDataSource;
import cn.cxqnet.datasource.annotation.DataSource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* @author MyIreland
* @description 动态数据源切面
* @date 2018/10/19
*/
@Aspect
@Component
public class DataSourceAspect {
protected Logger logger = LoggerFactory.getLogger(getClass());
@Pointcut("@annotation(cn.xdf.amoy.datasource.annotation.DataSource)")
public void dataSourcePointCut() {
}
@Around("dataSourcePointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
DataSource ds = method.getAnnotation(DataSource.class);
if(ds == null){
DynamicDataSource.setCurrentDatasource(DynamicDataSource.ORACLE_DATASOURCE);
logger.debug("set datasource is {}", DynamicDataSource.ORACLE_DATASOURCE);
}else {
DynamicDataSource.setCurrentDatasource(DynamicDataSource.MY_SQL_DATASOURCE);
logger.debug("set datasource is {}", DynamicDataSource.MY_SQL_DATASOURCE);
}
try {
return point.proceed();
} finally {
DynamicDataSource.clearCurrentDatasource();
logger.debug("clean datasource");
}
}
}
6.使用方式
在serviceImpl的方法里面添加@DataSource注解即可
@Override
@DataSource("{数据库名字}")
public Object save( CmdbAsset asset ) {
// 保存设备时,需要执行一步对设备进行编码的操作。
String something = dao.doSomething();
}
网友评论