微人事——分页设置

1、Service层

  • Pageable是JPA的分页类
  • PageRequest.of(page,size,Sort.Direction.DESC,“key”)方法
    page即当前页。
    size代表每页有几条记录第三个参数是排序字段,
    Sort.Direction.DESC是倒序的意思。
    最后一个参数是排序关键字,将按照这个关键字进行排序。
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    EmployeeRepository employeeRepository;

    @Override
    public RespPageBean getEmployeeByPage(Integer page, Integer size) {
        if (page != null && size != null) {
            page= (page-1)*size;
        }
        Pageable pageable = PageRequest.of(page,size, Sort.Direction.DESC,"id");
        Page<Employee> allPage = employeeRepository.findAll(pageable);
        List<Employee> data = allPage.getContent();
        long total = allPage.getTotalElements();
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setData(data);
        respPageBean.setTotal(total);
        return respPageBean;
    }
}
public class RespPageBean {
    private Long total;
    private List<?> data;

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public List<?> getData() {
        return data;
    }

    public void setData(List<?> data) {
        this.data = data;
    }
}

2、Controller层

@RestController
@RequestMapping("/system/emp")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    @GetMapping("/")
    public RespPageBean getEmployeeByPage(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer size){
        return employeeService.getEmployeeByPage(page,size);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值