Spring Data Jpa @Query Pageable用法

1.Controller

public ResponseEntity<ResponseData> pageableList(
            @RequestParam(required = true) String appkey,
            @RequestParam(required = true) String token,
            @RequestParam(required = false, defaultValue = "0") String pageNo,
            @RequestParam(required = false, defaultValue = "10") String pageSize){
        //检测页码、条数参数是否为数字
        if(!StrUtil.isNumeric(pageNo) || !StrUtil.isNumeric(pageSize)){
            //页码、条数参数有误
        }
        Integer pageNoNum = Integer.parseInt(pageNo);
        Integer pageSizeNum = Integer.parseInt(pageSize);
        Pageable pageable = null;
        Sort sort = new Sort(Sort.Direction.ASC, "id");
        pageable = new PageRequest(pageNoNum, pageSizeNum, sort);

        User user = userService.getUserByToken(token);
        //获取用户的信息
        List<User> UserList = this.UserService.getUserById(user .getId(), pageable);
        message.setContent(UserList);
        return new ResponseEntity<ResponseData>(message, HttpStatus.OK);
    }

2.Repository

 @Query(value="SELECT * FROM User WHERE ENDTIME is null AND  ID = ?1 order by ?#{#pageable}",nativeQuery = true)
List findByEndtimeIsNullAndIdOrderByStarttime(String id,  Pageable pageable);

你可能感兴趣的:(springboot,jpa)