#413 - Refine reference documentation after Spring R2DBC migration.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.r2dbc.documentation;
|
||||
|
||||
// tag::class[]
|
||||
public class Person {
|
||||
|
||||
private final String id;
|
||||
private final String name;
|
||||
private final int age;
|
||||
|
||||
public Person(String id, String name, int age) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
|
||||
}
|
||||
}
|
||||
// end::class[]}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.r2dbc.documentation;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.data.r2dbc.repository.Modifying;
|
||||
import org.springframework.data.r2dbc.repository.Query;
|
||||
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
|
||||
|
||||
public interface PersonRepository extends ReactiveCrudRepository<Person, String> {
|
||||
|
||||
Flux<Person> findByFirstname(String firstname);
|
||||
|
||||
// tag::atModifying[]
|
||||
@Modifying
|
||||
@Query("UPDATE person SET firstname = :firstname where lastname = :lastname")
|
||||
Mono<Integer> setFixedFirstnameFor(String firstname, String lastname);
|
||||
// end::atModifying[]
|
||||
|
||||
// tag::spel[]
|
||||
@Query("SELECT * FROM person WHERE lastname = :#{[0]}")
|
||||
Flux<Person> findByQueryWithExpression(String lastname);
|
||||
// end::spel[]
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.r2dbc.documentation;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
//@formatter:off
|
||||
// tag::class[]
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration
|
||||
class PersonRepositoryTests {
|
||||
|
||||
@Autowired
|
||||
PersonRepository repository;
|
||||
|
||||
@Test
|
||||
void readsAllEntitiesCorrectly() {
|
||||
|
||||
repository.findAll()
|
||||
.as(StepVerifier::create)
|
||||
.expectNextCount(1)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void readsEntitiesByNameCorrectly() {
|
||||
|
||||
repository.findByFirstname("Hello World")
|
||||
.as(StepVerifier::create)
|
||||
.expectNextCount(1)
|
||||
.verifyComplete();
|
||||
}
|
||||
}
|
||||
// end::class[]
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.r2dbc.documentation;
|
||||
// tag::class[]
|
||||
import io.r2dbc.spi.ConnectionFactories;
|
||||
import io.r2dbc.spi.ConnectionFactory;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
|
||||
|
||||
public class R2dbcApp {
|
||||
|
||||
private static final Log log = LogFactory.getLog(R2dbcApp.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:h2:mem:///test?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
|
||||
|
||||
R2dbcEntityTemplate template = new R2dbcEntityTemplate(connectionFactory);
|
||||
|
||||
template.getDatabaseClient().sql("CREATE TABLE person" +
|
||||
"(id VARCHAR(255) PRIMARY KEY," +
|
||||
"name VARCHAR(255)," +
|
||||
"age INT)")
|
||||
.fetch()
|
||||
.rowsUpdated()
|
||||
.as(StepVerifier::create)
|
||||
.expectNextCount(1)
|
||||
.verifyComplete();
|
||||
|
||||
template.insert(Person.class)
|
||||
.using(new Person("joe", "Joe", 34))
|
||||
.as(StepVerifier::create)
|
||||
.expectNextCount(1)
|
||||
.verifyComplete();
|
||||
|
||||
template.select(Person.class)
|
||||
.first()
|
||||
.doOnNext(it -> log.info(it))
|
||||
.as(StepVerifier::create)
|
||||
.expectNextCount(1)
|
||||
.verifyComplete();
|
||||
}
|
||||
}
|
||||
// tag::class[]
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.r2dbc.documentation;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
|
||||
|
||||
import static org.springframework.data.domain.Sort.*;
|
||||
import static org.springframework.data.domain.Sort.Order.*;
|
||||
import static org.springframework.data.relational.core.query.Criteria.*;
|
||||
import static org.springframework.data.relational.core.query.Query.*;
|
||||
import static org.springframework.data.relational.core.query.Update.*;
|
||||
|
||||
/**
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
//@formatter:off
|
||||
class R2dbcEntityTemplateSnippets {
|
||||
|
||||
void saveAndSelect(R2dbcEntityTemplate template) {
|
||||
|
||||
// tag::insertAndSelect[]
|
||||
Person person = new Person("John", "Doe");
|
||||
|
||||
Mono<Person> saved = template.insert(person);
|
||||
Mono<Person> loaded = template.selectOne(query(where("firstname").is("John")),
|
||||
Person.class);
|
||||
// end::insertAndSelect[]
|
||||
}
|
||||
|
||||
|
||||
void select(R2dbcEntityTemplate template) {
|
||||
|
||||
// tag::select[]
|
||||
Flux<Person> loaded = template.select(query(where("firstname").is("John")),
|
||||
Person.class);
|
||||
// end::select[]
|
||||
}
|
||||
|
||||
void simpleSelect(R2dbcEntityTemplate template) {
|
||||
|
||||
// tag::simpleSelect[]
|
||||
Flux<Person> people = template.select(Person.class) // <1>
|
||||
.all();
|
||||
// end::simpleSelect[]
|
||||
}
|
||||
|
||||
void fullSelect(R2dbcEntityTemplate template) {
|
||||
|
||||
// tag::fullSelect[]
|
||||
Mono<Person> first = template.select(Person.class) // <1>
|
||||
.from("other_person")
|
||||
.matching(query(where("firstname").is("John") // <2>
|
||||
.and("lastname").in("Doe", "White"))
|
||||
.sort(by(desc("id")))) // <3>
|
||||
.one(); // <4>
|
||||
// end::fullSelect[]
|
||||
}
|
||||
|
||||
void insert(R2dbcEntityTemplate template) {
|
||||
|
||||
// tag::insert[]
|
||||
Mono<Person> insert = template.insert(Person.class) // <1>
|
||||
.using(new Person()); // <2>
|
||||
// end::insert[]
|
||||
}
|
||||
|
||||
void fluentUpdate(R2dbcEntityTemplate template) {
|
||||
|
||||
// tag::update[]
|
||||
Mono<Integer> update = template.update(Person.class) // <1>
|
||||
.inTable("other_table") // <2>
|
||||
.matching(query(where("firstname").is("John"))) // <3>
|
||||
.apply(update("age", 42)); // <4>
|
||||
// end::update[]
|
||||
}
|
||||
|
||||
void delete(R2dbcEntityTemplate template) {
|
||||
|
||||
// tag::delete[]
|
||||
Mono<Integer> delete = template.delete(Person.class) // <1>
|
||||
.from("other_table") // <2>
|
||||
.matching(query(where("firstname").is("John"))) // <3>
|
||||
.all(); // <4>
|
||||
// end::delete[]
|
||||
}
|
||||
|
||||
static class Person {
|
||||
String firstname, lastname;
|
||||
public Person(String firstname, String lastname) {
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
}}
|
||||
}
|
||||
Reference in New Issue
Block a user