码迷,mamicode.com
首页 > 数据库 > 详细

springboot&数据库连接池&切面动态数据源

时间:2020-07-26 19:38:07      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:aspect   oid   amp   app   auto   cer   table   java   nec   

 

 

DateSource 定义注解
DateSourceContext 定义ThreadLocal上下文
DataSourceRouteAspect会拦截注解dataSource的方法,业务方法之前设置数据源,设置到ThreadLocal中
                     也可根据业务参数自主设置数据源,达到分库效果 
DruidConfig 配置多数据源
MybatisConfig 扫描sql配置类
RouteDataSource 通过继承AbstractRoutingDataSource重写,获取ThreadLocal中的数据源



@Target({ ElementType.METHOD,ElementType.TYPE,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
    String value() default  "";
}

public class DataSourceContext {
    private static final ThreadLocal<String> context = new ThreadLocal<>();
    public static void setDataSource(String value){
        context.set(value);
    }
    public static String getDataSource(){
        return context.get();
    }
    public static void clearDataSource(){
        context.remove();
    }
}

@Aspect
@Component
public class DataSourceRouteAspect {
    @Before("@annotation(dataSource)")
    public void selectDateSource(JoinPoint point, DataSource dataSource){
        String value = dataSource.value();
        DataSourceContext.setDataSource(value);
    }
    @After("@annotation(dataSource)")
    public void removeDataSource(JoinPoint point, DataSource dataSource){
        DataSourceContext.clearDataSource();
    }
}

@Configuration
public class DruidConfig {
//    @Value("${mysql.one.aliasName}")
//    private String aliasNameOne;
//    @Primary
    @Bean(name="dataSourceOne",initMethod = "init",destroyMethod = "close")
    @ConfigurationProperties(prefix = "mysql.one")
    public DruidDataSource dataSourceOne(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setQueryTimeout(300);
        dataSource.setTestWhileIdle(true);
        dataSource.setTestOnBorrow(true);
        dataSource.setTestOnReturn(true);
        dataSource.setTimeBetweenEvictionRunsMillis(600000);
        dataSource.setMinEvictableIdleTimeMillis(300000);
        return dataSource;
    }
//    @Bean(name="dataSourceTwo",initMethod = "init",destroyMethod = "close")
//    @ConfigurationProperties(prefix = "mysql.two")
//    public DruidDataSource dataSourceOne(){
//        return dataSource;
//    }
    @Bean(name = "routeDataSource")
    public RouteDataSource dataSource(@Qualifier("dataSourceOne")DruidDataSource dataSourceOne
//    ,@Qualifier("dataSourceTwo")DruidDataSource dataSourceTwo
    ){
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("dataSourceOne",dataSourceOne);
//        targetDataSources.put("dataSourceTwo",dataSourceTwo);
        RouteDataSource dataSource = new RouteDataSource();
        dataSource.setTargetDataSources(targetDataSources);
        dataSource.setDefaultTargetDataSource(dataSourceOne);
        System.out.println("routeDataSource******************"+dataSource);
        return  dataSource;
    }
}

@Configuration
public class DruidConfig {
//    @Value("${mysql.one.aliasName}")
//    private String aliasNameOne;
//    @Primary
    @Bean(name="dataSourceOne",initMethod = "init",destroyMethod = "close")
    @ConfigurationProperties(prefix = "mysql.one")
    public DruidDataSource dataSourceOne(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setQueryTimeout(300);
        dataSource.setTestWhileIdle(true);
        dataSource.setTestOnBorrow(true);
        dataSource.setTestOnReturn(true);
        dataSource.setTimeBetweenEvictionRunsMillis(600000);
        dataSource.setMinEvictableIdleTimeMillis(300000);
        return dataSource;
    }
//    @Bean(name="dataSourceTwo",initMethod = "init",destroyMethod = "close")
//    @ConfigurationProperties(prefix = "mysql.two")
//    public DruidDataSource dataSourceOne(){
//        return dataSource;
//    }
    @Bean(name = "routeDataSource")
    public RouteDataSource dataSource(@Qualifier("dataSourceOne")DruidDataSource dataSourceOne
//    ,@Qualifier("dataSourceTwo")DruidDataSource dataSourceTwo
    ){
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("dataSourceOne",dataSourceOne);
//        targetDataSources.put("dataSourceTwo",dataSourceTwo);
        RouteDataSource dataSource = new RouteDataSource();
        dataSource.setTargetDataSources(targetDataSources);
        dataSource.setDefaultTargetDataSource(dataSourceOne);
        System.out.println("routeDataSource******************"+dataSource);
        return  dataSource;
    }
}

public class RouteDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        return DataSourceContext.getDataSource();
    }
}


mapper配置类
import cn.com.config.dbConfig.DataSource;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface MyTestIbatis extends BaseMapper<String>{
    @DataSource("dataSourceOne")
    @Select("select id from tbl_user")
    List<String> selectStr();
}

import tk.mybatis.mapper.common.Mapper;
public interface BaseMapper<T> extends Mapper<T> {
}


@Autowired
MyTestIbatis myTestIbatis;
myTestIbatis.selectStr()

  

springboot&数据库连接池&切面动态数据源

标签:aspect   oid   amp   app   auto   cer   table   java   nec   

原文地址:https://www.cnblogs.com/xingminghui111/p/13381232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!