spring data r2dbc可以生成schema吗?

我正在使用 R2DBC 和 H2 创建一个快速项目,以熟悉这种新的反应性东西。制作了一个扩展 ReactiveCrudRepository 的存储库,只要我使用 DatabaseClient 发出与我的实体首先匹配的 CREATE TABLE 语句,一切都很好......

我了解 spring data R2DBC 的功能不如 spring data JPA(还?),但是目前有没有办法从实体类生成模式?


森栏
浏览 54回答 4
4回答

杨__羊羊

不,目前无法使用 Spring Data R2DBC 从实体生成模式。我在一个带有 Postgres DB 的项目中使用它,管理数据库迁移很复杂,但我设法在启动时使用同步 Postgre 驱动程序(Flyway 尚不支持反应式驱动程序)连接 Flyway 来处理架构迁移。即使您仍然需要编写自己的 CREATE TABLE 语句,这应该不那么难,您甚至可以在一些简单的项目中修改实体以创建 JPA 实体并让 Hibernate 创建架构,然后将其复制粘贴到您的迁移文件中R2DBC 项目。

慕哥6287543

可用于测试和生产。我确保您的用户无权更改架构,否则您可能会错误地删除表!或者使用flyway之类的迁移工具。您需要将 schema.sql 放入主资源中并添加相关属性spring.r2dbc.initialization-mode=alwaysh2 用于测试,postgres 用于生产我使用gradle,驱动程序的版本是:    implementation 'org.springframework.boot.experimental:spring-boot-actuator-autoconfigure-r2dbc'    runtimeOnly 'com.h2database:h2'    runtimeOnly 'io.r2dbc:r2dbc-h2'    runtimeOnly 'io.r2dbc:r2dbc-postgresql'    runtimeOnly 'org.postgresql:postgresql'    testImplementation 'org.springframework.boot.experimental:spring-boot-test-autoconfigure-r2dbc'BOM 版本为dependencyManagement {    imports {        mavenBom 'org.springframework.boot.experimental:spring-boot-bom-r2dbc:0.1.0.M3'    }}

一只萌萌小番薯

实际上可以通过这样定义特定的类来加载模式:import io.r2dbc.spi.ConnectionFactoryimport org.springframework.context.annotation.Beanimport org.springframework.context.annotation.Configurationimport org.springframework.core.io.ClassPathResourceimport org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositoriesimport org.springframework.r2dbc.connection.init.ConnectionFactoryInitializerimport org.springframework.r2dbc.connection.init.ResourceDatabasePopulator@Configuration@EnableR2dbcRepositoriesclass DbConfig {    @Bean    fun initializer(connectionFactory: ConnectionFactory): ConnectionFactoryInitializer {        val initializer = ConnectionFactoryInitializer()        initializer.setConnectionFactory(connectionFactory)        initializer.setDatabasePopulator(            ResourceDatabasePopulator(                ClassPathResource("schema.sql")            )        )        return initializer    }}请注意,IntelliJ 会给出错误“无法自动装配。未找到‘ConnectionFactory’类型的 beans ”,但这实际上是误报。因此,忽略它并重新构建您的项目。该schema.sql文件必须放在资源文件夹中。

蓝山帝景

