1、带条件查询带分页的课程列表
(1)创建条件查询的vo 在service_edu模块中com/atguigu/eduservice/entity/vo编写CourseQueryVo类。
java
@Data
public class CourseQueryVo {
@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)在com/atguigu/eduservice/api编写CourseApiController
java
@Api(description="前台课程展现")
@RestController
@RequestMapping("/eduservice/courseapi")
@CrossOrigin
public class CourseApiController {
@Autowired
private EduCourseService courseService;
@ApiOperation(value = "带条件分页查询课程列表")
@PostMapping("getFrontCourseList/{current}/{limit}")
public R getTeacherPageVo(@PathVariable Long current,
@PathVariable Long limit,
@RequestBody CourseQueryVo courseQueryVo){
Page<EduCourse> page = new Page<>(current,limit);
Map<String,Object> map = courseService.getCoursePageList(page,courseQueryVo);
return R.ok().data(map);
}
}
(3)在EduCourseService中添加带条件分页查询课程列表 接口
java
Map<String, Object> getCoursePageList(Page<EduCourse> page, CourseQueryVo courseQueryVo);
(4)在EduCourseServiceImpl中实现接口的方法
java
//带条件分页查询课程列表(前台)
@Override
public Map<String, Object> getCoursePageList(Page<EduCourse> pageParam, CourseQueryVo courseQueryVo) {
//1取出查询条件
String subjectParentId = courseQueryVo.getSubjectParentId();//一级分类
String subjectId = courseQueryVo.getSubjectId();//二级分类
String buyCountSort = courseQueryVo.getBuyCountSort();//关注度
String priceSort = courseQueryVo.getPriceSort();//价格
String gmtCreateSort = courseQueryVo.getGmtCreateSort();//时间
//2条件判断
QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
if(!StringUtils.isEmpty(subjectParentId)){
wrapper.eq("subject_parent_id",subjectParentId);
}
if(!StringUtils.isEmpty(subjectId)){
wrapper.eq("subject_id",subjectId);
}
if(!StringUtils.isEmpty(buyCountSort)){
wrapper.orderByDesc("buy_count");
}
if(!StringUtils.isEmpty(priceSort)){
wrapper.orderByDesc("price");
}
if(!StringUtils.isEmpty(gmtCreateSort)){
wrapper.orderByDesc("gmt_create");
}
//3查询数据
baseMapper.selectPage(pageParam,wrapper);
//4封装数据
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;
}