欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2022-12-17 09:50:39|栏目:JAVA代码|点击:

1、修改application.properties

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

# 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

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

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+atomikos+druid 数据库连接失效分析

栏    目:JAVA代码

下一篇:Mybatis 多对一查询的实现方法

本文标题:springboot后端配置多个数据源、Mysql数据库的便捷方法

本文地址:http://www.codeinn.net/misctech/221579.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有