SpringBoot 配置Mysql多数据源DataSource以及各种工作环境切换
来源:互联网 发布:荷兰国旗问题算法java 编辑:程序博客网 时间:2024/06/05 13:25
公司内部项目从SpringMVC转型至SpringBoot,期间遇到不少小挫折,现记录下来方便其他小伙伴能够少走坑。
通常我们一个项目可能存在开发、联调、测试、线上等环境,那么我们使用SpringBoot的工作环境切换配置会很方便,首先新建一个application-dev.properties开发环境文件,然后再application.properties主文件中使用spring.profiles.active=dev引入开发环境配置即可。
下面开始详解多数据源的配置:
1.在application-dev.properties中写入
#mysql x1spring.datasource.url=xxxspring.datasource.username=xxxspring.datasource.password=xxxspring.datasource.driver-class-name=com.mysql.jdbc.Driver#mysql x2spring.datasource-wmatch.url=xxxspring.datasource-wmatch.username=xxxspring.datasource-wmatch.password=xxxspring.datasource-wmatch.driver-class-name=com.mysql.jdbc.Driver#mysql x3spring.datasource-match.url=xxxspring.datasource-match.username=xxxspring.datasource-match.password=xxxspring.datasource-match.driver-class-name=com.mysql.jdbc.Driver
2.@Configuration// Springboot多数据源获取配置基类public class DataSourceConfig {
//get set 对应配置文件中的4个属性private String url;private String username;private String password;private String driverClassName;@Bean(name = "micDataSource")@Primary //主数据源@ConfigurationProperties(ignoreUnknownFields = false, prefix = "spring.datasource")public DataSource micDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "wmatchDataSource")@ConfigurationProperties(ignoreUnknownFields = false, prefix = "spring.datasource-wmatch")public DataSource wmatchDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "matchDataSource")@ConfigurationProperties(ignoreUnknownFields = false, prefix = "spring.datasource-match")public DataSource matchDataSource() {return DataSourceBuilder.create().build();}
//拿到不同数据源的SessionFactory@Bean(name = "micSessionFactory")@Primarypublic LocalSessionFactoryBean micSessionFactory(@Qualifier("micDataSource") DataSource dataSource) {LocalSessionFactoryBean bean = new LocalSessionFactoryBean();bean.setDataSource(dataSource); return bean;}@Bean(name = "wmatchSessionFactory")public LocalSessionFactoryBean wmatchSessionFactory(@Qualifier("wmatchDataSource") DataSource dataSource) {LocalSessionFactoryBean bean = new LocalSessionFactoryBean();bean.setDataSource(dataSource); return bean;}@Bean(name = "matchSessionFactory")public LocalSessionFactoryBean matchSessionFactory(@Qualifier("matchDataSource") DataSource dataSource) {LocalSessionFactoryBean bean = new LocalSessionFactoryBean();bean.setDataSource(dataSource); return bean;}/** * @return the url */public String getUrl() {return url;}/** * @param url * the url to set */public void setUrl(String url) {this.url = url;}/** * @return the username */public String getUsername() {return username;}/** * @param username * the username to set */public void setUsername(String username) {this.username = username;}/** * @return the password */public String getPassword() {return password;}/** * @param password * the password to set */public void setPassword(String password) {this.password = password;}/** * @return the driverClassName */public String getDriverClassName() {return driverClassName;}/** * @param driverClassName * the driverClassName to set */public void setDriverClassName(String driverClassName) {this.driverClassName = driverClassName;}}
以上配置已经结束,只需要在使用的地方进行针对不同SessionFactory的注入即可,注入关键字@Qualifier 阅读全文
0 0
- SpringBoot 配置Mysql多数据源DataSource以及各种工作环境切换
- Springboot多数据源配置--数据源动态切换
- springboot 配置多数据源
- springboot 配置多数据源
- springboot 多数据源配置
- Springboot 多数据源配置
- SpringBoot配置多数据源
- Springboot MyBatis多数据源切换
- Springboot MyBatis多数据源切换
- springboot的多数据源配置
- SpringBoot、MyBatis配置多数据源
- springboot下配置多数据源
- SpringBoot的多数据源配置
- springboot+mybatis配置多数据源
- SpringBoot多数据源及事务配置
- Springboot配置Mybatis多数据源
- springBoot+mybatis多数据源的配置
- SpringBoot下配置Mybatis多数据源
- jsoncpp在linux和windows下的编译及使用详解
- JSP标准标签库(JSTL)core、format
- 给小涵同学的Java入门学习笔记之起步篇
- STL的stack和queue读书笔记
- 【剑指offer】面试题11:旋转数组的最小数字
- SpringBoot 配置Mysql多数据源DataSource以及各种工作环境切换
- 基于Swift的iOS应用程序开发:创建Swift与Objective-C之间的桥接头文件
- Error:Failed to open zip file. Gradle's dependency cache may be corrupt
- 方法和数组
- 数组与链表的区别
- 设计模式讲解与代码实践(八)——桥接
- 读鸟哥linux私房菜收获总结(20170718)
- js shell 运行javascript文件
- 多重循环+方法+递归(新手作业)