1.配置多數(shù)據(jù)源連接信息(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>
#自定義動(dòng)態(tài)數(shù)據(jù)源實(shí)體配置
<bean id="dataSource" class="cn.xdf.amoy.datasource.DynamicDataSource"><!--注意: 這里寫選擇數(shù)據(jù)源的類地址 下面跟著給出-->
<property name="defaultTargetDataSource" ref="oracleDataSource"/><!-- 設(shè)置默認(rèn)為此mySqlDataSource數(shù)據(jù)源-->
<property name="targetDataSources">
<map>
<entry key="oracleDataSource" value-ref="oracleDataSource"/>
<entry key="mysqlDataSource" value-ref="mysqlDataSource"/>
</map>
</property>
</bean>
3.創(chuàng)建動(dòng)態(tài)數(shù)據(jù)源配置類
package cn.cxqnet.datasource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* @author MyIreland
* @description 動(dòng)態(tài)數(shù)據(jù)源實(shí)體
* @date 2018/10/19
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
public static final String ORACLE_DATASOURCE = "oracleDataSource";
public static final String MY_SQL_DATASOURCE = "mysqlDataSource";
//本地線程,獲取當(dāng)前正在執(zhí)行的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.創(chuàng)建數(shù)據(jù)源注解
package cn.cxqnet.datasource.annotation;
import java.lang.annotation.*;
/**
* @author MyIreland
* @description 動(dòng)態(tài)數(shù)據(jù)源注解
* @date 2018/10/19
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
String value() default "";
}
5.創(chuàng)建動(dòng)態(tài)數(shù)據(jù)源切面(注解方式)
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 動(dòng)態(tài)數(shù)據(jù)源切面
* @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("{數(shù)據(jù)庫(kù)名字}")
public Object save( CmdbAsset asset ) {
// 保存設(shè)備時(shí),需要執(zhí)行一步對(duì)設(shè)備進(jìn)行編碼的操作。
String something = dao.doSomething();
}