SpringBoot多数据源-集成多个mybatis框架
- 1.基本框架
- 2.使用
- 2.1项目结构
- 2.2 依赖导入
- 2.3 application.yml配置
- 2.4 创建读与写的SqlSessionFactoryBean
- 3.总结
1.基本框架
通过启动多个SqlSessionFactoryBean
,每个SqlSessionFactoryBean
对应一个datasource
和指定位置的mapper.xml
文件,就可以实现多个数据源了。而不用切换数据源,不用实现AbstractRoutingDataSource
。
2.使用
2.1项目结构
2.2 依赖导入
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.4</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.rql</groupId><artifactId>springboot-mybatis-dataSource-043</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-mybatis-dataSource-043</name><description>springboot-mybatis-dataSource-043</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></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><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter-test</artifactId><version>3.0.3</version><scope>test</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.6</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>
2.3 application.yml配置
spring:datasource:datasource1:driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/test_masterusername: rootpassword: rootdruid:initial-size: 1min-idle: 1max-active: 20test-on-borrow: truedatasource2:driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/test_slaveusername: rootpassword: rootdruid:initial-size: 1min-idle: 1max-active: 20test-on-borrow: truemain:allow-bean-definition-overriding: truemybatis:type-aliases-package: com.rql.entity
2.4 创建读与写的SqlSessionFactoryBean
RMybatisConfig.java
(读)
package com.rql.config;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.rql.mapper.r",sqlSessionFactoryRef = "rsqlSessionFactory")
public class RMybatisConfig {@Bean@ConfigurationProperties(prefix = "spring.datasource.datasource2")public DataSource dataSource2(){return DataSourceBuilder.create().build();}@Beanpublic SqlSessionFactory rsqlSessionFactory() throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource2());sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/r/*.xml"));return sqlSessionFactoryBean.getObject();}
}
WMybatisConfig.java
(写)
package com.rql.config;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource;@Configuration
//继承mybatis
//1.basePackages:指定扫描的mapper接口包(主库)
//2.sqlSessionFactoryRef:指定使用sqlSessionFactory是哪个
@MapperScan(basePackages = "com.rql.mapper.w",sqlSessionFactoryRef = "wsqlSessionFactory")
public class WMybatisConfig {@Bean@ConfigurationProperties(prefix = "spring.datasource.datasource1")public DataSource dataSource1(){return DataSourceBuilder.create().build();}@Bean@Primarypublic SqlSessionFactory wsqlSessionFactory() throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(dataSource1());sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/w/*.xml"));return sqlSessionFactoryBean.getObject();}
}
剩下的就是在基本的mybatis
的基础上进行测试了,具体的xml
书写这里不在列出,可以简单写个查询和插入,来验证多数据源的配置是否正确即可。
3.总结
SpringBoot
集成多个mybatis
框架,实现多数据源,就不需要再像之前的单个Mybatis
或者AOP
的方式,需要实现AbstractRoutingDataSource
来切换数据源。