-
public class DishController { @Autowired private DishService dishService; @GetMapping("/page") public Result<PageResult> page(DishPageQueryDTO dto) { PageResult page = dishService.page(dto); return Result.success(page); } }
-
@Service public class DishServiceImpl implements DishService { @Autowired private DishMapper dishMapper; @Override public PageResult page(DishPageQueryDTO dto) { LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.like(!StringUtils.isBlank(dto.getName()),Dish::getName, dto.getName()) .eq(!Objects.isNull(dto.getCategoryId()),Dish::getCategoryId, dto.getCategoryId()); // 添加了类别ID的查询条件 IPage<Dish> dishIPage = new Page<>(dto.getPage(), dto.getPageSize()); dishIPage = dishMapper.selectPage(dishIPage, lambdaQueryWrapper); // 使用 lambdaQueryWrapper 而不是 null List<DishVO> dishVOS = BeanUtil.copyToList(dishIPage.getRecords(), DishVO.class); return PageResult.builder().records(dishVOS).total(dishIPage.getTotal()).build(); } }
-
public interface DishMapper extends BaseMapper<Dish> { }
-
配置类
@Configuration public class MybatisPlusConfig { @Bean public PaginationInterceptor mybatisPlusInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false // paginationInterceptor.setOverflow(false); // 设置最大单页限制数量,默认 500 条,-1 不受限制 paginationInterceptor.setLimit(1000); // 开启 count 的 join 优化,只针对部分 left join paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true)); return paginationInterceptor; } }