SpringDataJdbc多数据源
代码基于 SpringBoot + SpringDataJDBC + Mybatis 架构
介绍使用多数据源的关键的一些类:AbstractRoutingDataSource继承实现determineCurrentLookupKey方法,此方法主要返回AbstractRoutingDataSource抽象类的多数据源的Key值
提醒:这里主要提醒一下,Spring Aop(AspectJ实现)的拦截方法,@注解之类的方法的参数可以具有一个 org.aspectj.lang.JoinPoint 类型的形参,而@Around类需要是ProceedingJoinPoint此形参,不要混用(官方说明来源:https://docs.spring.io/spring/docs/5.2.6.RELEASE/spring-framework-reference/core.html#aop-ataspectj-advice-params)
首先展示下配置文件:application.yml
spring: datasource: one: username: root password: root jdbc-url: jdbc:mariadb://localhost:3306/myschool driver-class-name: org.mariadb.jdbc.Driver two: username: root password: root jdbc-url: jdbc:mariadb://localhost:3306/myschool driver-class-name: org.mariadb.jdbc.Driver
配置多个数据源:
@Bean(value = "db1")@ConfigurationProperties(prefix = "spring.datasource.one")public HikariDataSource dataSource() { HikariDataSource dataSource = new HikariDataSource(); return dataSource;}
@Bean(value = "db2")@ConfigurationProperties(prefix = "spring.datasource.two")public HikariDataSource dataSource2() { HikariDataSource dataSource = new HikariDataSource(); return dataSource;}下面是多数据源的配置:
动态数据源实现如下:
获取数据源的目标位置:DataSourceTransactionManager类的doBegin方法
具体可以下载Demo试试:https://github.com/starSmallDream/MySpringDataJdbcAndMybatisByManyDataSource.git
相关推荐
csuzxm000 2020-08-02
幸运小侯子 2020-07-06
mituan 2020-05-26
zhangdy0 2020-05-25
吾日五省我身 2020-05-12
whbing 2020-04-30
琦小虾的代码世界 2020-03-13
也许不会看见 2020-01-29
zxznsjdsj 2020-01-18
吾日五省我身 2020-01-07
burning 2014-05-28
whbing 2019-12-19
flydoging 2019-12-05
whileinsist 2019-11-19
zhangbeizhen 2019-11-16
lysanderK 2019-11-08
congcongsuiyue 2016-08-19
数据分析杂谈 2016-12-13
smalllove 2019-10-31