使用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 orderdetailPage = new Page<>(page,limit);
    Map map = orderdetailService.selectByPages(orderdetailPage,orderdetail);
    return Result.ok(map);
}

//service层

Map selectByPages(Page orderdetailPage, Orderdetail orderdetail);

//serviceImpl层

@Service
public class OrderdetailServiceImpl extends ServiceImpl implements OrderdetailService {

    @Override
    public Map selectByPages(Page orderdetailPage, Orderdetail orderdetail) {
        QueryWrapper 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 selectPage = baseMapper.selectPage(orderdetailPage, wrapper);
        long pages = selectPage.getPages();
        long total = selectPage.getTotal();
        List records = selectPage.getRecords();
        Map map = new HashMap<>();
        map.put("pages",pages);
        map.put("total",total);
        map.put("records",records);
        return map;
    }
}

你可能感兴趣的:(java,servlet,jvm)