SpringBoot使用Mybatis-PageHelper,和PageResult

public PageResult<SpuBo> querySpuBoByPage(String key, Boolean saleable, Integer page, Integer rows) {

        Example example = new Example(Spu.class);
        Example.Criteria criteria = example.createCriteria();
        // 搜索条件
        if (StringUtils.isNotBlank(key)){
            criteria.andLike("title","%"+key+"%");
        }
        if (!Objects.isNull(saleable)){
            criteria.andEqualTo("saleable",saleable);
        }

        //分页
        PageHelper.startPage(page,rows);   // 分页条件
        /**
         * 分页条件必须放在下面的查询语句前面
         */
        // 执行查询
        List<Spu> spus=this.spuMapper.selectByExample(example);

        PageInfo<Spu> pageInfo = new PageInfo<>(spus);

        //扩展的spuBos  list
        ArrayList<SpuBo> spuBos = new ArrayList<SpuBo>();
        spus.forEach(spu -> {
            SpuBo spuBo = new SpuBo();
            // copy共同属性的值到新的对象
            BeanUtils.copyProperties(spu,spuBo);

            // 查询分类名称
            List<String> names = this.categoryService.queryNamesByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));
            //设置spuBo的cname
            spuBo.setCname(StringUtils.join(names,"/"));
            //设置spuBo的bname
            Brand brand = this.brandMapper.selectByPrimaryKey(spu.getBrandId());
            String bname = brand.getName();
            spuBo.setBname(bname);

            //spuBo属性设置完成  添加到spuBos
            spuBos.add(spuBo);
            spuBo=null;
        });

        return new PageResult<SpuBo>(pageInfo.getTotal(),pageInfo.getPages(),spuBos);
    }

分页结果,可以调整分页
在这里插入图片描述
如果把PageHelper.startPage(page,rows); // 分页条件
放在List<Spu> spus=this.spuMapper.selectByExample(example);之后
结果为查询所有

关于MyBatis通用Mapper的使用可以看这篇文章

https://blog.csdn.net/illusory_germ/article/details/100145907
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值