12. 分页带条件查询

文章目录

    • 1. 分页
        • 1.1 MyBatisPlusConfig中配置分页插件
        • 1.2 分页Controller方法
        • 1.3 Swagger中测试
    • 2. 条件查询
        • 2.1 创建查询对象
        • 2.2 controller
        • 2.3 Swagger中测试

1. 分页

1.1 MyBatisPlusConfig中配置分页插件

/**
 * @Date 2020/4/12 12:51
 * @Version 10.21
 * @Author DuanChaojie
 */
@Configuration
@EnableTransactionManagement
@MapperScan("com.atguigu.eduservice.mapper")
public class MyBatisPlusConfig {
     
    /**
     * 分页的插件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor(){
     
        return new PaginationInterceptor();
    }
}

1.2 分页Controller方法

EduTeacherController中添加分页方法

    @ApiOperation(value = "分页查询讲师列表")
    @GetMapping("/{page}/{limit}")
    public R findPage(
            @ApiParam(name = "page",value = "当前页码",required = true,defaultValue = "1")
            @PathVariable("page") Long page,
            @ApiParam(name = "limit",value = "每页的记录数",required = true)
            @PathVariable("limit") Long limit
    ){
     
    
        Page<EduTeacher> pageParam = new Page<EduTeacher>(page, limit);
        
        eduTeacherService.page(pageParam,null);

        List<EduTeacher> lists = pageParam.getRecords();

        long total = pageParam.getTotal();
        // 也可以创建Map来返回
        return R.ok().data("total",total).data("rows",lists);
    }

1.3 Swagger中测试

2. 条件查询

  • 根据讲师名称name,讲师头衔level、讲师入驻时间gmt_create(时间段)查询

2.1 创建查询对象

  • package com.atguigu.eduservice.entity.vo;
/**
 * @Date 2020/4/12 15:34
 * @Version 10.21
 * @Author DuanChaojie
 */
@Data
public class QueryTeacher implements Serializable {
     

    @ApiModelProperty(value = "教师名称,模糊查询")
    private String name;

    @ApiModelProperty(value = "头衔1-高级讲师,2-首席讲师")
    private Integer level;

    /**
     * 注意这里使用的是String类型,前端传来的数据无需进行类型转换
     */
    @ApiModelProperty(value = "查询起始时间")
    private String begin;

    @ApiModelProperty(value = "查询结束时间")
    private String end;

}

2.2 controller

  • package com.atguigu.eduservice.controller;
/**
 * 

* 讲师 前端控制器 *

* * @author DuanChaojie * @since 2020-04-12 */
@Api("讲师管理") @RestController @RequestMapping("/edu/edu-teacher") public class EduTeacherController { @Autowired private EduTeacherService eduTeacherService; @ApiOperation(value = "分页带条件查询讲师列表") @GetMapping("/{page}/{limit}") public R findPage( @ApiParam(name = "page",value = "当前页码",required = true,defaultValue = "1") @PathVariable("page") Long page, @ApiParam(name = "limit",value = "每页的记录数",required = true) @PathVariable("limit") Long limit, @ApiParam(name = "queryTeacher",value = "查询对象",required = false) QueryTeacher queryTeacher ){ Page<EduTeacher> pageParam = new Page<EduTeacher>(page, limit); // 构建查询条件 QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper(); // 拿到queryTeacher中的数据 String name = queryTeacher.getName(); Integer level = queryTeacher.getLevel(); String begin = queryTeacher.getBegin(); String end = queryTeacher.getEnd(); // 判断条件是否为空 if(!StringUtils.isEmpty(name)){ queryWrapper.like("name",name); } if(!StringUtils.isEmpty(level)){ queryWrapper.eq("level",level); } if(!StringUtils.isEmpty(begin)){ queryWrapper.ge("gmt_create",begin); } if(!StringUtils.isEmpty(end)){ queryWrapper.le("gmt_create",end); } eduTeacherService.page(pageParam,queryWrapper); List<EduTeacher> lists = pageParam.getRecords(); long total = pageParam.getTotal(); // 也可以创建Map来返回 return R.ok().data("total",total).data("rows",lists); } }

2.3 Swagger中测试

12. 分页带条件查询_第1张图片

你可能感兴趣的:(谷粒学院,谷粒学院)