- 在項目中引入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的數(shù)據(jù)源配置
spring:
datasource:
admin:
username: root
password: 123456
jdbc-url: jdbc:mysql://localhost:3306/***數(shù)據(jù)庫名稱***?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/***數(shù)據(jù)庫名稱***?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: 配置要執(zhí)行的liquibase相關(guān)的yml文件路徑
- 在項目中新增數(shù)據(jù)源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();
}
/**
* 默認使用此事務(wù)管理器 如果不想使用 則使用 @Transactional(transactionManager = "transactionManagerWeb") 來指定其他的事務(wù)處理器
* */
@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"));
//修改數(shù)據(jù)庫的實體包路徑
sfb.setTypeAliasesPackage("com.****.admin.domain.model.admin");
sfb.setVfs(SpringBootVFS.class);
SqlSessionFactory factory = sfb.getObject();
assert factory != null;
factory.getConfiguration().setMapUnderscoreToCamelCase(true);
return factory;
}
/**
* 實現(xiàn) liquibase在多數(shù)據(jù)源創(chuàng)建表結(jié)構(gòu)
* 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數(shù)據(jù)結(jié)構(gòu)是否正確