SpringBoot 配置Mysql多数据源DataSource以及各种工作环境切换
2017-07-18 22:39
661 查看
公司内部项目从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.Driver2.
@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
相关文章推荐
- springboot+DruidDataSource 双数据源配置
- Spring Boot 动态数据源(yml配置,多数据源自动切换)
- 各种数据库(oracle、mysql、sqlserver等)在Spring中数据源的配置和JDBC驱动包
- Spring Boot中Datasource配置(Spring Boot dbcp2数据源配置)
- 各种数据库(oracle、mysql、sqlserver等)在Spring中数据源的配置和JDBC驱动包 .
- springboot打包jar包找不到jsp文件以及在linux永久执行java -jar以及springboot的多环境配置
- Springboot使用maven的profiles实现多环境配置文件切换
- Spring-boot 配置dataSource 可配多数据源
- Mybatis在非spring环境下配置文件中使用外部数据源(druidDatasource)
- springboot配置双数据源 MySQL和SqlServer
- 从零开始,配置环境以及第一个spring boot程序
- Spring Boot教程 - Spring Boot Profiles实现多环境下配置切换
- Spring boot + MySQl + druid + JPA + AOP pom文件 以及 application.yml 的配置
- Springboot+Mybatis+Pagehelper+Aop动态配置Oracle、Mysql数据源
- Spring环境下Mybatis的配置以及工作机制
- spring + druid 配置动态配置数据源以及多数据源切换功能实现
- spring BasicDataSource 数据源配置 sqlserver数据库 oracle数据库 mysql数据jdbc配置
- Springboot+Ebean配置Mysql数据源报错
- SpringCloud入门之Spring Boot多环境配置切换指南
- 快速搭建springboot框架以及整合ssm+shiro+安装Rabbitmq和Erlang、Mysql下载与配置