类似的实现百度一大把,不过别人实现了的照搬到自己电脑上不见的也可以运行,依然遇到很多问题,这回自己实现一把,亲测可用,文末附源码地址。
核心配置代码:
1. pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-mybatis-multiple-datasource</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-mybatis-multiple-datasource</name>
<description>Demo Multiple Datasource for Spring Boot</description>
<parent>
<groupId>com.along</groupId>
<artifactId>spring-boot-all</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
<!-- mybatis-generator-core 反向生成java代码-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--mybatis逆向工程maven插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--允许移动生成的文件-->
<verbose>true</verbose>
<!--允许覆盖生成的文件-->
<overwrite>true</overwrite>
<!--配置文件的路径 默认resources目录下-->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
<!--插件依赖的jar包-->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
2. application.yml
注意:springboot2开始mysql驱动变为com.mysql.cj.jdbc.Driver
server:
port: 8080
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
primary:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&serverTimezone=UTC&allowMultiQueries=true
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
#获取连接等待超时时间
max-wait: 60000
#间隔多久进行一次检测,检测需要关闭的空闲连接
time-between-eviction-runs-millis: 60000
#一个连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
pool-prepared-statements: false
max-pool-prepared-statement-per-connection-size: 20
local:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test2?useSSL=false&useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&serverTimezone=UTC&allowMultiQueries=true
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
prod:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test3?useSSL=false&useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&serverTimezone=UTC&allowMultiQueries=true
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
#映射文件所在路径
mapper-locations: classpath:com.along.dao/*.xml
#pojo类所在包路径
type-aliases-package: com.along.entity
configuration:
#配置项:开启下划线到驼峰的自动转换. 作用:将数据库字段根据驼峰规则自动注入到对象属性。
map-underscore-to-camel-case: true
#pagehelper
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
logging:
level:
#打印SQL信息
com.along.dao: debug
3. 数据源配置类 MultipleDataSourceConfig.java
/**
* 数据源配置
*/
@Configuration
public class MultipleDataSourceConfig {
@Bean(name = "dataSourcePrimary")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return new DruidDataSource();
}
@Bean(name = "dataSourceLocal")
@ConfigurationProperties(prefix = "spring.datasource.local")
public DataSource localDataSource() {
return new DruidDataSource();
}
@Bean(name = "dataSourceProd")
@ConfigurationProperties(prefix = "spring.datasource.prod")
public DataSource prodDataSource() {
return new DruidDataSource();
}
@Primary
@Bean(name = "dynamicDataSource")
public DataSource dynamicDataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
//配置默认数据源
dynamicDataSource.setDefaultTargetDataSource(primaryDataSource());
//配置多数据源
HashMap<Object, Object> dataSourceMap = new HashMap<>();
dataSourceMap.put(ContextConst.DataSourceType.PRIMARY.name(), primaryDataSource());
dataSourceMap.put(ContextConst.DataSourceType.LOCAL.name(), localDataSource());
dataSourceMap.put(ContextConst.DataSourceType.PROD.name(), prodDataSource());
dynamicDataSource.setTargetDataSources(dataSourceMap); // 该方法是AbstractRoutingDataSource的方法
return dynamicDataSource;
}
/**
* 配置@Transactional注解事务
*
* @return
*/
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dynamicDataSource());
}
}
4. 数据源持有类 DataSourceContextHolder.java
/**
* 数据源持有类
*/
public class DataSourceContextHolder {
private static final Logger logger = LoggerFactory.getLogger(DataSourceContextHolder.class);
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
public static void setDataSource(String dbType){
logger.info("切换到[{}]数据源",dbType);
contextHolder.set(dbType);
}
public static String getDataSource(){
return contextHolder.get();
}
public static void clearDataSource(){
contextHolder.remove();
}
}
5. 数据源路由实现类 DynamicDataSource.java
/**
* 数据源路由实现类
*/
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final Logger logger = LoggerFactory.getLogger(DynamicDataSource.class);
@Override
protected Object determineCurrentLookupKey() {
String dataSource = DataSourceContextHolder.getDataSource();
if (dataSource == null) {
logger.info("当前数据源为[primary]");
} else {
logger.info("当前数据源为{}", dataSource);
}
return dataSource;
}
}
6. 自定义切换数据源的注解
/**
* 切换数据源的注解
*/
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {
ContextConst.DataSourceType value() default ContextConst.DataSourceType.PRIMARY;
}
7. 数据源枚举类
/**
* 上下文常量
*/
public interface ContextConst {
/**
* 数据源枚举
*/
enum DataSourceType {
PRIMARY, LOCAL, PROD, TEST
}
}
8. 定义切换数据源的切面,为注解服务
/**
* 切换数据源的切面
*/
@Component
@Aspect
@Order(1) //这是关键,要让该切面调用先于AbstractRoutingDataSource的determineCurrentLookupKey()
public class DynamicDataSourceAspect {
@Before("execution(* com.along.service..*.*(..))")
public void before(JoinPoint point) {
try {
DataSource annotationOfClass = point.getTarget().getClass().getAnnotation(DataSource.class);
String methodName = point.getSignature().getName();
Class[] parameterTypes = ((MethodSignature) point.getSignature()).getParameterTypes();
Method method = point.getTarget().getClass().getMethod(methodName, parameterTypes);
DataSource methodAnnotation = method.getAnnotation(DataSource.class);
methodAnnotation = methodAnnotation == null ? annotationOfClass : methodAnnotation;
ContextConst.DataSourceType dataSourceType = methodAnnotation != null
&& methodAnnotation.value() != null ? methodAnnotation.value() : ContextConst.DataSourceType.PRIMARY;
DataSourceContextHolder.setDataSource(dataSourceType.name());
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
@After("execution(* com.along.service..*.*(..))")
public void after(JoinPoint point) {
DataSourceContextHolder.clearDataSource();
}
}
9. 修改启动类
//排除DataSource自动配置类,否则会默认自动配置,不会使用我们自定义的DataSource,并且启动报错
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@MapperScan({"com.along.dao"}) // 扫描包路径
public class SpringBootMybatisMultipleDatasourceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisMultipleDatasourceApplication.class, args);
}
}
10. 使用
在方法上通过注解@DataSource指定该方法所用的数据源,如果没有使用注解指定则使用默认数据源
下面是在service实现类中的应用:
/**
* @Description: service实现
* @Author along
* @Date 2018/12/28 17:44
*/
@Service(value = "personService")
@Transactional
public class PersonServiceImpl implements PersonService {
private PersonMapper personMapper;
@Autowired
public PersonServiceImpl(@Qualifier("personMapper") PersonMapper personMapper) {
this.personMapper = personMapper;
}
@Override
public Integer add(Person person) {
return personMapper.insert(person);
}
@Override
public PageInfo<Person> findAllPerson(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
PersonExample example = new PersonExample();
List<Person> personList = personMapper.selectByExample(example);
return new PageInfo<>(personList);
}
@DataSource(ContextConst.DataSourceType.PROD) // 指定该方法使用prod数据源
@Override
public PageInfo<Person> findByName(String name) {
PersonExample example = new PersonExample();
PersonExample.Criteria criteria = example.createCriteria();
criteria.andNameEqualTo(name);
List<Person> personList = personMapper.selectByExample(example);
return new PageInfo<>(personList);
}
@DataSource(ContextConst.DataSourceType.LOCAL) // 指定该方法使用local数据源
@Override
public int insert(Person person) {
return personMapper.insert(person);
}
@Override
public int insertBatch(List<Person> list) {
return personMapper.insertBatchSelective(list);
}
@Override
public int updateBatch(List<Person> list) {
return personMapper.updateBatchByPrimaryKeySelective(list);
}
}
源码地址
https://github.com/alonglong/spring-boot-all/tree/master/spring-boot-mybatis-multiple-datasource
网友评论