Mybatis分页插件pagehelper的使用

@Configuration
public class MybatisConfig {
    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");

        pageHelper.setProperties(properties);
        return pageHelper;
    }
}

@Getter
@Setter
public class PagingParam implements Serializable{

    @Min(value = 1, message = "参数[pageNum]有误")
    private int pageNum = 1;

    @Min(value = 1, message = "参数[pageSize]有误")
    private int pageSize = 12;

}
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelperartifactId>
            <version>4.2.1version>
        dependency>
   int pageNum = pagingParam.getPageNum();
   int pageSize = pagingParam.getPageSize();
   PageHelper.startPage(pageNum, pageSize);
 PageInfo pageInfo = new PageInfo<>(List list);  //你的数据源
  pageInfo.setTotal(new PageInfo<>(userInformations).getTotal());
  **return new Result(200, "成功获取所有成员", Result.constructData(pageInfo));**
    public static <T> Map<String, Object> constructData(PageInfo<T> pageInfo){
        Map<String, Object> data = new TreeMap<>();
        data.put("total",pageInfo.getTotal());
        data.put("pageNum",pageInfo.getPageNum());
        if (pageInfo.getList() == null || pageInfo.getList().size() == 0){
            data.put("list", new ArrayList<>());
        }else {
            data.put("list", pageInfo.getList());
        }
        return data;
    }
  public Result selectAllPaging(PagingParam pagingParam, HttpServletRequest res){

        int pageNum = pagingParam.getPageNum();
        int pageSize = pagingParam.getPageSize();

        String organization = "";

        PageHelper.startPage(pageNum, pageSize);
        List userInformations;
        try{
            userInformations = userInformationMapper.selectByOrganization(organization);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400, "未知错误");
        }
        PageInfo pageInfo = new PageInfo<>(copyFromUserInformationToSUserDTO(userInformations, res));
        pageInfo.setTotal(new PageInfo<>(userInformations).getTotal());

        return new Result(200, "成功获取所有成员", Result.constructData(pageInfo));
    }

你可能感兴趣的:(mybatis)