You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.adoc
+53-45Lines changed: 53 additions & 45 deletions
Original file line number
Diff line number
Diff line change
@@ -10,45 +10,52 @@ This means that it does rather little out of the box. But it offers plenty of pl
10
10
11
11
== Maven Coordinates
12
12
13
-
```xml
13
+
[source,xml]
14
+
----
14
15
<dependency>
15
16
<groupId>org.springframework.data</groupId>
16
17
<artifactId>spring-data-jdbc</artifactId>
17
18
<version>1.0.0.BUILD-SNAPSHOT</version>
18
19
</dependency>
19
-
```
20
+
----
20
21
21
22
== Features
22
23
23
24
=== CRUD operations
24
25
25
26
In order use Spring Data JDBC you need the following:
26
27
27
-
1. An entity with an attribute marked as _id_ using the spring datas https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/annotation/Id.html[`@Id`] annotation.
28
-
29
-
public class Person {
30
-
@Id
31
-
Integer id;
32
-
}
33
-
34
-
2. A repository
35
-
36
-
public interface PersonRepository extends CrudRepository<Person, Integer> {}
37
-
38
-
3. Add `@EnableJdbcRepositories` to your application context configuration.
39
-
40
-
4. Make sure your application context contains a bean of type `DataSource`.
28
+
1. An entity with an attribute marked as _id_ using the spring datas https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/annotation/Id.html[`@Id`] annotation.
29
+
+
30
+
[source,java]
31
+
----
32
+
public class Person {
33
+
@Id
34
+
Integer id;
35
+
}
36
+
----
37
+
+
38
+
1. A repository
39
+
+
40
+
[source,java]
41
+
----
42
+
public interface PersonRepository extends CrudRepository<Person, Integer> {}
43
+
----
44
+
+
45
+
1. Add `@EnableJdbcRepositories` to your application context configuration.
46
+
1. Make sure your application context contains a bean of type `DataSource`.
41
47
42
48
Now you can get an instance of the repository interface injected into your beans and use it:
43
49
44
-
```java
45
-
@Autowired
46
-
private PersonRepository repository;
50
+
[source,java]
51
+
----
52
+
@Autowired
53
+
private PersonRepository repository;
47
54
48
-
public void someMethod() {
49
-
Person person = repository.save(new Person());
50
-
}
51
-
```
55
+
public void someMethod() {
56
+
Person person = repository.save(new Person());
57
+
}
58
+
----
52
59
53
60
=== Id generation
54
61
@@ -74,39 +81,40 @@ You can tweak that by providing a https://github.com/spring-projects/spring-data
74
81
Spring Data Jdbc triggers events which will get publish to any matching `ApplicationListener` in the application context.
75
82
For example the following listener will get invoked before an aggregate gets saved.
76
83
77
-
``` java
78
-
@Bean
79
-
public ApplicationListener<BeforeSave> timeStampingSaveTime() {
80
-
81
-
return event -> {
82
-
83
-
Object entity = event.getEntity();
84
-
if (entity instanceof Category) {
85
-
Category category = (Category) entity;
86
-
category.timeStamp();
87
-
}
88
-
};
89
-
}
90
-
```
84
+
[source,java]
85
+
----
86
+
@Bean
87
+
public ApplicationListener<BeforeSave> timeStampingSaveTime() {
| before an aggregate root gets saved, i.e. inserted or updated but after the decision was made if it will get updated or deleted.
107
112
The event has a reference to an https://github.com/spring-projects/spring-data-jdbc/blob/master/src/main/java/org/springframework/data/jdbc/core/conversion/AggregateChange.java[`AggregateChange`] instance.
108
113
The instance can be modified by adding or removing https://github.com/spring-projects/spring-data-jdbc/blob/master/src/main/java/org/springframework/data/jdbc/core/conversion/DbAction.java[`DbAction`]s.
0 commit comments