这就是我解决这个问题的方法:控制器:&nbsp; &nbsp; @PostMapping(MAP + PATH_DDL_PROC_DB)&nbsp; //PATH_DDL_PROC_DB = "/database/{db}/{schema}/{table}"&nbsp; &nbsp; public Flux<Object> createDbByDb(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @PathVariable("db") String db,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @PathVariable("schema") String schema,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @PathVariable("table") String table) {&nbsp; &nbsp; &nbsp; &nbsp; return ddlProcService.createDbByDb(db,schema,table);服务:&nbsp; &nbsp; public Flux<Object> createDbByDb(String db,String schema,String table) {&nbsp; &nbsp; &nbsp; &nbsp; return ddl.createDbByDb(db,schema,table);&nbsp; &nbsp; }存储库:&nbsp; &nbsp; @Autowired&nbsp; &nbsp; PostgresqlConnectionConfiguration.Builder connConfig;&nbsp; &nbsp; public Flux<Object> createDbByDb(String db,String schema,String table) {&nbsp; &nbsp; &nbsp; &nbsp; return createDb(db).thenMany(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Mono.from(connFactory(connConfig.database(db)).create())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMapMany(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connection ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Flux.from(connection&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .createBatch()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add(sqlCreateSchema(db))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add(sqlCreateTable(db,table))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add(sqlPopulateTable(db,table))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .execute()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)));&nbsp; &nbsp; }&nbsp; &nbsp; private Mono<Void> createDb(String db) {&nbsp; &nbsp; &nbsp; &nbsp; PostgresqlConnectionFactory&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connectionFactory = connFactory(connConfig);&nbsp; &nbsp; &nbsp; &nbsp; DatabaseClient ddl = DatabaseClient.create(connectionFactory);&nbsp; &nbsp; &nbsp; &nbsp; return ddl&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .execute(sqlCreateDb(db))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .then();&nbsp; &nbsp; }连接类别:@Slf4j@Configuration@EnableR2dbcRepositoriespublic class Connection extends AbstractR2dbcConfiguration {&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp;**********************************************&nbsp; &nbsp; &nbsp;* Spring Data JDBC:&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; DDL: does not support JPA.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* R2DBC&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; DDL:&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -does no support JPA&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -To achieve DDL, uses R2dbc.DataBaseClient&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; DML:&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -it uses R2dbcREpositories&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -R2dbcRepositories is different than&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; R2dbc.DataBaseClient&nbsp; &nbsp; &nbsp;* ********************************************&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; @Bean&nbsp; &nbsp; public PostgresqlConnectionConfiguration.Builder connectionConfig() {&nbsp; &nbsp; &nbsp; &nbsp; return PostgresqlConnectionConfiguration&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .host("db-r2dbc")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .port(5432)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .username("root")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .password("root");&nbsp; &nbsp; }&nbsp; &nbsp; @Bean&nbsp; &nbsp; public PostgresqlConnectionFactory connectionFactory() {&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new PostgresqlConnectionFactory(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connectionConfig().build()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }}DDL 脚本:@Getter@NoArgsConstructor(access = AccessLevel.PRIVATE)public final class DDLScripts {&nbsp; &nbsp; public static final String SQL_GET_TASK = "select * from tasks";&nbsp; &nbsp; public static String sqlCreateDb(String db) {&nbsp; &nbsp; &nbsp; &nbsp; String sql = "create database %1$s;";&nbsp; &nbsp; &nbsp; &nbsp; String[] sql1OrderedParams = quotify(new String[]{db});&nbsp; &nbsp; &nbsp; &nbsp; String finalSql = format(sql,(Object[]) sql1OrderedParams);&nbsp; &nbsp; &nbsp; &nbsp; return finalSql;&nbsp; &nbsp; }&nbsp; &nbsp; public static String sqlCreateSchema(String schema) {&nbsp; &nbsp; &nbsp; &nbsp; String sql = "create schema if not exists %1$s;";&nbsp; &nbsp; &nbsp; &nbsp; String[] sql1OrderedParams = quotify(new String[]{schema});&nbsp; &nbsp; &nbsp; &nbsp; return format(sql,(Object[])&nbsp; sql1OrderedParams);&nbsp; &nbsp; }&nbsp; &nbsp; public static String sqlCreateTable(String schema,String table) {&nbsp; &nbsp; &nbsp; &nbsp; String sql1 = "create table %1$s.%2$s " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "(id serial not null constraint tasks_pk primary key, " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "lastname varchar not null); ";&nbsp; &nbsp; &nbsp; &nbsp; String[] sql1OrderedParams = quotify(new String[]{schema,table});&nbsp; &nbsp; &nbsp; &nbsp; String sql1Final = format(sql1,(Object[])&nbsp; sql1OrderedParams);&nbsp; &nbsp; &nbsp; &nbsp; String sql2 = "alter table %1$s.%2$s owner to root; ";&nbsp; &nbsp; &nbsp; &nbsp; String[] sql2OrderedParams = quotify(new String[]{schema,table});&nbsp; &nbsp; &nbsp; &nbsp; String sql2Final = format(sql2,(Object[])&nbsp; sql2OrderedParams);&nbsp; &nbsp; &nbsp; &nbsp; return sql1Final + sql2Final;&nbsp; &nbsp; }&nbsp; &nbsp; public static String sqlPopulateTable(String schema,String table) {&nbsp; &nbsp; &nbsp; &nbsp; String sql = "insert into %1$s.%2$s values (1, 'schema-table-%3$s');";&nbsp; &nbsp; &nbsp; &nbsp; String[] sql1OrderedParams = quotify(new String[]{schema,table,schema});&nbsp; &nbsp; &nbsp; &nbsp; return format(sql,(Object[]) sql1OrderedParams);&nbsp; &nbsp; }&nbsp; &nbsp; private static String[] quotify(String[] stringArray) {&nbsp; &nbsp; &nbsp; &nbsp; String[] returnArray = new String[stringArray.length];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < stringArray.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; returnArray[i] = "\"" + stringArray[i] + "\"";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return returnArray;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java