业务场景使用多个数据源例如主从复制,本篇是在springboot整合mybaties基础上整合来的
pom文件的使用: 代码未补充完整,后续上传到github
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 这是一个web应用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- swagger文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- 打包时拷贝MyBatis的映射文件 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/sqlmap/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<!--打包时不包括resources下面的资源-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
applcaition.yml文件:
server:
port: 9089
spring:
datasource:
master:
# class name
driver-class-name: com.mysql.cj.jdbc.Driver
#springboot默认的连接池
type: com.zaxxer.hikari.HikariDataSource
#规定写jdbcUrl路径
jdbcUrl: jdbc:mysql://127.0.0.1:3306/springboot?useSSL=false&serverTimezone=UTC
username: root
password: 123456
slave:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
jdbcUrl: jdbc:mysql://127.0.0.1:3306/learn?useSSL=false&serverTimezone=UTC
username: root
password: 123456
配置类:
/**
* @Author: Jason
* @Create: 2020/11/18 21:47
* @Description 配置了多个数据源
*/
@SpringBootConfiguration
@MapperScan("com.example.demo.dao")
public class MybatiesConfig {
/**
* 定义一个默认的数据库
* 从配置文件中直接拿取配置好的主数据库配置
* @return
*/
@Bean("master")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource master(){
return DataSourceBuilder.create().build();
}
@Bean("slave")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource slave(){
return DataSourceBuilder.create().build();
}
/**
* 配置动态数据源类
* @return
* @throws Exception
*/
@Bean("dynamicDataSource")
public DataSource dynamicDataSource() throws Exception {
DynamicDataSource sqlSessionFactoryBean = new DynamicDataSource();
Map<Object, Object> dataSourceMap = new HashMap<>();
//将配置好的数据源添加到动态数据源中去
dataSourceMap.put("master", master());
dataSourceMap.put("slave", slave());
//指定默认的数据源
sqlSessionFactoryBean.setDefaultDataSource(master());
//将已经配置好的类添加
sqlSessionFactoryBean.setDataSources(dataSourceMap);
return sqlSessionFactoryBean;
}
/**
* 创建一个sqlSessionFactory类
* @return
* @throws Exception
*/
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//设置好动态数据类
sessionFactory.setDataSource(dynamicDataSource());
sessionFactory.setTypeAliasesPackage("com.example.demo.model");
//扫描到xml位置的并且注入到工厂中去
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactory.setMapperLocations(resolver.getResources("classpath*:**/sqlmap/*.xml"));
return sessionFactory;
}
/**
* 对动态数据源开启事务管理
* @return
* @throws Exception
*/
@Bean
public PlatformTransactionManager platformTransactionManager() throws Exception {
return new DataSourceTransactionManager(dynamicDataSource());
}
}