标签:
http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-part-two-crud/
We have now configured the persistence layer of our Spring application. We are finally ready to create our first Spring Data JPA repository.
This blog post describes how we can create a repository that provides CRUD operations for todo entries.
Let’s get started.
If you are not familiar with Spring Data JPA, you should read the following blog posts before you continue reading this blog post:
Before we can create our first Spring Data JPA repository, we have to create an entity class that contains the information of a single todo entry. The relevant part of the Todo class looks as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
import org.hibernate.annotations.Type; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.PrePersist; import javax.persistence.Table; import javax.persistence.Version; import java.time.ZonedDateTime; @Entity @Table (name = "todos" ) final class Todo { @Id @GeneratedValue (strategy = GenerationType.AUTO) private Long id; @Column (name = "creation_time" , nullable = false ) @Type (type = "org.jadira.usertype.dateandtime.threeten.PersistentZonedDateTime" ) private ZonedDateTime creationTime; @Column (name = "description" , length = 500 ) private String description; @Column (name = "modification_time" ) @Type (type = "org.jadira.usertype.dateandtime.threeten.PersistentZonedDateTime" ) private ZonedDateTime modificationTime; @Column (name = "title" , nullable = false , length = 100 ) private String title; @Version private long version; //The constructor, builder, and other methods are omitted } |
We are now ready to create our first Spring Data JPA repository. We can create the repository that provides CRUD operations for Todo objects by using one of the following methods:
Let’s take a closer look at these methods.
If we create our repository by extending the CrudRepository interface, we have to provide two type parameters:
In other words, when we create the repository that provides CRUD operations for Todo objects, we have to provide the following type parameters:
The source code of the TodoRepository interface looks as follows:
1
2
3
4
5
|
import org.springframework.data.repository.CrudRepository; interface TodoRepository extends CrudRepository<Todo, Long> { } |
The CrudRepository interface declares many methods, but the methods that are relevant for this blog post are described in the following:
Let’s find out how we can create a repository interface that extends the Repository interface.
If we create our repository by extending the Repository interface, we have to follow these steps:
The source code of the TodoRepository interface looks as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
import org.springframework.data.repository.Repository; import java.util.List; import java.util.Optional; interface TodoRepository extends Repository<Todo, Long> { void delete(Todo deleted); List<Todo> findAll(); Optional<Todo> findOne(Long id); Todo save(Todo persisted); } |
Additional Reading:
Let’s move on and find out which method we should use.
It depends.
I know that this is probably the most annoying answer one can give to a question. That is why I created two rules that we can follow when we are creating Spring Data JPA repositories. These rules are:
Case closed?
Not exactly. I argue that we should always use the second method. This opinion is based on two reasons:
If we create our repositories by extending the Repository interface and adding the required methods to the created repository interfaces, we need to add the “same” methods to every interface. Right?
Wrong.
We can avoid this by following these steps:
Let’s move on and take a closer look at these steps.
First, we have to create a base interface that declares the methods shared by our repositories. We can do this by following these steps:
The source code of the BaseRepository interface looks as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import org.springframework.data.repository.NoRepositoryBean; import org.springframework.data.repository.Repository; import java.util.List; import java.util.Optional; @NoRepositoryBean interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID> { void delete(T deleted); List<T> findAll(); Optional<T> findOne(ID id); T save(T persisted); } |
Second, we have to create the actual repository interface that extends our base interface. We can do this by following these steps:
The source code of the TodoRepository interface looks as follows:
1
2
3
|
interface TodoRepository extends BaseRepository<Todo, Long> { } |
We have now created a repository hierarchy that allows us to:
The following figure illustrates the benefits of this solution:
Let’s move on and summarize what we learned from this blog post.
This blog post has taught us three things:
The next part of this tutorial describes how we can create database queries by using query methods.
P.S. You can get the example application of this blog post from Github.
Spring Data JPA Tutorial: CRUD
标签:
原文地址:http://www.cnblogs.com/glenblogs/p/4223054.html