應(yīng)用場(chǎng)景
- 當(dāng)應(yīng)用程序需要在不同的場(chǎng)景下訪問(wèn)不同的數(shù)據(jù)庫(kù)時(shí)嗡官,可以用動(dòng)態(tài)數(shù)據(jù)源來(lái)實(shí)現(xiàn)敞映。
- 實(shí)現(xiàn)方式:自定義注解 + AOP
配置多個(gè)數(shù)據(jù)源信息
jdbc1.driverClassName=com.mysql.jdbc.Driver
jdbc1.username=root
jdbc1.password=123456
jdbc1.url=jdbc:mysql://localhost:3306/db_1
jdbc2.driverClassName=com.mysql.jdbc.Driver
jdbc2.username=root
jdbc2.password=654321
jdbc2.url=jdbc:mysql://localhost:3306/db_2
定義數(shù)據(jù)源名稱常量枚舉
public enum DataSourceEnum {
MYSQL_DS, ORACLE_DS
}
定義動(dòng)態(tài)數(shù)據(jù)源注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DynamicDS {
DataSourceEnum value() default DataSourceEnum.MYSQL_DS;
}
定義DataSourceContextHolder
public class DataSourceContextHolder {
private static ThreadLocal<DataSourceEnum> context = new ThreadLocal<>();
public static void setDataSourceName(DataSourceEnum dsName){
context.set(dsName);
}
public static DataSourceEnum getDataSourceName(){
return context.get();
}
public static void clearDS(){
context.remove();
}
}
pom加入AOP的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
AOP實(shí)現(xiàn)
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Aspect
@Component
public class DynamicDataSourceAspect {
@Before("@annotation(DynamicDS)")
public void setDataSource(JoinPoint joinPoint)
throws NoSuchMethodException {
String methodName = joinPoint.getSignature().getName();
Class<?> clazz = joinPoint.getTarget().getClass();
Method method = clazz.getMethod(methodName,
((MethodSignature) joinPoint.getSignature()).getParameterTypes());
if (method.isAnnotationPresent(DynamicDS.class)) {
DataSourceContextHolder.setDataSourceName(
method.getAnnotation(DynamicDS.class).value());
}
}
@After("@annotation(DynamicDS)")
public void clearDataSource() {
DataSourceContextHolder.clearDS();
}
}
定義動(dòng)態(tài)數(shù)據(jù)源
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourceContextHolder.getDataSourceName();
}
}
Mybatis配置類
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@Configuration
@MapperScan(basePackages="com.**.dao")
@PropertySource("classpath:datasource.properties")
public class MybatisConfig implements EnvironmentAware {
@Primary
@Bean
public DataSource mysqlDS() throws Exception {
Properties prop = new Properties();
prop.setProperty("username",this.env.getProperty("jdbc1.username"));
prop.setProperty("password",this.env.getProperty("jdbc1.password"));
prop.setProperty("url",this.env.getProperty("jdbc1.url"));
prop.setProperty("driverClassName",this.env.getProperty("jdbc1.driverClassName"));
return DruidDataSourceFactory.createDataSource(prop);
}
@Bean
public DataSource oracleDS() throws Exception {
Properties prop = new Properties();
prop.setProperty("username",this.env.getProperty("jdbc2.username"));
prop.setProperty("password",this.env.getProperty("jdbc2.password"));
prop.setProperty("url",this.env.getProperty("jdbc2.url"));
prop.setProperty("driverClassName",this.env.getProperty("jdbc2.driverClassName"));
return DruidDataSourceFactory.createDataSource(prop);
}
@Bean
public DataSource dynamicDS(@Qualifier("mysqlDS") DataSource mysqlDS,
@Qualifier("oracleDS") DataSource oracleDS) {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
dynamicDataSource.setDefaultTargetDataSource(mysqlDS);
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceEnum.MYSQL_DS, mysqlDS);
targetDataSources.put(DataSourceEnum.ORACLE_DS, oracleDS);
dynamicDataSource.setTargetDataSources(targetDataSources);
dynamicDataSource.afterPropertiesSet();
return dynamicDataSource;
}
@Bean
public SqlSessionFactoryBean sqlSessionFactory(@Qualifier("dynamicDS") DataSource dynamicDS) throws IOException {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dynamicDS);
ResourcePatternResolver res = new PathMatchingResourcePatternResolver();
sqlSessionFactory.setMapperLocations(
res.getResources("classpath:com/**/dao/*.xml"));
return sqlSessionFactory;
}
private Environment env;
@Override
public void setEnvironment(Environment environment) {
this.env = environment;
}
}
具體使用
@DynamicDS(DataSourceEnum.MYSQL_DS)
- 將該注解貼于具體場(chǎng)景service實(shí)現(xiàn)類的方法上
- 括號(hào)里的值即是需要使用到的具體數(shù)據(jù)庫(kù)的名稱(在枚舉中定義的常量)