嗨,大家好,我是小壁虎,今天我们来聊聊一些簇新的话题。在这个快节律的数字化期间,手机已成为我们糊口中不行或缺的一部分,它陪同我们渡过每一个昼夜,纪录着我们的一丝...
图片黄色幽默黄色幽默 北宋 钧窑天蓝釉玫瑰紫斑折沿盘 D 18.9cm 开始: 1. 伦敦苏富比,2004年6月9日,编号172 2. 欧洲私东说念主储藏 3....
《剑灵2》新·灵动测试将于8月上旬慎重开启。本次测试包含繁多实质优化,以及全新实质加入黄色幽默,况且搏斗体验再次全面升级! 据官方先容,过程精采连系上测多半灵芝...
普罗旺斯的马赞村耳光 调教,因其讲究的满足和安宁,眩惑了一批想隐士的东说念主们来此假寓。 女演员凯拉·奈特莉便是为了走避明星的压力,在迷东说念主的普罗旺斯马赞村...
跟着国内新能源车销量的进一步攀升,凭证中汽协的最新数据夸耀,9月新能源车的国内零卖浸透率已达53.3%黄色幽默,收场了同比16.4%的快速增长,而反不雅燃油车阵...
目次女同 av 名目场景: 一、集成com.baomidou的面目 1、maven依赖: 2、 成就文献: 3、 使用注解切换数据源: 二、基于AOP手动竣事宽绰据源原生的面目 1、maven依赖: 2、 成就文献: 3、 宽绰据源称号类 4、自界说注解 5、成就类 6、动态数据源成就女同 av 7、AOP切面禁止注解 8、Dao层,跟平常相同的 9、service层,加上宽绰据源注解 10、Controller测试 三、成果展示 名目场景:Spring Boot集成Druid竣事宽绰据源的两种面目: 1、集成com.baomidou,引入dynamic-datasource依赖; 2、原生的面目,基于AOP手动竣事宽绰据源; 一、集成com.baomidou的面目 1、maven依赖:<dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.5.1</version></dependency><dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version></dependency>2、 成就文献: 1.spring.datasource.dynamic前缀,是baomidou源码里固定的; 2.first、second是自界说的称号,不错篡改; # 宽绰据源成就,默许masterspring.datasource.dynamic.primary = first# 数据源1spring.datasource.dynamic.datasource.first.driverClassName = com.mysql.cj.jdbc.Driverspring.datasource.dynamic.datasource.first.type = com.alibaba.druid.pool.DruidDataSourcespring.datasource.dynamic.datasource.first.url = jdbc:mysql://127.0.0.1:3306/mp_dm?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT+8spring.datasource.dynamic.datasource.first.username = testspring.datasource.dynamic.datasource.first.password = test1# 数据源2spring.datasource.dynamic.datasource.second.driverClassName = com.mysql.cj.jdbc.Driverspring.datasource.dynamic.datasource.second.type = com.alibaba.druid.pool.DruidDataSourcespring.datasource.dynamic.datasource.second.url = jdbc:mysql://127.0.0.1:18306/mp_dm?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT+8spring.datasource.dynamic.datasource.second.username = testspring.datasource.dynamic.datasource.second.password = test1# druid宽绰据源全局成就spring.datasource.dynamic.druid.filter.stat.log-slow-sql = truespring.datasource.dynamic.druid.filter.stat.slow-sql-millis = 1000spring.datasource.dynamic.druid.min-evictable-idle-time-millis = 300000spring.datasource.dynamic.druid.test-on-borrow = falsespring.datasource.dynamic.druid.filter.stat.merge-sql = falsespring.datasource.dynamic.druid.test-on-return = falsespring.datasource.dynamic.druid.initial-size = 10spring.datasource.dynamic.druid.min-idle = 10spring.datasource.dynamic.druid.max-wait = 60000spring.datasource.dynamic.druid.pool-prepared-statements = truespring.datasource.dynamic.druid.test-while-idle = truespring.datasource.dynamic.druid.validation-query = select 1spring.datasource.dynamic.druid.filter.wall.config.multi-statement-allow = truespring.datasource.dynamic.druid.time-between-eviction-runs-millis = 60000spring.datasource.dynamic.druid.max-pool-prepared-statement-per-connection-size = 20spring.datasource.dynamic.druid.max-active = 100# druid监控全局成就spring.datasource.druid.stat-view-servlet.enabled = truespring.datasource.druid.stat-view-servlet.url-pattern = /druid/* 考据成就文献是否收效:Spring Boot集成Druid检察成就是否收效_涛哥是个大帅比的博客-CSDN博客 3、 使用注解切换数据源:@DS注讲解明: 1.注解在程序上、类上、接口、陈列,同期存在就近原则,程序上注解优先于类上注解; 2.不使用@DS注解,默许主数据源; 类上: @DS("db2")public class test(){} 程序上: @DS("db2")public void test(){} mapper程序上 @Select("SELECT * FROM TEST WHERE A = #{A}")@DS("db2")Map<String, Object> test(@Param("A") String A); 宽绰据源可能会遇到事务问题: Spring Boot宽绰据源事务@DSTransactional的使用_涛哥是个大帅比的博客-CSDN博客 二、基于AOP手动竣事宽绰据源原生的面目 1、maven依赖:<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version></dependency>2、 成就文献: 1.Spring Boot 2.X 版块不再守旧成就采纳,宽绰据源的话每个数据源的悉数成就齐需要单独成就,不然成就不会收效; 2.first、second是自界说的称号,对应DynamicDataSourceConfig成就类,不错篡改; # 宽绰据源成就spring.datasource.type = com.alibaba.druid.pool.DruidDataSource# 数据源1spring.datasource.druid.first.name = firstspring.datasource.druid.first.driverClassName = com.mysql.cj.jdbc.Driverspring.datasource.druid.first.url = jdbc:mysql://127.0.0.1:3306/mp_dm?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT+8spring.datasource.druid.first.username = testspring.datasource.druid.first.password = test1# 数据源2spring.datasource.druid.second.name = secondspring.datasource.druid.second.driverClassName = com.mysql.cj.jdbc.Driverspring.datasource.druid.second.url = jdbc:mysql://127.0.0.1:18306/mp_dm?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT+8spring.datasource.druid.second.username = testspring.datasource.druid.second.password = test1# druid数据源1spring.datasource.druid.first.filter.stat.log-slow-sql = truespring.datasource.druid.first.filter.stat.slow-sql-millis = 1000spring.datasource.druid.first.min-evictable-idle-time-millis = 300000spring.datasource.druid.test-on-borrow = falsespring.datasource.druid.first.filter.stat.merge-sql = falsespring.datasource.druid.test-on-return = falsespring.datasource.druid.first.initial-size = 10spring.datasource.druid.first.min-idle = 10spring.datasource.druid.first.max-wait = 60000spring.datasource.druid.first.pool-prepared-statements = truespring.datasource.druid.first.test-while-idle = truespring.datasource.druid.first.validation-query = select 1spring.datasource.druid.first.filter.wall.config.multi-statement-allow = truespring.datasource.druid.first.time-between-eviction-runs-millis = 60000spring.datasource.druid.first.max-pool-prepared-statement-per-connection-size = 20spring.datasource.druid.first.max-active = 100# druid数据源2spring.datasource.druid.second.filter.stat.log-slow-sql = truespring.datasource.druid.second.filter.stat.slow-sql-millis = 1000spring.datasource.druid.second.min-evictable-idle-time-millis = 300000spring.datasource.druid.second.test-on-borrow = falsespring.datasource.druid.second.filter.stat.merge-sql = falsespring.datasource.druid.second.test-on-return = falsespring.datasource.druid.second.initial-size = 10spring.datasource.druid.second.min-idle = 10spring.datasource.druid.second.max-wait = 60000spring.datasource.druid.second.pool-prepared-statements = truespring.datasource.druid.second.test-while-idle = truespring.datasource.druid.second.validation-query = select 1spring.datasource.druid.second.filter.wall.config.multi-statement-allow = truespring.datasource.druid.second.time-between-eviction-runs-millis = 60000spring.datasource.druid.second.max-pool-prepared-statement-per-connection-size = 20spring.datasource.druid.second.max-active = 100# druid监控全局成就spring.datasource.druid.stat-view-servlet.enabled = truespring.datasource.druid.stat-view-servlet.url-pattern = /druid/* 考据成就文献是否收效:Spring Boot集成Druid检察成就是否收效_涛哥是个大帅比的博客-CSDN博客 3、 宽绰据源称号类package com.test.datasources;/** * 增多宽绰据源,在此成就 */public interface DataSourceNames { String FIRST = "first"; String SECOND = "second";}4、自界说注解 package com.test.datasources.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 宽绰据源注解 *///同期守旧程序注解和类注解@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface DataSource { String value() default "";}5、成就类 package com.test.datasources;import java.util.HashMap;import java.util.Map;import javax.sql.DataSource;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.jdbc.core.JdbcTemplate;import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;/** * 成就宽绰据源 */@Configurationpublic class DynamicDataSourceConfig { @Bean @ConfigurationProperties("spring.datasource.druid.first") public DataSource firstDataSource(){ return DruidDataSourceBuilder.create().build(); } @Bean @ConfigurationProperties("spring.datasource.druid.second") public DataSource secondDataSource(){ return DruidDataSourceBuilder.create().build(); } @Bean @Primary public DynamicDataSource dataSource(DataSource firstDataSource, DataSource secondDataSource) { Map<Object, Object> targetDataSources = new HashMap<>(); targetDataSources.put(DataSourceNames.FIRST, firstDataSource); targetDataSources.put(DataSourceNames.SECOND, secondDataSource); return new DynamicDataSource(firstDataSource, targetDataSources); } @Bean public JdbcTemplate jdbcTemplate() { return new JdbcTemplate(firstDataSource()); }}6、动态数据源成就 package com.test.datasources;import java.util.Map;import javax.sql.DataSource;import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;/** * 动态数据源,要道代码 */public class DynamicDataSource extends AbstractRoutingDataSource { private static final ThreadLocal<String> contextHolder = new ThreadLocal<>(); public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources) { super.setDefaultTargetDataSource(defaultTargetDataSource); super.setTargetDataSources(targetDataSources); super.afterPropertiesSet(); } @Override protected Object determineCurrentLookupKey() { return getDataSource(); } public static void setDataSource(String dataSource) { contextHolder.set(dataSource); } public static String getDataSource() { return contextHolder.get(); } public static void clearDataSource() { contextHolder.remove(); }}7、AOP切面禁止注解 package com.test.datasources.aspect;import java.lang.reflect.Method;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.core.Ordered;import org.springframework.stereotype.Component;import com.test.datasources.DataSourceNames;import com.test.datasources.DynamicDataSource;import com.test.datasources.annotation.DataSource;/** * 宽绰据源,切面管束类 */@Aspect@Componentpublic class DataSourceAspect implements Ordered { protected Logger logger = LoggerFactory.getLogger(getClass()); /* * @annotation匹配指定注解的程序 * @within匹配指定注解的类 * 把稳:这里只禁止所注解的类,淌若调用的是父类的程序,那么不会禁止,除非父类程序在子类中被隐蔽。 */ @Pointcut("@annotation(com.test.datasources.annotation.DataSource) |