# SpringBoot - 数据库版本库控制
# JPA
依赖:
<!--jpa 启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
配置:
# 建议开发环境使用 JPA 自动创建表
# 其它环境通过 flyway 控制数据库版本
spring:
jpa:
hibernate:
ddl-auto: update
open-in-view: false
使用: (结合 MyBatisPlus 使用)
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("object_config")
@Entity
// KEY `idx_object_config_biz_object` (`biz_id`,`object_id`)
@Table(indexes = { @Index(name = "idx_object_config_biz_object", columnList = "bizId, objectId") })
public class ObjectConfig implements Serializable {
private static final long serialVersionUID = 1L;
// PRIMARY KEY (`id`),
@Id
@Column(columnDefinition = "bigint COMMENT '业务编码: 业务的配置。雪花算法生成的ID: 对象的配置'", nullable = false)
@TableId(type = IdType.ASSIGN_ID)
private Long id;
@Column(columnDefinition = "bigint COMMENT '业务编码: 1xxyyy'", nullable = false)
private Integer bizId;
@Column(columnDefinition = "bigint COMMENT '业务配置: 直接使用 业务编码'", nullable = false)
private Long objectId;
@Column(columnDefinition = "varchar(256) COMMENT '业务名称。(冗余字段)'")
private String bizName;
@TableField(fill = FieldFill.INSERT)
@Column(columnDefinition = "datetime COMMENT '创建日期'")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createDate;
}
# Flyway
依赖:
<!-- SpringBoot 已经管理该依赖的版本 -->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
</dependency>
配置:
spring:
datasource:
druid:
url: jdbc:mysql://localhost:3306/my-test-db
username: root
password: 123456
spring:
flyway:
enabled: true
encoding: UTF-8
# 可以支持多个location, 用','隔开
locations: classpath:db/migration
# migrate是否校验
validate-on-migrate: true
baseline-on-migrate: true
# 指定 baseline 的版本号,默认值为 1, 低于该版本号的 SQL 文件, migrate 时会被忽略
baseline-version: 1
# 如果配合 druid 使用,会报错 —— SELECT command denied to user 'xx'@'xx' for table 'user_variables_by_thread'
# 明确指定 数据库连接信息: 参考 [flyway与druid报错问题处理](https://blog.csdn.net/qq_64928997/article/details/146015002)
url: ${spring.datasource.druid.url}
user: ${spring.datasource.druid.username}
password: ${spring.datasource.druid.password}
SQL:
src/main/resources/
db/migration/
# baseline-version 默认值为 1,如果是不是 新数据库 则不会执行 V1.0 的 SQL 脚本
# "V版本__文件名.sql"
V2.0__init_db_20260301.sql
V3.0__patch_20260401.sql
# 参考
上一篇: 下一篇:
本章目录