springboot+liquibase多数据源管理配置
2020-06-13 本文已影响0人
Mr培
- 在项目中引入liquibase依赖
maven仓库
- pom.xml引入方式
<!-- https://mvnrepository.com/artifact/org.liquibase/liquibase-core -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.5.3</version>
</dependency>
- gradle引入方式
// https://mvnrepository.com/artifact/org.liquibase/liquibase-core
compile group: 'org.liquibase', name: 'liquibase-core', version: '3.5.3'
- springbbot中yml的数据源配置
spring:
datasource:
admin:
username: root
password: 123456
jdbc-url: jdbc:mysql://localhost:3306/***数据库名称***?useUnicode=true&amp;characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true
driver-class-name: com.mysql.jdbc.Driver
liquibase:
change-log: classpath:db/admin/liquibase-master.yml
web:
username: root
password: 123456
jdbc-url: jdbc:mysql://localhost:3306/***数据库名称***?useUnicode=true&amp;characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true
driver-class-name: com.mysql.jdbc.Driver
liquibase:
change-log: classpath:db/web/liquibase-master.yml
- change-log: 配置要执行的liquibase相关的yml文件路径
- 在项目中新增数据源config配置文件
- AdminSessionFactoryConfiguration
package com.cragc.admin.configuration.mybatis;
import liquibase.integration.spring.SpringLiquibase;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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;
/**
* @author Mr培
*/
@Configuration
@MapperScan(basePackages = "com.cragc.admin.domain.mapper.admin", sqlSessionFactoryRef = "adminSqlSessionFactory")
public class AdminSessionFactoryConfiguration {
@Bean(name = "adminDataSource")
@ConfigurationProperties(prefix = "spring.datasource.admin")
public DataSource adminDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 默认使用此事务管理器 如果不想使用 则使用 @Transactional(transactionManager = "transactionManagerWeb") 来指定其他的事务处理器
* */
@Bean(name = "transactionManagerAdmin")
@Qualifier(value = "admin")
@Primary
public DataSourceTransactionManager transactionManagerAdmin() {
return new DataSourceTransactionManager(adminDataSource());
}
@Bean(name = "adminSqlSessionFactory")
public SqlSessionFactory activitySqlSessionFactory(@Qualifier("adminDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sfb = new SqlSessionFactoryBean();
sfb.setDataSource(dataSource);
sfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/admin/**.xml"));
//修改数据库的实体包路径
sfb.setTypeAliasesPackage("com.****.admin.domain.model.admin");
sfb.setVfs(SpringBootVFS.class);
SqlSessionFactory factory = sfb.getObject();
assert factory != null;
factory.getConfiguration().setMapUnderscoreToCamelCase(true);
return factory;
}
/**
* 实现 liquibase在多数据源创建表结构
* liquibase配置
* */
@Bean(name = "adminLiquibaseProperties")
@ConfigurationProperties(prefix = "spring.datasource.admin.liquibase")
public LiquibaseProperties adminLiquibaseProperties() {
return new LiquibaseProperties();
}
@Bean(name = "adminLiquibase")
public SpringLiquibase adminLiquibase() {
return springLiquibase(adminDataSource(), adminLiquibaseProperties());
}
private static SpringLiquibase springLiquibase(DataSource dataSource, LiquibaseProperties properties) {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setDataSource(dataSource);
liquibase.setChangeLog(properties.getChangeLog());
liquibase.setContexts(properties.getContexts());
liquibase.setDefaultSchema(properties.getDefaultSchema());
liquibase.setDropFirst(properties.isDropFirst());
liquibase.setShouldRun(properties.isEnabled());
liquibase.setLabels(properties.getLabels());
liquibase.setChangeLogParameters(properties.getParameters());
liquibase.setRollbackFile(properties.getRollbackFile());
return liquibase;
}
}
- WebSessionFactoryConfiguration
package com.cragc.admin.configuration.mybatis;
import liquibase.integration.spring.SpringLiquibase;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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;
import java.util.Objects;
/**
* @author Mr培
*/
@Configuration
@MapperScan(basePackages = "com.cragc.admin.domain.mapper.web", sqlSessionFactoryRef = "webSqlSessionFactory")
public class WebSessionFactoryConfiguration {
@Bean(name = "webDataSource")
@ConfigurationProperties(prefix = "spring.datasource.web")
public DataSource webDataSource() {
return DataSourceBuilder.create().build();
}
@Qualifier(value = "web")
@Bean(name = "transactionManagerWeb")
public DataSourceTransactionManager transactionManagerWeb() {
return new DataSourceTransactionManager(webDataSource());
}
@Bean(name = "webSqlSessionFactory")
public SqlSessionFactory activitySqlSessionFactory(@Qualifier("webDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sfb = new SqlSessionFactoryBean();
sfb.setDataSource(dataSource);
sfb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/web/**.xml"));
sfb.setTypeAliasesPackage("com.cragc.admin.domain.model.web");
sfb.setVfs(SpringBootVFS.class);
SqlSessionFactory factory = sfb.getObject();
Objects.requireNonNull(factory).getConfiguration().setMapUnderscoreToCamelCase(true);
return factory;
}
/**
* liquibase配置
* */
@Bean(name = "webLiquibaseProperties")
@ConfigurationProperties(prefix = "spring.datasource.web.liquibase")
public LiquibaseProperties webLiquibaseProperties() {
return new LiquibaseProperties();
}
@Bean(name = "webLiquibase")
public SpringLiquibase webLiquibase() {
return springLiquibase(webDataSource(), webLiquibaseProperties());
}
private static SpringLiquibase springLiquibase(DataSource dataSource, LiquibaseProperties properties) {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setDataSource(dataSource);
liquibase.setChangeLog(properties.getChangeLog());
liquibase.setDefaultSchema(properties.getDefaultSchema());
liquibase.setContexts(properties.getContexts());
liquibase.setDropFirst(properties.isDropFirst());
liquibase.setShouldRun(properties.isEnabled());
liquibase.setChangeLogParameters(properties.getParameters());
liquibase.setLabels(properties.getLabels());
liquibase.setRollbackFile(properties.getRollbackFile());
return liquibase;
}
}
配置好后,启动项目,如果liquibase未启动成功,报错,一定要检查自己的liquibase数据结构是否正确