您的位置:首页 > 编程语言 > Java开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java SpringBoot