Java - PageHelper使用案例

商品分页查询PageHelper使用

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import tk.mybatis.mapper.entity.Example;
import org.springframework.beans.BeanUtils;
import java.util.stream.Collectors;

public PageResult<SpuBo> querySpuByPage(String key, Boolean saleable, Integer page, Integer rows) {
        // 1. 设置分页参数
        PageHelper.startPage(page, rows);

        // 2. 设置查询条件
        Example example = new Example(Spu.class);
        Example.Criteria criteria = example.createCriteria();
            // 先给key判空
            if (StringUtils.isNotBlank(key)) {
                // title是通过key去匹配查询的数据库中的字段, 可自定义
                criteria.andLike("title", "%" + key + "%");
            }
        criteria.andEqualTo("saleable", saleable);

        // 3. 执行查询
        List<Spu> spus = this.spuMapper.selectByExample(example);
//        spus.forEach(sup -> { // .forEach返回原来的数组, .stream.map返回一个新数组
        List<SpuBo> spuBos = spus.stream().map(spu -> {
            SpuBo spuBo = new SpuBo();
            // 3.1属性对拷
            BeanUtils.copyProperties(spu, spuBo);
            // 3.2 设置分类名称 cname -> categoryName
            List<String> names = this.categoryService.queryNamesByIds(
                    Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));
            spuBo.setCname(StringUtils.join(names, " > "));
            // 3.3 设置品牌名称 bname -> brandName
            Brand brand = this.brandMapper.selectByPrimaryKey(spu.getBrandId());
            spuBo.setBname(brand.getName());
            return spuBo;
        }).collect(Collectors.toList());

        // 4. 返回查询结果
        PageInfo<Spu> pageInfo = new PageInfo<>(spus);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getPages(), spuBos);
    }

PageResult实体类

import java.util.List;

public class PageResult<T> {

    // 总条数
    private Long total;
    // 总页数
    private Integer totalPage;
    // 当前页的数据集合
    private List<T> items;

    public PageResult() {
    }

    public PageResult(Long total, List<T> items) {
        this.total = total;
        this.items = items;
    }

    public PageResult(Long total, Integer totalPage, List<T> items) {
        this.total = total;
        this.totalPage = totalPage;
        this.items = items;
    }

    public Long getTotal() {
        return total;
    }

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

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getItems() {
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }
    
}

Controller

import org.springframework.http.ResponseEntity;

@GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "saleable", defaultValue = "true") Boolean saleable,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows
    ) {
        PageResult<SpuBo> result = this.goodsService.querySpuByPage(key, saleable, page, rows);
        return ResponseEntity.ok(result);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值