谷粒学院-4-多条件组合查询带分页
效果
流程:
再extity包下创建vo包,下面再创建TeacehrQueryWithCondition.java类
@Data
@ApiModel(value = "条件查询讲师封装对象",description = "带条件的讲师查询封装对象")
public class TeacherQueryWithCondition {
// private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "教程查询,模糊查询",example = "张")
private String name;
@ApiModelProperty(value = "头衔:1、高级讲师,2、首席讲师",example = "1")
private Integer level;
@ApiModelProperty(value = "开始时间",example = "2019-01-01 10:10:10")
private String begin;
@ApiModelProperty(value = "借宿时间",example = "2019-12-01 10:10:10")
private String end;
}
修改eduTeacehrService
//注意直接的service类是不需要@Service注解的
public interface EduTeacherService extends IService<EduTeacher> {
public void queryTeacherWithCondition(Page<EduTeacher> ResultPage, TeacherQueryWithCondition teacherQuery);
}
修改对应service的实现类
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
@Override
public void queryTeacherWithCondition(Page<EduTeacher> ResultPage, TeacherQueryWithCondition TeacherQueryWithCondition) {
//注意baseMapper对象,这个是内部已经封装好的注入的eduTeacherMapper对象
QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
if (TeacherQueryWithCondition == null){
//如果为空也需要做一个查询,这样之后组件中的service这个方法才能获得这个page对象
baseMapper.selectPage(ResultPage,wrapper);
return;
}
String name = TeacherQueryWithCondition.getName();
Integer level = TeacherQueryWithCondition.getLevel();
String begin = TeacherQueryWithCondition.getBegin();
String end = TeacherQueryWithCondition.getEnd();
if (!StringUtils.isEmpty(name)){
wrapper.like("name",name);
}
if(!StringUtils.isEmpty(level)){
wrapper.eq("level",level);
}
if (!StringUtils.isEmpty(begin)){
wrapper.ge("gmt_create",begin);
}
if (!StringUtils.isEmpty(end)){
wrapper.le("gmt_create",end);
}
baseMapper.selectPage(ResultPage,wrapper);
}
}
在controller中测试使用
@ApiOperation(value = "多条件分页查询")
@PostMapping("{page}/{limit}")
public Result queryTeacherWithCondition(
@ApiParam(name = "page",value = "页码",required = true)
@PathVariable long page,
@ApiParam(name = "limit",value = "每页大小",required = true)
@PathVariable long limit,
@ApiParam(name = "teacherQueryWithCondition",value = "带条件的讲师查询封装对象",required = false)
@RequestBody(required = false)TeacherQueryWithCondition teacherQueryWithCondition
){
Page<EduTeacher> resultPage = new Page<>(page, limit);
eduTeacherService.queryTeacherWithCondition(resultPage,teacherQueryWithCondition);
return Result.success()
.data("page",resultPage)
.data("hasNext",resultPage.hasNext()?1:0)
.data("hasPrevious",resultPage.hasPrevious()?1:0);
}