springboot后端配置多个数据源、Mysql数据库的便捷方法

时间:2021-11-19 01:42:29

1、修改application.properties

新建 Mapper、实体类 相应的文件夹,将不同数据源的文件保存到对应的文件夹下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
# test1 数据库的配置
test1.spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
test1.spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/database1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
test1.spring.datasource.username=username
test1.spring.datasource.password=password
# test2 数据库的配置
test2.spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
test2.spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/database2?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
test2.spring.datasource.username=username
test2.spring.datasource.password=password
# 配置 mybatis 配置 mapper.xml 文件路径mybatis.mapper-locations=classpath:/mapper/*.xml,classpath:/mapper/test1/*.xml,classpath:/mapper/test2/*.xml
# 配置 mybatis 配置 实体类 文件路径
mybatis.type-aliases-package=com.xxx.entity.test1, com.xxx.entity.test2

2、添加绑定数据库配置Config

1)Test1DataSourceConfig.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.xxx.config.dataSource;
import com.alibaba.druid.pool.DruidDataSource;
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.beans.factory.annotation.Value;
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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/** * @program: test1 主数据源配置
* @author: JackLee
* @create: 2020-08-09 09:30
* @version: 1.0
**///
扫描 Mapper 接口并容器管理
@MapperScan(basePackages = Test1DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "test1SqlSessionFactory")
@Configuration
public class Test1DataSourceConfig {
// 精确到 test1 目录,以便跟其他数据源隔离
static final String PACKAGE = "com.xxx.dao.test1";
static final String MAPPER_LOCATION = "classpath:mapper/test1/*.xml";
@Value("${test1.spring.datasource.jdbc-url}")
private String url;
@Value("${test1.spring.datasource.username}")
private String user;
@Value("${test1.spring.datasource.password}")
private String password;
@Value("${test1.spring.datasource.driver-class-name}")
private String driverClass;
@Bean(name = "test1DataSource")
@Primary
public DataSource test1DataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "test1TransactionManager")
@Primary
public DataSourceTransactionManager test1TransactionManager() {
return new DataSourceTransactionManager(test1DataSource());
}
@Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory test1SqlSessionFactory(@Qualifier("test1DataSource") DataSource test1DataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(test1DataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(Test1DataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
 
}

2)Test2DataSourceConfig.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.xxx.config.dataSource;
import com.alibaba.druid.pool.DruidDataSource;
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.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @program: test2 其他数据源配置
* @author: JackLee
* @create: 2020-08-09 09:30
* @version: 1.0
**/
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = Test2DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "test2SqlSessionFactory")
@Configuration
public class Test2DataSourceConfig {  
 // 精确到 test2 目录,以便跟其他数据源隔离   
 static final String PACKAGE = "com.xxx.dao.test2";   
 static final String MAPPER_LOCATION = "classpath:mapper/test2/*.xml";  
 
 @Value("${test2.spring.datasource.jdbc-url}")   
 private String url;  
 
 @Value("${test2.spring.datasource.username}")   
 private String user;  
 
 @Value("${test2.spring.datasource.password}")   
 private String password;  
 
 @Value("${test2.spring.datasource.driver-class-name}")  
 private String driverClass; 
 
 @Bean(name = "test2DataSource")   
 public DataSource test2DataSource() {      
 DruidDataSource dataSource = new DruidDataSource();       
 dataSource.setDriverClassName(driverClass);       
 dataSource.setUrl(url);       
 dataSource.setUsername(user);       
 dataSource.setPassword(password);       
 return dataSource;   
 }  
 @Bean(name = "test2TransactionManager")  
 public DataSourceTransactionManager test2TransactionManager() {      
 return new DataSourceTransactionManager(test2DataSource());   
 }   
 @Bean(name = "test2SqlSessionFactory")  
 public SqlSessionFactory test2SqlSessionFactory(@Qualifier("test2DataSource") DataSource test2DataSource)          
 throws Exception {      
 final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();       
 sessionFactory.setDataSource(test2DataSource);       
 sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()               
 .getResources(Test2DataSourceConfig.MAPPER_LOCATION));
 return sessionFactory.getObject();    }
 
}

搞定,轻轻松松~

注:

  • mapper文件夹下新建的文件夹需要有 *.xml 文件( 即:classpath:/mapper/xxx/*.xml下要存在文件 ),不然可能检测不到
  • 需要继续添加数据源就再加一个其他数据源

以上就是方便快捷实现springboot 后端配置多个数据源、Mysql数据库的详细内容,更多关于springboot 后端配置多个数据源、Mysql数据库的资料请关注服务器之家其它相关文章!希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_42280984/article/details/119530437