@ -32,49 +32,49 @@ Here is a quick teaser of an application using Spring Data JDBC Repositories in
@@ -32,49 +32,49 @@ Here is a quick teaser of an application using Spring Data JDBC Repositories in
class ApplicationConfig extends AbstractJdbcConfiguration {
@Bean
public DataSource dataSource() {
return …;
}
@Bean
public DataSource dataSource() {
return …;
}
@Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(DataSource dataSource) {
return new NamedParameterJdbcTemplate(dataSource);
}
@Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(DataSource dataSource) {
return new NamedParameterJdbcTemplate(dataSource);
}
}
----
@ -85,9 +85,9 @@ Add the Maven dependency:
@@ -85,9 +85,9 @@ Add the Maven dependency:
[source,xml]
----
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>${version}</version>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>${version}</version>
</dependency>
----
@ -96,15 +96,15 @@ If you'd rather like the latest snapshots of the upcoming major version, use our
@@ -96,15 +96,15 @@ If you'd rather like the latest snapshots of the upcoming major version, use our
[source,xml]
----
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>${version}-SNAPSHOT</version>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>${version}-SNAPSHOT</version>
</dependency>
<repository>
<id>spring-snapshot</id>
<name>Spring Snapshot Repository</name>
<url>https://repo.spring.io/snapshot</url>
<id>spring-snapshot</id>
<name>Spring Snapshot Repository</name>
<url>https://repo.spring.io/snapshot</url>
</repository>
----
@ -116,46 +116,46 @@ Here is a quick teaser of an application using Spring Data R2DBC Repositories in
@@ -116,46 +116,46 @@ Here is a quick teaser of an application using Spring Data R2DBC Repositories in
@ -167,9 +167,9 @@ Add the Maven dependency:
@@ -167,9 +167,9 @@ Add the Maven dependency:
[source,xml]
----
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>${version}</version>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>${version}</version>
</dependency>
----
@ -178,15 +178,15 @@ If you'd rather like the latest snapshots of the upcoming major version, use our
@@ -178,15 +178,15 @@ If you'd rather like the latest snapshots of the upcoming major version, use our
@ -11,10 +11,10 @@ In order to activate auditing, add `@EnableJdbcAuditing` to your configuration,
@@ -11,10 +11,10 @@ In order to activate auditing, add `@EnableJdbcAuditing` to your configuration,
@ -39,13 +39,13 @@ Then enter a project and a package name, such as `org.spring.jdbc.example`.
@@ -39,13 +39,13 @@ Then enter a project and a package name, such as `org.spring.jdbc.example`.
----
<dependencies>
<!-- other dependency elements omitted -->
<!-- other dependency elements omitted -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>{version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>{version}</version>
</dependency>
</dependencies>
----
@ -62,11 +62,11 @@ Then enter a project and a package name, such as `org.spring.jdbc.example`.
@@ -62,11 +62,11 @@ Then enter a project and a package name, such as `org.spring.jdbc.example`.
@ -16,10 +16,10 @@ The easiest way to properly plug MyBatis into Spring Data JDBC is by importing `
@@ -16,10 +16,10 @@ The easiest way to properly plug MyBatis into Spring Data JDBC is by importing `
@ -11,25 +11,25 @@ Defining such a query is a matter of declaring a method on the repository interf
@@ -11,25 +11,25 @@ Defining such a query is a matter of declaring a method on the repository interf
@Query("SELECT * FROM person WHERE username = :#{ principal?.username }")
Person findActiveUser(); <9>
@Query("SELECT * FROM person WHERE username = :#{ principal?.username }")
Person findActiveUser(); <9>
}
----
<1> The method shows a query for all people with the given `firstname`.
@ -156,8 +156,8 @@ The following example shows how to use `@Query` to declare a query method:
@@ -156,8 +156,8 @@ The following example shows how to use `@Query` to declare a query method:
@Query("select firstName, lastName from User u where u.emailAddress = :email")
User findByEmailAddress(@Param("email") String email);
@Query("select firstName, lastName from User u where u.emailAddress = :email")
User findByEmailAddress(@Param("email") String email);
}
----
@ -252,9 +252,9 @@ The following example shows how to register `DefaultQueryMappingConfiguration`:
@@ -252,9 +252,9 @@ The following example shows how to register `DefaultQueryMappingConfiguration`:
@ -12,11 +12,11 @@ If you need to tweak transaction configuration for one of the methods declared i
@@ -12,11 +12,11 @@ If you need to tweak transaction configuration for one of the methods declared i
@ -32,23 +32,24 @@ The following example shows how to create such a facade:
@@ -32,23 +32,24 @@ The following example shows how to create such a facade:
@Service
public class UserManagementImpl implements UserManagement {
private final UserRepository userRepository;
private final RoleRepository roleRepository;
private final UserRepository userRepository;
private final RoleRepository roleRepository;
UserManagementImpl(UserRepository userRepository,
RoleRepository roleRepository) {
this.userRepository = userRepository;
this.roleRepository = roleRepository;
}
UserManagementImpl(UserRepository userRepository,
RoleRepository roleRepository) {
this.userRepository = userRepository;
this.roleRepository = roleRepository;
}
@Transactional
public void addRoleToAllUsers(String roleName) {
@Transactional
public void addRoleToAllUsers(String roleName) {
Role role = roleRepository.findByName(roleName);
Role role = roleRepository.findByName(roleName);
for (User user : userRepository.findAll()) {
user.addRole(role);
userRepository.save(user);
for (User user : userRepository.findAll()) {
user.addRole(role);
userRepository.save(user);
}
}
}
----
@ -69,12 +70,12 @@ To let your query methods be transactional, use `@Transactional` at the reposito
@@ -69,12 +70,12 @@ To let your query methods be transactional, use `@Transactional` at the reposito
@Query("delete from User u where u.active = false")
void deleteInactiveUsers();
@Modifying
@Transactional
@Query("delete from User u where u.active = false")
void deleteInactiveUsers();
}
----
@ -105,8 +106,8 @@ In that cases both modes are equivalent of `PESSIMISTIC_WRITE`.
@@ -105,8 +106,8 @@ In that cases both modes are equivalent of `PESSIMISTIC_WRITE`.
@ -10,10 +10,10 @@ Since Spring Data R2DBC 1.2, auditing can be enabled by annotating a configurati
@@ -10,10 +10,10 @@ Since Spring Data R2DBC 1.2, auditing can be enabled by annotating a configurati
@EnableR2dbcAuditing
class Config {
@Bean
public ReactiveAuditorAware<AuditableUser> myAuditorProvider() {
return new AuditorAwareImpl();
}
@Bean
public ReactiveAuditorAware<AuditableUser> myAuditorProvider() {
@ -41,20 +41,20 @@ Then enter a project and a package name, such as `org.spring.r2dbc.example`.
@@ -41,20 +41,20 @@ Then enter a project and a package name, such as `org.spring.r2dbc.example`.
----
<dependencies>
<!-- other dependency elements omitted -->
<!-- other dependency elements omitted -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>{version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>{version}</version>
</dependency>
<!-- a R2DBC driver -->
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<version>x.y.z</version>
</dependency>
<!-- a R2DBC driver -->
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<version>x.y.z</version>
</dependency>
</dependencies>
----
@ -71,11 +71,11 @@ Then enter a project and a package name, such as `org.spring.r2dbc.example`.
@@ -71,11 +71,11 @@ Then enter a project and a package name, such as `org.spring.r2dbc.example`.
[source,xml]
----
<repositories>
<repository>
<id>spring-milestone</id>
<name>Spring Maven MILESTONE Repository</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-milestone</id>
<name>Spring Maven MILESTONE Repository</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
----
@ -100,10 +100,11 @@ Next, you need to create a table structure in your database, as follows:
@@ -100,10 +100,11 @@ Next, you need to create a table structure in your database, as follows:
[source,sql]
----
CREATE TABLE person
(id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255),
age INT);
CREATE TABLE person(
id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255),
age INT
);
----
You also need a main application to run, as follows:
@ -117,10 +118,11 @@ When you run the main program, the preceding examples produce output similar to
@@ -117,10 +118,11 @@ When you run the main program, the preceding examples produce output similar to
2018-11-28 10:47:04,074 DEBUG amework.core.r2dbc.DefaultDatabaseClient: 908 - Executing SQL statement [INSERT INTO person (id, name, age) VALUES($1, $2, $3)]
2018-11-28 10:47:04,092 DEBUG amework.core.r2dbc.DefaultDatabaseClient: 575 - Executing SQL statement [SELECT id, name, age FROM person]
2018-11-28 10:47:04,436 INFO org.spring.r2dbc.example.R2dbcApp: 43 - Person [id='joe', name='Joe', age=34]
@ -155,11 +157,11 @@ The following example shows an example of using Java-based bean metadata to regi
@@ -155,11 +157,11 @@ The following example shows an example of using Java-based bean metadata to regi
@Configuration
public class ApplicationConfiguration extends AbstractR2dbcConfiguration {
@ -62,16 +62,16 @@ See xref:r2dbc/getting-started.adoc#r2dbc.dialects[R2DBC Drivers] for how to con
@@ -62,16 +62,16 @@ See xref:r2dbc/getting-started.adoc#r2dbc.dialects[R2DBC Drivers] for how to con
@Configuration
public class MyAppConfig extends AbstractR2dbcConfiguration {
public ConnectionFactory connectionFactory() {
return ConnectionFactories.get("r2dbc:…");
}
public ConnectionFactory connectionFactory() {
return ConnectionFactories.get("r2dbc:…");
}
// the following are optional
// the following are optional
@Override
protected List<Object> getCustomConverters() {
return List.of(new PersonReadConverter(), new PersonWriteConverter());
}
@Override
protected List<Object> getCustomConverters() {
return List.of(new PersonReadConverter(), new PersonWriteConverter());
@ -199,13 +199,13 @@ The following example of a Spring Converter implementation converts from a `Row`
@@ -199,13 +199,13 @@ The following example of a Spring Converter implementation converts from a `Row`
[source,java]
----
@ReadingConverter
public class PersonReadConverter implements Converter<Row, Person> {
public class PersonReadConverter implements Converter<Row, Person> {
public Person convert(Row source) {
Person p = new Person(source.get("id", String.class),source.get("name", String.class));
p.setAge(source.get("age", Integer.class));
return p;
}
public Person convert(Row source) {
Person p = new Person(source.get("id", String.class),source.get("name", String.class));
p.setAge(source.get("age", Integer.class));
return p;
}
}
----
@ -222,13 +222,13 @@ The following example converts from a `Person` to a `OutboundRow`:
@@ -222,13 +222,13 @@ The following example converts from a `Person` to a `OutboundRow`:
@WritingConverter
public class PersonWriteConverter implements Converter<Person, OutboundRow> {
@ -10,21 +10,21 @@ Defining such a query is a matter of declaring a method on the repository interf
@@ -10,21 +10,21 @@ Defining such a query is a matter of declaring a method on the repository interf
@ -147,11 +147,11 @@ Using keywords from the preceding table can be used in conjunction with `delete
@@ -147,11 +147,11 @@ Using keywords from the preceding table can be used in conjunction with `delete
@ -192,8 +192,8 @@ The following example shows how to use `@Query` to declare a query method:
@@ -192,8 +192,8 @@ The following example shows how to use `@Query` to declare a query method:
@ -18,12 +18,12 @@ Consider the following `Person` class:
@@ -18,12 +18,12 @@ Consider the following `Person` class:
----
public class Person {
@Id
private Long id;
private String firstname;
private String lastname;
@Id
private Long id;
private String firstname;
private String lastname;
// … getters and setters omitted
// … getters and setters omitted
}
----
@ -34,7 +34,7 @@ The following example shows a repository interface for the preceding `Person` cl
@@ -34,7 +34,7 @@ The following example shows a repository interface for the preceding `Person` cl
----
public interface PersonRepository extends ReactiveCrudRepository<Person, Long> {
// additional custom query methods go here
// additional custom query methods go here
}
----
@ -49,10 +49,10 @@ The following example shows how to use Java configuration for a repository:
@@ -49,10 +49,10 @@ The following example shows how to use Java configuration for a repository:
@EnableR2dbcRepositories
class ApplicationConfig extends AbstractR2dbcConfiguration {