【问题标题】:Spring boot data r2dbc auto create tablesSpring Boot 数据 r2dbc 自动创建表
【发布时间】:2019-12-01 09:44:29
【问题描述】:

我有一个简单的问题,是否可以使用用于 MySQL 或其他数据库的 spring boot 数据 r2dbc 自动生成表?在 JPA 中,我添加了 spring.jpa.hibernate.ddl-auto=update 并创建了表

这是我的 pom:

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot.experimental</groupId>
        <artifactId>spring-boot-starter-data-r2dbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>dev.miku</groupId>
        <artifactId>r2dbc-mysql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot.experimental</groupId>
        <artifactId>spring-boot-test-autoconfigure-r2dbc</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.1.43.Final</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

【问题讨论】:

    标签: java spring-boot reactive spring-data-r2dbc


    【解决方案1】:

    我知道这不是确切的答案,但在 R2DBC 中创建表的唯一方法是利用 databasePopulator:

    @Bean
    fun initializer(connectionFactory: ConnectionFactory) =
        ConnectionFactoryInitializer().apply {
            setConnectionFactory(connectionFactory)
            setDatabasePopulator(CompositeDatabasePopulator()
                .apply {
                    addPopulators(ResourceDatabasePopulator(FileSystemResource("src/test/resources/sql/init.sql")))
                })
        }
    

    它可以在测试期间派上用场。

    【讨论】:

      猜你喜欢
      • 2018-12-30
      • 1970-01-01
      • 2019-11-06
      • 2021-08-13
      • 2019-01-21
      • 1970-01-01
      • 2017-01-16
      • 2019-01-12
      • 2017-10-08
      相关资源
      最近更新 更多