开始使用 Spring Boot 和 Liquibase

305 阅读5分钟

第一步:新建一个Spring Boot项目

要创建一个新的 Spring Boot 项目,您可以使用 Spring Initializr。这个基于 Web 的工具允许您生成一个新的 Spring Boot 项目,其中包含所有必要的依赖项和配置。

  1. 打开 Web 浏览器并导航到 Spring Initializr 网站start.spring.io/。
  2. 填写以下详细信息: – 项目:Maven 项目 – 语言:Java – Spring Boot:3.0.5 – 组:com.springboot – 工件:learningLiquibase – 包装:Jar – Java:11 – 描述:Spring Boot & Liquibase 入门
  3. 单击添加依赖项并添加以下依赖项: – Spring Web – Spring Data JPA – H2 数据库 – Liquibase Migration
  4. 单击生成并下载生成的 zip 文件。
  5. 所有配置完成后,设置应如下所示。

第二步:设置数据库

在此示例中,我们将使用 H2 数据库。Spring Boot 自带 H2 的自动配置,所以我们不需要额外的配置。

  • 打开位于 src/main/resources 目录下的 application.properties 文件,添加如下属性
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=learner
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialectydsfjbm

这些属性为我们的应用程序配置 H2 数据库。

  • 在 src/main/resources 目录中创建一个名为 schema.sql 的新文件,并添加以下 SQL 语句:
CREATE TABLE customer (
  id INT PRIMARY KEY,
  name VARCHAR(50)
);

这将创建一个名为 customer 的表,其中包含两列:id 和 name。

第 3 步:配置 Liquibase

Liquibase 提供了一种对数据库模式进行版本控制和管理数据库更改的方法。在此步骤中,我们将为我们的 Spring Boot 应用程序配置 Liquibase。

  • 在 src/main/resources 目录中创建一个名为 liquibase.properties 的新文件并添加以下属性:
spring.liquibase.change-log=classpath:/db/changelog/db.changelog-master.yaml

此属性告诉 Liquibase 在哪里可以找到更改日志文件。

  • 在 src/main/resources 目录下创建如下目录路径 db/changelog。
  • 在 src/main/resources/db/changelog 目录下创建一个名为 db.changelog-master.yaml 的新文件并添加以下内容:
databaseChangeLog:
  - include:
      file: classpath:/db/changelog/changes/001-initial-schema.sql

该文件定义了一个主变更日志,其中包括一个名为 001-initial-schema.sql 的变更日志文件。

  • 在 src/main/resources/db/changelog 目录中创建一个名为 changes 的新目录。
  • 在 src/main/resources/db/changelog/changes 目录下创建一个名为 001-initial-schema.sql 的新文件,并添加以下 SQL 语句:
CREATE TABLE customer (
  id INT PRIMARY KEY,
  name VARCHAR

此文件包含我们添加到 schema.sql 的相同 SQL 语句。这是因为我们希望 Liquibase 创建与我们在 schema.sql 中定义的相同的数据库模式。

第 4 步:创建 REST 控制器

  • 在src/main/java/com/springboot/learningLiquibase目录下新建一个名为CustomerController.java的文件,添加如下内容:
   package com.springboot.learningLiquibase;

   import org.springframework.beans.factory.annotation.Autowired;
   import org.springframework.http.HttpStatus;
   import org.springframework.http.ResponseEntity;
   import org.springframework.web.bind.annotation.GetMapping;
   import org.springframework.web.bind.annotation.RestController;

   import java.util.List;

   @RestController
   public class CustomerController {

       @Autowired
       private CustomerRepository customerRepository;

       @GetMapping("/customers")
       public ResponseEntity<List<Customer>> getAllCustomers() {
           List<Customer> customers = customerRepository.findAll();
           return new ResponseEntity<>(customers, HttpStatus.OK);
       }
   }

此类定义了一个 REST 控制器,该控制器具有一个返回客户列表的端点。它使用 CustomerRepository 从数据库中检索数据。

  • 在src/main/java/com/springboot/learningLiquibase目录下新建一个名为CustomerRepository.java的文件,添加如下内容:
package com.springboot.learningLiquibase;

import org.springframework.data.jpa.repository.JpaRepository;

public interface CustomerRepository extends JpaRepository<Customer, Integer> {
}

该接口扩展了 JpaRepository 接口并为 Customer 实体定义了一个存储库。它提供了常见的数据库操作方法,例如 findAll()、findById()、save() 和 delete()。

  • 在src/main/java/com/springboot/learningLiquibase目录下新建文件Customer.java,添加如下内容:
package com.springboot.learningLiquibase;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Customer {

    @Id
    private Integer id;

    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

此类定义具有两个属性的 Customer 实体:id 和 name。它使用 JPA 注释将实体映射到数据库中的客户表。

第 5 步:运行应用程序

  • 打开终端窗口并导航到项目的根目录。
  • 通过运行以下命令构建应用程序
java -jar target/liquibase-demo-0.0.1-SNAPSHOT.jar

这将启动应用程序并侦听传入的 HTTP 请求。

第 6 步:测试应用程序

  • 打开 Web 浏览器并导航到http://localhost:8080/customers。
  • 您应该会看到一个包含空数组 [] 的 JSON 响应。这是因为我们仍然需要将客户添加到数据库中。
  • 打开命令提示符并导航到项目的根目录。
  • 运行以下命令启动 H2 控制台:
java -jar target/liquibase-demo-0.0.1-SNAPSHOT.jar --spring.h2.console.enabled=true

这将启动 H2 控制台并打开 Web 浏览器窗口。

  • 在 H2 控制台中,输入以下 JDBC URL:
jdbc:h2:mem:testdb

这将连接到内存中的 H2 数据库。

  • 单击“连接”按钮。
  • 您应该在表列表中看到 CUSTOMER 表。点击表格
  • 单击 SQL 选项卡并输入以下 SQL 语句以插入新客户:
INSERT INTO CUSTOMER (ID, NAME) VALUES (1, 'John Doe');

这将在 CUSTOMER 表中插入一个新行,其 ID 为 1,名称为“John Doe”。

  • 单击“运行”按钮以执行 SQL 语句。
  • 刷新 Web 浏览器窗口,您应该会看到一个 JSON 响应,其中包含 ID 为 1 且名称为“John Doe”的单个客户。

恭喜!您已经成功创建了一个集成了 Liquibase 的 Spring Boot 应用程序,并通过将新客户添加到数据库并通过 REST API 检索数据对其进行了测试。

第 7 步:附加配置

  • 默认情况下,Liquibase 将在 resources/db/changelog 目录中查找名为 db.changelog-master.yaml 的变更日志文件。您可以通过在 application.properties 文件中设置 spring.liquibase.change-log 属性来更改文件的位置。
  • 例如,要使用项目根目录中名为 liquibase-changelog.yaml 的更新日志文件,请将以下行添加到 application.properties 文件:
spring.liquibase.change-log=classpath:/liquibase-changelog.yaml
  • 您还可以将 Liquibase 配置为使用与 H2 不同的数据库。为此,您需要将适当的 JDBC 驱动程序依赖项添加到 pom.xml 文件,并在 application.properties 文件中设置 spring.datasource.url、spring.datasource.username 和 spring.datasource.password 属性。例如,要使用 MySQL 作为数据库,请在 pom.xml 文件中添加以下依赖项:
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.23</version>
</dependency>

然后,在 application.properties 文件中设置以下属性:

spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=password
  • 分别用您自己的数据库名称、用户名和密码替换 mydb、root 和密码。

就是这样!您现在拥有一个功能齐全且集成了 Liquibase 的 Spring Boot 应用程序。