Spring初始化数据库脚本

实现

  • spring初始化
  • liquibase初始化
  1. pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.14</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.idea360</groupId>
<artifactId>auto-ddl-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>auto-ddl-demo</name>
<description>auto-ddl-demo</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>
  1. application.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
datasource:
password: root123456
url: jdbc:mysql://localhost:3306/liquibase
username: root
sql: # spring初始化schema
init:
mode: ALWAYS
schema-locations:
- 'classpath:sql/spring-create-table.sql'
liquibase: # liquibase初始化schema
enabled: true
change-log: classpath:database_changelog.xml
  1. database_changelog.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

<changeSet id="create-table" author="idea360">
<sqlFile path="classpath:sql/liquibase-create-table.sql"/>
</changeSet>

<changeSet id="init-data" author="idea360">
<sqlFile path="classpath:sql/liquibase-init-data.sql"/>
</changeSet>

</databaseChangeLog>
  1. resources/sql

liquibase-create-table.sql

1
2
3
4
5
6
CREATE TABLE liquibase_user_account(
id bigint NOT NULL AUTO_INCREMENT,
username varchar(50) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY (username)
) ENGINE=InnoDB;

liquibase-init-data.sql

1
2
3
INSERT INTO liquibase_user_account (id, username)
VALUES (1, 'zhangsan'),
(2, 'lisi');

spring-create-table.sql

1
2
3
4
5
6
CREATE TABLE spring_user_account(
id bigint NOT NULL AUTO_INCREMENT,
username varchar(50) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY (username)
) ENGINE=InnoDB;

bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Bean
public EmbeddedDatabase embeddedDatabase() {
// @formatter:off
return new EmbeddedDatabaseBuilder()
.generateUniqueName(false)
.setName("authzserver")
.setType(EmbeddedDatabaseType.H2)
.setScriptEncoding("UTF-8")
.addScript("org/springframework/security/oauth2/server/authorization/oauth2-authorization-schema.sql")
.addScript("org/springframework/security/oauth2/server/authorization/oauth2-authorization-consent-schema.sql")
.addScript("org/springframework/security/oauth2/server/authorization/client/oauth2-registered-client-schema.sql")
.build();
// @formatter:on
}