Spring Boot实现动态数据源
2019-05-06 本文已影响0人
Xiewb
应用场景
- 当应用程序需要在不同的场景下访问不同的数据库时,可以用动态数据源来实现。
- 实现方式:自定义注解 + AOP
配置多个数据源信息
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
定义数据源名称常量枚举
public enum DataSourceEnum {
MYSQL_DS, ORACLE_DS
}
定义动态数据源注解
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实现
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();
}
}
定义动态数据源
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)
- 将该注解贴于具体场景service实现类的方法上
- 括号里的值即是需要使用到的具体数据库的名称(在枚举中定义的常量)