1.创建一个vo类,用来封装查询的对象
package com.dm.eduservice.entity.front; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.io.Serializable; @Data public class CourseQueryVo implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "课程名称") private String title; @ApiModelProperty(value = "讲师id") private String teacherId; @ApiModelProperty(value = "一级类别id") private String subjectParentId; @ApiModelProperty(value = "二级类别id") private String subjectId; @ApiModelProperty(value = "销量排序") private String buyCountSort; @ApiModelProperty(value = "最新时间排序") private String gmtCreateSort; @ApiModelProperty(value = "价格排序") private String priceSort; }
2.创建一个Controller
书写我们的查询方法
@RestController @RequestMapping("eduservice/coursefront") @CrossOrigin public class CourseFrontController { @Autowired EduCourseService courseService; @PostMapping(value = "{page}/{limit}") public R pageList( @PathVariable Long page, @PathVariable Long limit, @RequestBody(required = false) CourseQueryVo courseQuery){ Page<EduCourse> pageParam = new Page<EduCourse>(page, limit); Map<String, Object> map = courseService.pageListWeb(pageParam, courseQuery); return R.OK().data(map); } }
3.书写ServiceImpl里的方法
@Override public Map<String, Object> pageListWeb(Page<EduCourse> pageParam, CourseQueryVo courseQuery) { QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>(); if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) { queryWrapper.eq("subject_parent_id", courseQuery.getSubjectParentId()); } if (!StringUtils.isEmpty(courseQuery.getSubjectId())) { queryWrapper.eq("subject_id", courseQuery.getSubjectId()); } if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) { queryWrapper.orderByDesc("buy_count"); } if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) { queryWrapper.orderByDesc("gmt_create"); } if (!StringUtils.isEmpty(courseQuery.getPriceSort())) { queryWrapper.orderByDesc("price"); } baseMapper.selectPage(pageParam, queryWrapper); List<EduCourse> records = pageParam.getRecords(); long current = pageParam.getCurrent(); long pages = pageParam.getPages(); long size = pageParam.getSize(); long total = pageParam.getTotal(); boolean hasNext = pageParam.hasNext(); boolean hasPrevious = pageParam.hasPrevious(); Map<String, Object> map = new HashMap<String, Object>(); map.put("items", records); map.put("current", current); map.put("pages", pages); map.put("size", size); map.put("total", total); map.put("hasNext", hasNext); map.put("hasPrevious", hasPrevious); return map; }