使用mybatisPlus实现分页查询

//第一步先进入分页插件
package com.example.spingbootmybatis.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("com.atguigu.ggkt.vod.mapper")
public class VodConfig {

        /**
         * 分页插件
         */
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            return new PaginationInterceptor();
        }
    }
//Controller层
@GetMapping("/findPages/{page}/{limit}")
@ApiOperation("订单列表分页查询")
public Result findPages(@PathVariable Long page,
                        @PathVariable Long limit,
                        Orderdetail orderdetail){
    Page<Orderdetail> orderdetailPage = new Page<>(page,limit);
    Map<String,Object> map = orderdetailService.selectByPages(orderdetailPage,orderdetail);
    return Result.ok(map);
}

//service层

Map<String, Object> selectByPages(Page<Orderdetail> orderdetailPage, Orderdetail orderdetail);

//serviceImpl层

@Service
public class OrderdetailServiceImpl extends ServiceImpl<OrderdetailMapper, Orderdetail> implements OrderdetailService {

    @Override
    public Map<String, Object> selectByPages(Page<Orderdetail> orderdetailPage, Orderdetail orderdetail) {
        QueryWrapper<Orderdetail> wrapper = new QueryWrapper<>();
//获取要进行分页查询的条件的字段
        Integer id = orderdetail.getId();
        Integer orderbasetableId = orderdetail.getOrderbasetableId();
        Integer shoppingnum = orderdetail.getShoppingnum();
        Integer goodstableId = orderdetail.getGoodstableId();
        if (!StringUtils.isEmpty(id)){
//"*" *表示的是数据库里面的字段名,判断是否为空,不为空则直接用wrapper封装起来
            wrapper.eq("id",id);
        }
        if (!StringUtils.isEmpty(orderbasetableId)){
            wrapper.eq("orderbasetable_id",orderbasetableId);
        }
        if (!StringUtils.isEmpty(shoppingnum)){
            wrapper.eq("shoppingnum",shoppingnum);
        }
        if (!StringUtils.isEmpty(goodstableId)){
            wrapper.eq("goodstableId",goodstableId);
        }
        Page<Orderdetail> selectPage = baseMapper.selectPage(orderdetailPage, wrapper);
        long pages = selectPage.getPages();
        long total = selectPage.getTotal();
        List<Orderdetail> records = selectPage.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("pages",pages);
        map.put("total",total);
        map.put("records",records);
        return map;
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值