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