4 回答

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
不,目前無(wú)法使用 Spring Data R2DBC 從實(shí)體生成模式。
我在一個(gè)帶有 Postgres DB 的項(xiàng)目中使用它,管理數(shù)據(jù)庫(kù)遷移很復(fù)雜,但我設(shè)法在啟動(dòng)時(shí)使用同步 Postgre 驅(qū)動(dòng)程序(Flyway 尚不支持反應(yīng)式驅(qū)動(dòng)程序)連接 Flyway 來(lái)處理架構(gòu)遷移。
即使您仍然需要編寫自己的 CREATE TABLE 語(yǔ)句,這應(yīng)該不那么難,您甚至可以在一些簡(jiǎn)單的項(xiàng)目中修改實(shí)體以創(chuàng)建 JPA 實(shí)體并讓 Hibernate 創(chuàng)建架構(gòu),然后將其復(fù)制粘貼到您的遷移文件中R2DBC 項(xiàng)目。

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊
可用于測(cè)試和生產(chǎn)。
我確保您的用戶無(wú)權(quán)更改架構(gòu),否則您可能會(huì)錯(cuò)誤地刪除表!或者使用flyway之類的遷移工具。
您需要將 schema.sql 放入主資源中并添加相關(guān)屬性
spring.r2dbc.initialization-mode=always
h2 用于測(cè)試,postgres 用于生產(chǎn)
我使用gradle,驅(qū)動(dòng)程序的版本是:
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'
}
}

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊
實(shí)際上可以通過這樣定義特定的類來(lái)加載模式:
import io.r2dbc.spi.ConnectionFactory
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.io.ClassPathResource
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories
import org.springframework.r2dbc.connection.init.ConnectionFactoryInitializer
import org.springframework.r2dbc.connection.init.ResourceDatabasePopulator
@Configuration
@EnableR2dbcRepositories
class DbConfig {
@Bean
fun initializer(connectionFactory: ConnectionFactory): ConnectionFactoryInitializer {
val initializer = ConnectionFactoryInitializer()
initializer.setConnectionFactory(connectionFactory)
initializer.setDatabasePopulator(
ResourceDatabasePopulator(
ClassPathResource("schema.sql")
)
)
return initializer
}
}
請(qǐng)注意,IntelliJ 會(huì)給出錯(cuò)誤“無(wú)法自動(dòng)裝配。未找到‘ConnectionFactory’類型的 beans ”,但這實(shí)際上是誤報(bào)。因此,忽略它并重新構(gòu)建您的項(xiàng)目。
該schema.sql文件必須放在資源文件夾中。

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊
這就是我解決這個(gè)問題的方法:
控制器:
@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);
服務(wù):
public Flux<Object> createDbByDb(String db,String schema,String table) {
return ddl.createDbByDb(db,schema,table);
}
存儲(chǔ)庫(kù):
@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
@EnableR2dbcRepositories
public 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;
}
}
添加回答
舉報(bào)