MyBatis Plus实现带条件分页查询

技术架构:Spring Cloud+MyBatis Plus+Mysql

TeacherQuery条件字段封装

/**
 * 

* Description: *

* * @author songzixian * @version v1.0.0 * @create 2020-05-24 18:19 * @see com.greatdata.provider.domain */ @ApiModel(value = "Teacher查询对象",description = "讲师查询对象封装") @Data public class TeacherQuery { @ApiModelProperty(value = "教师名称,模糊查询") private String name; @ApiModelProperty(value = "头衔 1高级讲师 2首席讲师") private Integer level; @ApiModelProperty(value = "查询开始时间", example = "2019-01-01 10:10:10") private String gmtCreate; @ApiModelProperty(value = "查询结束时间", example = "2020-12-01 10:10:10") private String gmtModified; }

Controller

/**
     * 带条件的讲师分页查询方法
     * @return
     */
    @ApiOperation("带条件的讲师分页查询")
    @GetMapping("pageListTeacherCondition/{current}/{limit}")
    public ResponseResult> pageListTeacherCondition(@PathVariable long current,
                                                                     @PathVariable Long limit,
                                                                     TeacherQuery teacherQuery){
        Page eduTeacherPage = eduTeacherService.pageListTeacherCondition(current, limit, teacherQuery);
        return new ResponseResult(ResponseResult.CodeStatus.OK,"讲师条件方法查询成功",eduTeacherPage);
    }

servicei

/**
    * 带条件的讲师分页查询
    * @param current
    * @param limit
    * @param teacherQuery
    * @return
    */
   Page pageListTeacherCondition(long current, Long limit, TeacherQuery teacherQuery);

serviceimpl

/**
 * 带条件的分页查询
 * @param current
 * @param limit
 * @param teacherQuery
 * @return
 */
@Override
public Page pageListTeacherCondition(long current, Long limit, TeacherQuery teacherQuery){

    //创建Page对象
    Page eduTeacherPage = new Page<>(current,limit);

    //构建条件
    QueryWrapper wrapper = new QueryWrapper<>();

    //获取传入讲师的条件是否为空
    //讲师名
    String name = teacherQuery.getName();
    //讲师级别
    Integer level = teacherQuery.getLevel();
    //开始时间
    String gmtCreate = teacherQuery.getGmtCreate();
    //结束时间
    String gmtModified = teacherQuery.getGmtModified();

    //多条件组合查询
    //判断条件值是否为空,如果不为空拼接条件
    if (!StringUtils.isEmpty(name)){
        //构建条件 模糊查询
        wrapper.like("name",name);
    }

    if (!StringUtils.isEmpty(level)){
        //等于
        wrapper.eq("level",level);
    }

    if (!StringUtils.isEmpty(gmtCreate)){
        //大于等于
        wrapper.ge("gmt_create",gmtCreate);
    }

    if (!StringUtils.isEmpty(gmtModified)){
        //小于等于
        wrapper.le("gmt_modified",gmtModified);
    }

    //调用mybatis plus分页方法进行查询
    eduTeacherMapper.selectPage(eduTeacherPage,wrapper);

    //通过Page对象获取分页信息
    //long current = page.getCurrent();//当前页
    List records = eduTeacherPage.getRecords(); //每页的数据 list集合
    //long size = eduTeacherPage.getSize(); //每页显示的条数
    long total = eduTeacherPage.getTotal(); //总记录数
    long pages = eduTeacherPage.getPages(); //总页数

    boolean pagehasNext = eduTeacherPage.hasNext(); //下一页
    boolean pagehasPrevious = eduTeacherPage.hasPrevious(); //上一页
    
    return eduTeacherPage;

访问方式GET :http://localhost:9030/eduservice/teacher/pageListTeacherCondition/1/5?current=1&limit=5&gmtCreate=2019-01-01 10:10:10&gmtModified=2020-02-01 10:10:10&level=1&name=宋

MyBatis Plus实现带条件分页查询_第1张图片

你可能感兴趣的:(java)