R2dbc 数据库在 Spring 项目中未自动初始化

问题描述 投票:0回答:1

我正在创建一个连接到 r2dbc postgres 的简单 Spring Boot 项目

我的 application.yml 看起来像这样:

  r2dbc:
    url: r2dbc:postgresql://localhost:5432/postgres
    username: postgres
    password: admin
    sql:
      init:
        mode: always
  logging:
    level:
      org.springframework: DEBUG
      io.r2dbc: DEBUG
  data:
    initialization-mode: always


server:
  port: 8088

我的 UserData 模型是:

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "USER_DATA", schema = "management")
public class UserData {
    @Id
    String userId;
    String firstName;
    String lastName;
    String emailId;
    String password;
}

我已根据指定的 SpringBoot 文档在 src/main/resources 文件夹中创建了此 schema.sql 文件

CREATE SCHEMA IF NOT EXISTS management;
CREATE TABLE IF NOT EXISTS user_data (
    user_id SERIAL PRIMARY KEY,
    first_name VARCHAR(100),
    last_name VARCHAR(100),
    email_id VARCHAR(100) UNIQUE,
    password VARCHAR(255)
);
<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>3.3.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.kaori</groupId>
    <artifactId>management</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-postgresql</artifactId>
            <version>0.8.13.RELEASE</version> <!-- Correct version -->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
            <version>1.18.30</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

一旦我启动应用程序,架构就不会创建,表也不会创建。 我已经尝试了各种配置,但似乎无法让它工作

postgresql spring-boot r2dbc
1个回答
0
投票

SQL 初始化属性是

spring.sql.init.mode
,而不是
r2dbc.sql.init.mode
。该属性适用于 Jdbc/R2dbc 等

spring.sql.init.mode=always 

检查我的工作示例

© www.soinside.com 2019 - 2024. All rights reserved.