michaelin007 2024-01-29 03:51:28 +00:00
parent b9c8a43ff0
commit f0b95c51c4
3 changed files with 114 additions and 2 deletions

View File

@ -2,6 +2,10 @@ package com.baeldung.r2dbc.configuration;
import io.r2dbc.h2.H2ConnectionConfiguration;
import io.r2dbc.h2.H2ConnectionFactory;
import io.r2dbc.spi.Connection;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.ConnectionFactoryMetadata;
import org.reactivestreams.Publisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;

View File

@ -7,12 +7,19 @@ import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table
public class Player {
@Id
Integer id;
String name;
Integer age;
public Player() {
}
public Player(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
}

View File

@ -0,0 +1,101 @@
package com.baeldung.r2dbc;
import com.baeldung.r2dbc.configuration.R2DBCConfiguration;
import com.baeldung.r2dbc.model.Player;
import com.baeldung.r2dbc.repository.PlayerRepository;
import io.r2dbc.h2.H2ConnectionFactory;
import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.relational.core.query.Query;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.test.context.junit4.SpringRunner;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Hooks;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.util.Arrays;
import java.util.List;
import static org.springframework.data.relational.core.query.Criteria.where;
import static org.springframework.data.relational.core.query.Query.query;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = R2DBCConfiguration.class)
public class R2dbcApplicationRdbcTemplateIntegrationTest {
@Autowired
PlayerRepository playerRepository;
@Autowired
DatabaseClient client;
@Autowired
H2ConnectionFactory factory;
ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:h2:mem:///testdb?options=DB_CLOSE_DELAY=-1;TRACE_LEVEL_FILE=4;USER=sa;PASSWORD=");
R2dbcEntityTemplate template = new R2dbcEntityTemplate(connectionFactory);
@Before
public void setup() {
Hooks.onOperatorDebug();
List<String> statements = Arrays.asList("DROP TABLE IF EXISTS player;", "CREATE table player (id INT AUTO_INCREMENT NOT NULL, name VARCHAR2, age INT NOT NULL);");
statements.forEach(it -> client.sql(it)
.fetch()
.rowsUpdated()
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete());
}
@Test
public void whenSearchForSaka_thenOneIsExpected() {
insertPlayers();
template.select(Player.class)
.matching(query(where("name").is("Saka")))
.one()
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();
}
@Test
public void whenInsertThreePlayers_thenThreeAreExpected() {
insertPlayers();
template.select(Player.class)
.all()
.as(StepVerifier::create)
.expectNextCount(3)
.verifyComplete();
}
public void insertPlayers() {
List<Player> players = Arrays.asList(new Player(null, "Saka", 22), new Player(null, "Pedro", 32), new Player(null, "Mbappé", 20));
for (Player player : players) {
template.insert(Player.class)
.using(player)
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();
}
}
}