1、依赖
这里数据源使用的是postgresql
org.liquibase liquibase-coreorg.postgresql postgresql42.2.14 com.baomidou mybatis-plus-boot-starter3.4.1
2、配置
spring:
liquibase:
# 开启 liquibase
enabled: true
# 配置 changlog 文件路径
change-log: classpath:liquibase/master.xml
url: jdbc:postgresql://${POSTGRES_HOST:39.98.xxx.xxx}:${POSTGRES_PORT:xxx}/${POSTGRES_DATAbase:xxx}?sslmode=disable¤tSchema=${POSTGRES_CURRENTSCHEMA:test}
user: ${POSTGRES_USERNAME:xxx}
password: ${POSTGRES_PASSWORD:123456}
datasource:
username: ${POSTGRES_USERNAME:xxx}
password: ${POSTGRES_PASSWORD:123456}
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://${POSTGRES_HOST:39.98.xxx.xxx}:${POSTGRES_PORT:xxxx}/${POSTGRES_DATAbase:xxx}?sslmode=disable¤tSchema=${POSTGRES_CURRENTSCHEMA:test}
mybatis-plus:
mapper-locations=classpath: mapper/*xml
3、配置类
@Configuration
public class LiquibaseConfig {
@Qualifier("dataSource")
private DataSource dataSource;
// 更多配置:
//
// spring.liquibase.change-log 配置文件的路径,默认值为 classpath:/db/changelog/db.changelog-master.yaml
// spring.liquibase.check-change-log-location 检查 change log的位置是否存在,默认为true.
// spring.liquibase.contexts 用逗号分隔的运行环境列表。
// spring.liquibase.default-schema 默认数据库 schema
// spring.liquibase.drop-first 是否先 drop schema(默认 false)
// spring.liquibase.enabled 是否开启 liquibase(默认为 true)
// spring.liquibase.password 数据库密码
// spring.liquibase.url 要迁移的JDBC URL,如果没有指定的话,将使用配置的主数据源.
// spring.liquibase.user 数据用户名
// spring.liquibase.rollback-file 执行更新时写入回滚的 SQL文件
@Bean
public SpringLiquibase liquibase() {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setDataSource(dataSource);
liquibase.setChangeLog("classpath:liquibase/master.xml");
// liquibase.setContexts("development,test,preproduction,production");
// liquibase.setShouldRun(true);
return liquibase;
}
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}
4、创建master.xml
5、启动项目
自动生成两张表
6、简单使用
创建表
启动项目
其他
// 新增删除字段
// 修改字段的类型
// 如果master.xml 写的太多了,可以在另一个文件中继续写
以后各个环境的表结构,都能自动同步啦



