前言
來啦老鐵!
學(xué)習(xí)路徑
- 修改項目啟動類台舱;
- 修改項目配置文件饲化;
- 增加 MybatisConfig.java 配置文件;
- 調(diào)整項目目錄結(jié)構(gòu)劣摇;
1. 修改項目啟動類;
- 刪除 @MapperScan 注解弓乙,例如單 mysql 時:
package priv.dylan.space;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"priv.dylan"})
@MapperScan(basePackages = "priv.dylan.space.mapper")
public class SpaceApplication {
public static void main(String[] args) {
SpringApplication.run(SpaceApplication.class, args);
System.out.println("Hello World~");
}
}
改為:
package priv.dylan.space;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"priv.dylan"})
public class SpaceApplication {
public static void main(String[] args) {
SpringApplication.run(SpaceApplication.class, args);
System.out.println("Hello World~");
}
}
2. 修改項目配置文件末融;
- 找到項目配置文件:application.yml 或 application.propertities 文件;
- spring 下的 datasource 節(jié)點改造為 2 個 mysql 的配置暇韧,如:
spring:
application:
name: utils
datasource:
primary:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3307/my_db?useSSL=false
username: root
password: root
secondary:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://xx.xxx.xxx.xx:3306/xx_xx?useSSL=false
username: xxxxxx
password: xxxxxx
注:
a. primary 和 secondary 是自己定義的滑潘,可以使用任意文本
b. 修改 mybatis 配置,刪除 mapper-locations 和 type-aliases-package锨咙,如:
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: priv.dylan.space.entity
...
改為:
mybatis:
...
3. 增加 MybatisConfig.java 配置文件;
- 代碼參考如下:
package priv.dylan.space.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
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
public class MybatisConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource firstDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory firstSqlSessionFactory(DataSource firstDataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(firstDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/mydb/*.xml"));
return sessionFactory.getObject();
}
@Bean
public SqlSessionFactory secondSqlSessionFactory(DataSource secondDataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(secondDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/testdb/*.xml"));
return sessionFactory.getObject();
}
@Bean
public MapperScannerConfigurer firstMapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("firstSqlSessionFactory");
mapperScannerConfigurer.setBasePackage("priv.dylan.space.mapper.mydb");
return mapperScannerConfigurer;
}
@Bean
public MapperScannerConfigurer secondMapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("secondSqlSessionFactory");
mapperScannerConfigurer.setBasePackage("priv.dylan.space.mapper.testdb");
return mapperScannerConfigurer;
}
}
注:
a. @ConfigurationProperties(prefix = "spring.datasource.primary")
和 @ConfigurationProperties(prefix = "spring.datasource.secondary") 中的 primary 和 secondary 要與項目配置文件 application.yml 或 application.properities 文件中關(guān)于 mysql 數(shù)據(jù)的配置名一致追逮;
b.為不同的 mapper 接口類配置不同的 SqlSessionFactory 這一點非常重要酪刀,如 firstMapperScannerConfigurer 方法和 secondMapperScannerConfigurer;
firstMapperScannerConfigurer 指定了 priv.dylan.space.mapper.mydb 這底下的類全部使用 firstSqlSessionFactory钮孵,也就是使用 classpath:mappers/mydb/*.xml 這底下的 mapper xml 文件骂倘;
而 firstSqlSessionFactory 又使用了 firstDataSource,也就是第一個 mysql 配置去鏈接 mysql巴席;
c. secondMapperScannerConfigurer 類似历涝;
這樣,我們?yōu)槊總€ mapper 接口類指定了 mapper xml 文件漾唉,并且指定了執(zhí)行 sql 的數(shù)據(jù)庫信息荧库,也就能做到支持多個數(shù)據(jù)庫了;
4. 調(diào)整項目目錄結(jié)構(gòu)赵刑;
entity 包按不同數(shù)據(jù)庫劃分(建議)分衫;
mapper 包按不同數(shù)據(jù)庫劃分;
mapper xml 按不同數(shù)據(jù)庫劃分般此;
其他如 service 層蚪战、controller 層、domain 等铐懊,自行評估邀桑,可選;
結(jié)構(gòu)如:
如此科乎,就能在 springboot3 項目中使用2個或多個 mysql 數(shù)據(jù)庫啦~
當(dāng)然壁畸,我們還可以參考使用 dynamic-datasource-spring-boot-starter 包:
《Spring Boot之多數(shù)據(jù)庫源:極簡方案》
如果本文對您有幫助,麻煩點贊喜喂、關(guān)注瓤摧!
謝謝竿裂!