• day02---分页和条件查询接口开发(06)


    一、分页

    1、EduConfig 中配置分页插件

     /**
         * 分页插件
         */
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            return new PaginationInterceptor();
        }
    

    2、分页Controller方法

    EduTeacherController中添加分页方法

    @ApiOperation(value = "分页讲师列表")
        @GetMapping("{page}/{limit}")
        public R pageQuery(
                @ApiParam(name = "page", value = "当前页码", required = true)
                @PathVariable Long page,
                @ApiParam(name = "limit", value = "每页记录数", required = true)
                @PathVariable Long limit,
                @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
                        TeacherQuery teacherQuery){
            Page<EduTeacher> pageParam = new Page<>(page, limit);
            eduTeacherService.pageQuery(pageParam, teacherQuery);
            List<EduTeacher> records = pageParam.getRecords();
            long total = pageParam.getTotal();
            /*//返回数据
            Map<String,Object> map = new HashMap<>();
            map.put("total", total);
            map.put("rows", records);*/
           return  R.ok().data("total", total).data("rows", records);
        }
    

    3、Swagger中测试

    http://localhost:8001/swagger-ui.html
    

    二、条件查询

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

    1、创建查询对象

    创建com.atguigu.eduservice.query包,创建TeacherQuery.java查询对象

    package com.atguigu.eduservice.query;
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;
    import java.io.Serializable;
    @ApiModel(value = "Teacher查询对象", description = "讲师查询对象封装")
    @Data
    public class TeacherQuery implements Serializable {
        private static final long serialVersionUID = 1L;
        @ApiModelProperty(value = "教师名称,模糊查询")
        private String name;
        @ApiModelProperty(value = "头衔 1高级讲师 2首席讲师")
        private Integer level;
        @ApiModelProperty(value = "查询开始时间", example = "2019-01-01 10:10:10")
        private String begin;//注意,这里使用的是String类型,前端传过来的数据无需进行类型转换
        @ApiModelProperty(value = "查询结束时间", example = "2019-12-01 10:10:10")
        private String end;
    }
    

    2、service

    (1)编写接口

    package com.atguigu.eduservice.service;
    import com.atguigu.eduservice.entity.EduTeacher;
    import com.atguigu.eduservice.query.TeacherQuery;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.IService;
    public interface EduTeacherService extends IService<EduTeacher> {
        void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery);
    }
    

    (2)实现接口

    package com.atguigu.eduservice.service.impl;
    import com.alibaba.nacos.client.naming.utils.StringUtils;
    import com.atguigu.eduservice.entity.EduTeacher;
    import com.atguigu.eduservice.mapper.EduTeacherMapper;
    import com.atguigu.eduservice.query.TeacherQuery;
    import com.atguigu.eduservice.service.EduTeacherService;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.springframework.stereotype.Service;
    import java.util.Collection;
    @Service
    public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
        @Override
        public void pageQuery(Page<EduTeacher> pageParam, TeacherQuery teacherQuery) {
            QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByAsc("sort");
            if (teacherQuery == null){
                baseMapper.selectPage(pageParam, queryWrapper);
                return;
            }
            String name = teacherQuery.getName();
            Integer level = teacherQuery.getLevel();
            String begin = teacherQuery.getBegin();
            String end = teacherQuery.getEnd();
            if (!StringUtils.isEmpty(name)) {
                queryWrapper.like("name", name);
            }
            if (!StringUtils.isEmpty(String.valueOf(level)) ) {
                queryWrapper.eq("level", level);
            }
            if (!StringUtils.isEmpty(begin)) {
                queryWrapper.ge("gmt_create", begin);
            }
            if (!StringUtils.isEmpty(end)) {
                queryWrapper.le("gmt_create", end);
            }
            baseMapper.selectPage(pageParam, queryWrapper);
        }
    }
    

    3、controller

    在EduTeacherController中添加getTeacherPageVo方法实现带条件分页查询讲师列表:

    增加参数TeacherQuery teacherQuery,非必选

    @ApiOperation(value = "带条件分页查询讲师列表")
        @PostMapping("getTeacherPageVo/{current}/{limit}")
        public R getTeacherPageVo(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody TeacherQuery teacherQuery){
            //@RequestBody 必须使用PostMapping
            //@RequestBody 可以把json对象转化成vo
            //1创建page对象
            Page<EduTeacher> page = new Page<>(current,limit);
            //1.5从teacherQuery获取值拼接条件
            String name = teacherQuery.getName();
            Integer level = teacherQuery.getLevel();
            String begin = teacherQuery.getBegin();
            String end = teacherQuery.getEnd();
            QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(name)){
                queryWrapper.like("name",name);
            }
            if(!StringUtils.isEmpty(String.valueOf(level))){
                queryWrapper.eq("level",level);
            }
            if(!StringUtils.isEmpty(begin)){
                queryWrapper.ge("gmt_create",begin);
            }
            if(!StringUtils.isEmpty(end)){
                queryWrapper.le("gmt_create",end);
            }
            //2调用方法获得数据
            eduTeacherService.page(page,queryWrapper);
            //3从page里获取数据
            long total = page.getTotal();
            List<EduTeacher> records = page.getRecords();
            //4返回数据
    //        Map<String,Object> map = new HashMap<>();
    //        map.put("total",total);
    //        map.put("items",records);
    //        return R.ok().data(map);
            return R.ok().data("total",total).data("items",records);
        }
    

    4、Swagger中测试

    http://localhost:8001/swagger-ui.html
    
  • 相关阅读:
    maven错误
    angularjs的一点总结
    工具汇总
    重启outlook的bat脚本
    前端框架参考
    imply套件以及plyql的安装
    centos下nodejs,npm的安装和nodejs的升级
    kafka错误集锦
    动态规划DP笔记
    链接
  • 原文地址:https://www.cnblogs.com/lvwuwa/p/14129564.html
Copyright © 2020-2023  润新知