Mybatis-plus分页查询和条件查询通用写法- 动态接口参数

一 查询条件VO

/**
* @className: TeacherQueryVo
* @description: 讲师查询条件封装
* @date: 2020/11/18
* @author: cakin
*/
@ApiModel("Teacher查询对象")
@Data
public class TeacherQueryVo implements Serializable {
   private static final long serialVersionUID = 1L;

   @ApiModelProperty(value = "讲师姓名")
   private String name;

   @ApiModelProperty(value = "讲师级别")
   private Integer level;

   @ApiModelProperty(value = "开始时间")
   private String joinDateBegin;

   @ApiModelProperty(value = "结束时间")
   private String joinDateEnd;
}

二 讲师控制器中的分页和条件查询

/**
* 功能描述:分页和条件查询
*
* @param page           当前页码
* @param limit          每页记录数
* @param teacherQueryVo 查询条件封装
* @return R 返回给前端的数据
* @author cakin
* @date 2020/11/18
*/
@ApiOperation("讲师分页列表")
@GetMapping("list/{page}/{limit}")
public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                  @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                  @ApiParam("讲师列表查询对象") TeacherQueryVo teacherQueryVo) {
    // 分页参数
    Page<Teacher> pageParam = new Page<>(page, limit);
    IPage<Teacher> pageModel = teacherService.selectPage(pageParam, teacherQueryVo);
    List<Teacher> records = pageModel.getRecords();
    long total = pageModel.getTotal();
    return R.ok().data("total", total).data("rows", records);
}

三 讲师服务层实现

/**
* @className: TeacherServiceImpl
* @description: 讲师 服务实现类
* @date: 2020/11/18
* @author: cakin
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    /**
     * 功能描述:显示分页查询列表
     *
     * @author cakin
     * @date 2020/11/18
     * @param pageParam 分页查询参数
     * @param teacherQueryVo 条件查询参数
     * @return IPage<Teacher> 分页信息
     */
    @Override
    public IPage<Teacher> selectPage(Page<Teacher> pageParam, TeacherQueryVo teacherQueryVo) {
        // 1 排序:按照sort字段排序
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        // 2 有分页查询 无条件查询
        if (teacherQueryVo == null) {
            return baseMapper.selectPage(pageParam, queryWrapper);
        }
        // 3 构造条件查询
        String name = teacherQueryVo.getName();
        Integer level = teacherQueryVo.getLevel();
        String joinDateBegin = teacherQueryVo.getJoinDateBegin();
        String joinDateEnd = teacherQueryVo.getJoinDateEnd();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.likeRight("name", name);
        }
        if (level != null) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(joinDateBegin)) {
            queryWrapper.ge("join_date", joinDateBegin);
        }
        if (!StringUtils.isEmpty(joinDateEnd)) {
            queryWrapper.le("join_date", joinDateEnd);
        }
        // 2 有分页查询 有条件查询
        return baseMapper.selectPage(pageParam, queryWrapper);
    }

转载:https://blog.csdn.net/chengqiuming/article/details/109786365

posted @ 2022-10-13 17:28  Lafite-1820  阅读(751)  评论(0编辑  收藏  举报