BAEL-2840 - Spring Data Web Support (#6672)
* Initial Commit * Update UserRepository.java * Update UserControllerIntegrationTest.java * Update UserRepository.java * Update pom.xml
This commit is contained in:
committed by
KevinGilmore
parent
a821cd553d
commit
e89f5ace7a
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.springdatawebsupport.application;
|
||||
|
||||
import com.baeldung.springdatawebsupport.application.entities.User;
|
||||
import com.baeldung.springdatawebsupport.application.repositories.UserRepository;
|
||||
import java.util.stream.Stream;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
CommandLineRunner initialize(UserRepository userRepository) {
|
||||
return args -> {
|
||||
Stream.of("John", "Robert", "Nataly", "Helen", "Mary").forEach(name -> {
|
||||
User user = new User(name);
|
||||
userRepository.save(user);
|
||||
});
|
||||
userRepository.findAll().forEach(System.out::println);
|
||||
};
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.springdatawebsupport.application.controllers;
|
||||
|
||||
import com.baeldung.springdatawebsupport.application.entities.User;
|
||||
import com.baeldung.springdatawebsupport.application.repositories.UserRepository;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.querydsl.binding.QuerydslPredicate;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
public UserController(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
@GetMapping("/users/{id}")
|
||||
public User findUserById(@PathVariable("id") User user) {
|
||||
return user;
|
||||
}
|
||||
|
||||
@GetMapping("/users")
|
||||
public Page<User> findAllUsers(Pageable pageable) {
|
||||
return userRepository.findAll(pageable);
|
||||
}
|
||||
|
||||
@GetMapping("/sortedusers")
|
||||
public Page<User> findAllUsersSortedByName() {
|
||||
Pageable pageable = PageRequest.of(0, 5, Sort.by("name"));
|
||||
return userRepository.findAll(pageable);
|
||||
}
|
||||
|
||||
@GetMapping("/filteredusers")
|
||||
public Iterable<User> getUsersByQuerydslPredicate(@QuerydslPredicate(root = User.class) Predicate predicate) {
|
||||
return userRepository.findAll(predicate);
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.springdatawebsupport.application.entities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
private final String name;
|
||||
|
||||
public User() {
|
||||
this.name = "";
|
||||
}
|
||||
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "id=" + id + ", name=" + name + '}';
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.springdatawebsupport.application.repositories;
|
||||
|
||||
import com.baeldung.springdatawebsupport.application.entities.User;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends PagingAndSortingRepository<User, Long>,
|
||||
QuerydslPredicateExecutor<User> {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user