springboot+mybatis-plus+多数据源配置,实现分表分库的数据访问
经过上⼀篇springboot+mybatis-plus的配置后,这次多数据源配置没有遇到太⼤的问题。参考了 感谢这位仁兄⽂档的帮助,多数据源利⽤AbstractRoutingDataSource实现动态数据源切换,中间利⽤了切⾯和上下⽂获取的功能实现。
我的开发环境是springboot2.1.4,项⽬结构是:
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<!-- mvnrepository/artifact/org.springframework.boot/spring-boot-starter-aop --> <dependency>
<groupId>org.springframework.boot</groupId>springboot aop
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.1</version>
</dependency>
server:
port: 8080
#mysql
spring:
datasource:
druid:
db1:
username: root
password: 123456
driver-class-name: sql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/local?useUnicode=true&characterEncoding=utf8 initialSize: 5
minIdle: 5
maxActive: 20
db2:
username: root
password: 123456
driver-class-name: sql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
initialSize: 5
minIdle: 5
maxActive: 20
#mybatis-plus 映射⽂件可以在配置⽂件中配置,也可以在配置类中配置,两种⽅式都可以#路径必须正确,不然会报错Invalid bound statement (not found)
mybatis-plus:
mapper-locations: classpath*:/mapper/*.xml
下边就是关于多数据源的配置了
DataSource.java
fig.customenum.DataSourceEnum;
import java.lang.annotation.*;
/**
* ⾃定义注解
* @author leiwenwei
* 类和⽅法⽣效
*/
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
DataSourceEnum value() default DataSourceEnum.DB1;
}
DataSourceAspect.java
fig.annotation.DataSource;
tholder.DataSourceContextHolder;
slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import annotation.Order;
import org.springframework.stereotype.Component;
/**
* 数据源切⾯配置
* Order设置优先级
* @author leiwenwei
*/
@Component
@Slf4j
@Aspect
@Order(-1)
public class DataSourceAspect {
/**
* 设置切⾯范围
*/
@Pointcut("@fig.annotation.DataSource) || @fig.annotation.DataSource)") public void pointCut(){
}
/**
* 添加数据源上下⽂
* @param dataSource
*/
@Before("pointCut() && @annotation(dataSource)")
public void doBefore(DataSource dataSource){
log.info("选择数据源---"+dataSource.value().getValue());
DataSourceContextHolder.setDataSource(dataSource.value().getValue());
}
/**
* 清除数据源上下⽂
*/
@After("pointCut()")
public void doAfter(){
DataSourceContextHolder.clear();
}
}
DataSourceContextHolder.java
/**
* 定义数据源上下⽂
* @author leiwenwei
*/
public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new InheritableThreadLocal<>();
/**
* 设置数据源
* @param db
*/
public static void setDataSource(String db){
contextHolder.set(db);
}
/**
* 取得当前数据源
* @return
*/
public static String getDataSource(){
();
}
/**
* 清除上下⽂数据
*/
public static void clear(){
}
}
DataSourceEnum.java
/**
* 数据源枚举类
* @author leiwenwei
*/
public enum DataSourceEnum {
/**
* 数据库1
*/
DB1("db1"),
/**
* 数据库2
*/
DB2("db2");
private String value;
DataSourceEnum(String value){this.value=value;}
public String getValue() {
return value;
}
}
MultipleDataSource.java
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论