15
4

3 に答える 3

27

Just create a query method in your repository interface for this.

Take a look in the docs, here.

Add a query method with paging and where clause.

Page<User> findByLastname(String lastname, Pageable pageable);

So you can find User by a property "lastname" and configure Paging settings.

于 2013-01-02T10:50:31.937 に答える
4
于 2013-01-02T10:43:06.583 に答える
0

To fetch Spring Data JPA apply sorting, pagination along with a where clause check bellow codes

  1. Entity class

    @Entity
    @Table(name = "users")
    Public Class User{
        private String name;
        private Integer id;
        private String email;
        // add required setter getter//
    }
    
  2. Repository class

    @Repository("userRepository")
    @Transactional`
        public interface UserRepository extends JpaRepository<User, Integer>{
    
        }
    
  3. Service layer to pass where clause using Example object and Pagination+Sorting together using Pagable and Sort Class. We will implement Pageable in controller to inject Sorting logic then will pass that Pagable object to service Layer.

     @Service
     Public class UserService{
        public Page<User> getUserListPaginated(`EXAMPLE<User>` searchTerm, Pageable pageable) {
            return userRepo.findAll(searchTerm,pageable);
        }
    }
    

Example<T> does not take the attribute if its value is null. for that I used Integer id in Entity class as I can set it's value null for first call

  1. Now Controller Class

    @Controller
    Public class UserController{
    
        @Autowired UserService userService;
        @GetMapping("/users")
        public String users(Model model,@RequestParam(value="page",defaultValue = "0") Integer  pageNumber, @SortDefault(sort = "name", direction = Sort.Direction.ASC) Sort sort){
            Pageable pageable = PageRequest.of(pageNumber,10, sort);
            Example<User> searchTerm = Example.of(new User());
            Page<User> userList = userService.getUserListPaginated(searchTerm, pageable);
    
    
        }
    }
    
于 2018-12-29T12:30:10.457 に答える