本篇内容介绍了“AbstractRoutingDataSource AOP如何实现动态数据源切换”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:申请域名、虚拟空间、营销软件、网站建设、代县网站维护、网站推广。
## datasource master # spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.MySQL.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/master?characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 ## datasource slave # spring.datasource-slave.type=com.alibaba.druid.pool.DruidDataSource spring.datasource-slave.driver-class-name=com.mysql.jdbc.Driver spring.datasource-slave.url=jdbc:mysql://localhost:3306/slave?characterEncoding=UTF-8&serverTimezone=UTC spring.datasource-slave.username=root spring.datasource-slave.password=123456
public interface Datasources { String MASTER_DB = "masterDB"; String SLAVE_DB = "slaveDB"; }
@Configuration public class DataSourceConfig { //destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用. @Bean(destroyMethod = "close", name = Datasources.MASTER_DB) @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().type(DruidDataSource.class).build(); } @Bean(destroyMethod = "close", name = Datasources.SLAVE_DB) @ConfigurationProperties(prefix = "spring.datasource-slave") public DataSource dataSourceSlave() { return DataSourceBuilder.create().type(DruidDataSource.class).build(); } }
@Configuration @MapperScan(basePackages = {"com.example.dao"}) public class MybatisConfig { @Autowired @Qualifier(Datasources.MASTER_DB) private DataSource masterDB; @Autowired @Qualifier(Datasources.SLAVE_DB) private DataSource slaveDB; /** * 动态数据源 */ @Bean(name = "dynamicDataSource") public DataSource dynamicDataSource() { DynamicDataSource dynamicDataSource = new DynamicDataSource(); // 默认数据源 dynamicDataSource.setDefaultTargetDataSource(masterDB); // 配置多数据源 Map<Object, Object> dsMap = new HashMap<>(); dsMap.put(Datasources.MASTER_DB, masterDB); dsMap.put(Datasources.SLAVE_DB, slaveDB); dynamicDataSource.setTargetDataSources(dsMap); return dynamicDataSource; } @Bean @ConfigurationProperties(prefix = "mybatis") public SqlSessionFactoryBean sqlSessionFactoryBean() { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); // 配置数据源,此处配置为关键配置,如果没有将 dynamicDataSource 作为数据源则不能实现切换 sqlSessionFactoryBean.setDataSource(dynamicDataSource()); return sqlSessionFactoryBean; } }
public class DataSourceContextHolder { /** * 默认数据源 */ public static final String DEFAULT_DATASOURCE = Datasources.MASTER_DB; private static final ThreadLocal<String> contextHolder = new ThreadLocal<>(); // 设置数据源名 public static void setDB(String dbType) { System.out.println("切换到{}数据源:" + dbType); contextHolder.set(dbType); } // 获取数据源名 public static String getDB() { return (contextHolder.get()); } // 清除数据源名 public static void clearDB() { contextHolder.remove(); } }
@Aspect @Component public class DynamicDataSourceAspect { @Before("@annotation(com.example.util.RoutingDataSource)") public void beforeSwitchDS(JoinPoint joinPoint) { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature.getMethod(); String dataSource = DataSourceContextHolder.DEFAULT_DATASOURCE; if (method.isAnnotationPresent(RoutingDataSource.class)) { RoutingDataSource routingDataSource = method.getDeclaredAnnotation(RoutingDataSource.class); dataSource = routingDataSource.value(); } DataSourceContextHolder.setDB(dataSource); } @After("@annotation(com.example.util.RoutingDataSource)") public void afterSwitchDS(JoinPoint point) { DataSourceContextHolder.clearDB(); } }
public class DynamicDataSource extends AbstractRoutingDataSource{ @Override protected Object determineCurrentLookupKey() { System.out.println("数据源为{}:" + DataSourceContextHolder.getDB()); return DataSourceContextHolder.getDB(); } }
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) public class CutDataBaseApplication { public static void main(String[] args) { SpringApplication.run(CutDataBaseApplication.class, args); } }
/** * @author aYong * @version 1.0 * @date 2019/7/24 */ @RestController @RequestMapping("/route") public class SysUserController { @Autowired private SysUserService sysUserService; @GetMapping("/test1") public SysUser test1(long id) { return sysUserService.test1(id); } @GetMapping("/test2") public Integer test2(long id, String name) { return sysUserService.test2(id, name); } }
“AbstractRoutingDataSource AOP如何实现动态数据源切换”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
名称栏目:AbstractRoutingDataSourceAOP如何实现动态数据源切换
路径分享:https://www.cdcxhl.com/article14/jjppge.html
成都网站建设公司_创新互联,为您提供网站制作、网站导航、电子商务、用户体验、域名注册、静态网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联