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
In this first step you defined a common base interface for all your domain repositories and exposed `findOne(…)` as well as `save(…)`.These methods will be routed into the base repository implementation of the store of your choice provided by Spring Data ,e.g. in the case if JPA `SimpleJpaRepository`, because they are matching the method signatures in `CrudRepository`. So the `UserRepository` will now be able to save users, and find single ones by id, as well as triggering a query to find `Users` by their email address.
212
+
In this first step you defined a common base interface for all your domain repositories and exposed `findById(…)` as well as `save(…)`.These methods will be routed into the base repository implementation of the store of your choice provided by Spring Data ,e.g. in the case if JPA `SimpleJpaRepository`, because they are matching the method signatures in `CrudRepository`. So the `UserRepository` will now be able to save users, and find single ones by id, as well as triggering a query to find `Users` by their email address.
211
213
212
214
NOTE: Note, that the intermediate repository interface is annotated with `@NoRepositoryBean`. Make sure you add that annotation to all repository interfaces that Spring Data should not create instances for at runtime.
@@ -946,15 +948,15 @@ Several Spring Data modules offer integration with Querydsl via `QueryDslPredica
946
948
----
947
949
public interface QueryDslPredicateExecutor<T> {
948
950
949
-
T findOne(Predicate predicate); <1>
951
+
Optional<T> findById(Predicate predicate); <1>
950
952
951
-
Iterable<T> findAll(Predicate predicate); <2>
953
+
Iterable<T> findAll(Predicate predicate); <2>
952
954
953
-
long count(Predicate predicate); <3>
955
+
long count(Predicate predicate); <3>
954
956
955
-
boolean exists(Predicate predicate); <4>
957
+
boolean exists(Predicate predicate); <4>
956
958
957
-
// … more functionality omitted.
959
+
// … more functionality omitted.
958
960
}
959
961
----
960
962
<1> Finds and returns a single entity matching the `Predicate`.
@@ -999,7 +1001,7 @@ Spring Data modules ships with a variety of web support if the module supports t
999
1001
@Configuration
1000
1002
@EnableWebMvc
1001
1003
@EnableSpringDataWebSupport
1002
-
class WebConfiguration {}
1004
+
class WebConfiguration {}
1003
1005
----
1004
1006
====
1005
1007
@@ -1035,10 +1037,10 @@ The `DomainClassConverter` allows you to use domain types in your Spring MVC con
1035
1037
----
1036
1038
@Controller
1037
1039
@RequestMapping("/users")
1038
-
public class UserController {
1040
+
class UserController {
1039
1041
1040
1042
@RequestMapping("/{id}")
1041
-
public String showUserForm(@PathVariable("id") User user, Model model) {
1043
+
String showUserForm(@PathVariable("id") User user, Model model) {
1042
1044
1043
1045
model.addAttribute("user", user);
1044
1046
return "userForm";
@@ -1047,7 +1049,7 @@ public class UserController {
1047
1049
----
1048
1050
====
1049
1051
1050
-
As you can see the method receives a User instance directly and no further lookup is necessary. The instance can be resolved by letting Spring MVC convert the path variable into the id type of the domain class first and eventually access the instance through calling `findOne(…)` on the repository instance registered for the domain type.
1052
+
As you can see the method receives a User instance directly and no further lookup is necessary. The instance can be resolved by letting Spring MVC convert the path variable into the id type of the domain class first and eventually access the instance through calling `findById(…)` on the repository instance registered for the domain type.
1051
1053
1052
1054
NOTE: Currently the repository has to implement `CrudRepository` to be eligible to be discovered for conversion.
1053
1055
@@ -1061,12 +1063,16 @@ The configuration snippet above also registers a `PageableHandlerMethodArgumentR
1061
1063
----
1062
1064
@Controller
1063
1065
@RequestMapping("/users")
1064
-
public class UserController {
1066
+
class UserController {
1065
1067
1066
-
@Autowired UserRepository repository;
1068
+
private final UserRepository repository;
1069
+
1070
+
UserController(UserRepository repository) {
1071
+
this.repository = repository;
1072
+
}
1067
1073
1068
1074
@RequestMapping
1069
-
public String showUsers(Model model, Pageable pageable) {
@@ -1324,21 +1330,20 @@ Given you are developing a Spring MVC web application you typically have to reso
1324
1330
----
1325
1331
@Controller
1326
1332
@RequestMapping("/users")
1327
-
public class UserController {
1333
+
class UserController {
1328
1334
1329
1335
private final UserRepository userRepository;
1330
1336
1331
-
@Autowired
1332
-
public UserController(UserRepository userRepository) {
1337
+
UserController(UserRepository userRepository) {
1333
1338
Assert.notNull(repository, "Repository must not be null!");
1334
1339
this.userRepository = userRepository;
1335
1340
}
1336
1341
1337
1342
@RequestMapping("/{id}")
1338
-
public String showUserForm(@PathVariable("id") Long id, Model model) {
1343
+
String showUserForm(@PathVariable("id") Long id, Model model) {
1339
1344
1340
1345
// Do null check for id
1341
-
User user = userRepository.findOne(id);
1346
+
User user = userRepository.findById(id);
1342
1347
// Do null check for user
1343
1348
1344
1349
model.addAttribute("user", user);
@@ -1347,7 +1352,7 @@ public class UserController {
1347
1352
}
1348
1353
----
1349
1354
1350
-
First you declare a repository dependency for each controller to look up the entity managed by the controller or repository respectively. Looking up the entity is boilerplate as well, as it's always a `findOne(…)` call. Fortunately Spring provides means to register custom components that allow conversion between a `String` value to an arbitrary type.
1355
+
First you declare a repository dependency for each controller to look up the entity managed by the controller or repository respectively. Looking up the entity is boilerplate as well, as it's always a `findById(…)` call. Fortunately Spring provides means to register custom components that allow conversion between a `String` value to an arbitrary type.
1351
1356
1352
1357
[[web.legacy.property-editors]]
1353
1358
===== PropertyEditors
@@ -1373,14 +1378,13 @@ If you have configured Spring MVC as in the preceding example, you can configure
1373
1378
----
1374
1379
@Controller
1375
1380
@RequestMapping("/users")
1376
-
public class UserController {
1381
+
class UserController {
1377
1382
1378
1383
@RequestMapping("/{id}")
1379
-
public String showUserForm(@PathVariable("id") User user, Model model) {
1384
+
String showUserForm(@PathVariable("id") User user, Model model) {
0 commit comments