采用PageHelper分页助手


<!--pagehelper:通用Mapper分页-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

采用PageHelper分页助手_第1张图片

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DictionaryResult<T> {

    private  Long total;
    private List<T> rows;



}

查询功能:Controller

    @GetMapping(value = "/find")
    public ResponseEntity<DictionaryResult<Dictionary>> queryStandard(Integer page, Integer rows){
        try {
            // 查询数据
            DictionaryResult<Dictionary> result = this.dictionaryService.queryStandardByPage(page, rows);
            if(result != null){
                // 资源存在,返回200
                return new ResponseEntity<>(result, HttpStatus.OK);
            }
            // 资源不存在,返回404
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        } catch (Exception e) {
            e.printStackTrace();
            // 出现异常,返回500
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

查询功能:Service

 @Override
    public DictionaryResult<Dictionary> queryStandardByPage(Integer page, Integer rows) {
        // 1.使用分页助手开始分页,指定两个参数:当前页码,每页条数
        PageHelper.startPage(page, rows);
        // 2,然后分页拦截器会自动对接下来的查询进行分页
        List<Dictionary> userDomains = dictionaryDao.findAll();
        // 3,将list封装到分页结果中
        PageInfo<Dictionary> pageInfo = new PageInfo<>(userDomains);
        // 4,将数据封装到DictionaryResult中

        DictionaryResult<Dictionary> result = new DictionaryResult(pageInfo.getTotal(), userDomains);

        return result;
    }

你可能感兴趣的:(采用PageHelper分页助手)