SpringBoot+Mybatis整合多数据源
在实际项目开发中,不免会遇到需要一套程序需要操作多个数据库的场景,这时候多数据源就派上用场了。
项目结构
引入Maven依赖包
<?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>2.6.4</version>
<relativePath/>
</parent>
<groupId>com.stw</groupId>
<artifactId>STW</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>STW</name>
<description>STW</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>STW</finalName>
<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>
配置数据源
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
db01:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/db01?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
username: 你的用户名
password: 你的密码
db02:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/db02?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
username: 你的用户名
password: 你的密码
引入需要的Bean
1、数据源01 bean 注册
@Configuration
public class DataSourceConfig1 {
@Bean("db01DataSource")
@ConfigurationProperties(prefix = "spring.datasource.db01")
public DataSource getDb1DataSource(){
return DataSourceBuilder.create().build();
}
@Bean("db01SqlSessionFactory")
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db01DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db01/*.xml"));
return bean.getObject();
}
@Bean("db01SqlSessionTemplate")
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db01SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean("db01MMapperScannerConfigurer")
public MapperScannerConfigurer db02MMapperScannerConfigurer() {
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
configurer.setBasePackage("**.dao.db01");
configurer.setSqlSessionFactoryBeanName("db01SqlSessionFactory");
return configurer;
}
}
2、数据源02 bean 注册
@Configuration
public class DataSourceConfig2 {
@Bean("db02DataSource")
@ConfigurationProperties(prefix = "spring.datasource.db02")
public DataSource getDb02DataSource(){
return DataSourceBuilder.create().build();
}
@Bean("db02SqlSessionFactory")
public SqlSessionFactory db02SqlSessionFactory(@Qualifier("db02DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db02/*.xml"));
return bean.getObject();
}
@Bean("db02SqlSessionTemplate")
public SqlSessionTemplate db02SqlSessionTemplate(@Qualifier("db02SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean("db02MMapperScannerConfigurer")
public MapperScannerConfigurer db02MMapperScannerConfigurer() {
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
configurer.setBasePackage("**.dao.db02");
configurer.setSqlSessionFactoryBeanName("db02SqlSessionFactory");
return configurer;
}
}
|