条件查询分页

条件查询

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

创建查询对象

创建com.leon.edu.query包,创建TeacherQuery.java查询对象

package com.leon.edu.query;

@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;
}

service

接口

package com.leon.edu.service;
public interface TeacherService extends IService {

	void pageQuery(Page pageParam, TeacherQuery teacherQuery);
}

实现

package com.leon.edu.service.impl;

@Service
public class TeacherServiceImpl extends ServiceImpl implements TeacherService {

	@Override
	public void pageQuery(Page pageParam, TeacherQuery teacherQuery) {

		QueryWrapper 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(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);
	}
}

controller

TeacherAdminController中修改 pageList方法:

增加参数TeacherQuery teacherQuery,非必选

@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 pageParam = new Page<>(page, limit);

    teacherService.pageQuery(pageParam, teacherQuery);
    List records = pageParam.getRecords();
    long total = pageParam.getTotal();

    return  R.ok().data("total", total).data("rows", records);
}

 

你可能感兴趣的:(条件查询分页)