1、MyBatisPlusConfig中配置分页插件
依赖参考:https://www.cnblogs.com/konglxblog/p/16793936.html
package com.stu.service.base.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 分页功能 */ @Configuration @MapperScan("com.stu.service.*.mapper") public class MyBatisPlusConfig { /** * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除) */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2)); return interceptor; } }
2、方式一(不涉及mapper.java文件和sql.xml文件)
controller
package com.stu.service.edu.controller.admin; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.stu.service.base.result.R; import com.stu.service.edu.entity.Teacher; import com.stu.service.edu.entity.vo.ChapterVo; import com.stu.service.edu.entity.vo.TeacherQueryVo; import com.stu.service.edu.service.TeacherService; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.List; import java.util.Map; /** * <p> * 讲师 前端控制器 * </p> * * @author stu * @since 2022-04-11 */ @RestController @RequestMapping("/admin/edu/teacher") //@CrossOrigin public class TeacherController { @Autowired private TeacherService teacherService; /*********************************** * 用途说明:批量删除 * 返回值说明: com.stu.service.base.result.R ***********************************/ @DeleteMapping("batchRemoveTeacher") public R batchRemoveTeacher(@RequestBody List<String> idList) { boolean result = teacherService.removeByIds(idList); if (result) { return R.ok().message("删除成功"); } return R.error().message("删除失败"); } /*********************************** * 用途说明:删除讲师 * 返回值说明: com.stu.service.base.result.R ***********************************/ @DeleteMapping("deleteTeacher/{id}") public R deleteTeacher(@PathVariable String id) { /* if (true) { throw new CustomException(ResultCodeEnum.DIVIDE_ZERO); }*/ boolean result = teacherService.removeById(id); if (result) { return R.ok().message("删除成功"); } return R.error().message("删除失败"); } /*********************************** * 用途说明:修改讲师 * 返回值说明: com.stu.service.base.result.R ***********************************/ @PostMapping("updateTeacher") public R updateTeacher(@RequestBody Teacher teacher) { boolean result = teacherService.updateById(teacher); if (result) { return R.ok().message("更新成功"); } return R.error().message("更新失败"); } /*********************************** * 用途说明:添加讲师 * 返回值说明: com.stu.service.base.result.R ***********************************/ @ApiOperation("添加讲师") @PostMapping("addTeacher") public R addTeacher(@ApiParam("讲师对象") @RequestBody Teacher teacher) { boolean result = teacherService.save(teacher); if (result) { return R.ok().message("保存成功"); } return R.error().message("保存失败"); } /*********************************** * 用途说明:查询讲师表所有数据 * 返回值说明: com.stu.service.base.result.R ***********************************/ @GetMapping("findAll") public R findAllTeacher() { List<Teacher> list = teacherService.list(); return R.ok().data("list", list); } /*********************************** * 用途说明:查询 * 返回值说明: com.stu.service.edu.entity.Teacher ***********************************/ @GetMapping("get/{id}") public R getTeacher(@PathVariable String id) { return R.ok().data("dataInfo", teacherService.getById(id)); } /*********************************** * 用途说明:查询讲师表所有数据 * 返回值说明: com.stu.service.base.result.R ***********************************/ @GetMapping("pageList/{page}/{limit}") public R pageList(@PathVariable long page, @PathVariable long limit, TeacherQueryVo teacherQueryVo) { Page<Teacher> pageParam = new Page<>(page, limit); teacherService.listPage(pageParam, teacherQueryVo); Map<String, Object> map = new HashMap<String, Object>(); long total = pageParam.getTotal(); List<Teacher> list = pageParam.getRecords(); map.put("total", total); map.put("list", list); return R.ok().data(map); } }
service接口
package com.stu.service.edu.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.stu.service.edu.entity.Teacher; import com.baomidou.mybatisplus.extension.service.IService; import com.stu.service.edu.entity.vo.ChapterVo; import com.stu.service.edu.entity.vo.TeacherQueryVo; import java.util.List; import java.util.Map; /** * <p> * 讲师 服务类 * </p> * * @author stu * @sinc e 2022-04-11 */ public interface TeacherService extends IService<Teacher> { /*********************************** * 用途说明:分页 * @param pageParam * @param teacherQueryVo * 返回值说明: * @return com.baomidou.mybatisplus.core.metadata.IPage<com.stu.service.edu.entity.Teacher> ***********************************/ IPage<Teacher> listPage(Page<Teacher> pageParam, TeacherQueryVo teacherQueryVo); /*********************************** * 用途说明:取得推荐讲师 * 返回值说明: * @return java.util.List<com.stu.service.edu.entity.Teacher> ***********************************/ List<Teacher> listHotTeacher(); /*********************************** * 用途说明:前台讲师分页 * @param pageParam * 返回值说明: * @return java.util.Map<java.lang.String, java.lang.Object> ***********************************/ Map<String,Object> listPageTeachers(Page<Teacher> pageParam); }
service实现类
package com.stu.service.edu.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.stu.service.edu.entity.Teacher; import com.stu.service.edu.entity.vo.TeacherQueryVo; import com.stu.service.edu.mapper.TeacherMapper; import com.stu.service.edu.service.TeacherService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; import java.util.Map; /** * <p> * 讲师 服务实现类 * </p> * * @author stu * @since 2022-04-11 */ @Service public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService { @Autowired private TeacherService teacherService; /*********************************** * 用途说明:分页 * @param pageParam * @param teacherQueryVo * 返回值说明: * @return com.baomidou.mybatisplus.core.metadata.IPage<com.stu.service.edu.entity.Teacher> ***********************************/ @Override public IPage<Teacher> listPage(Page<Teacher> pageParam, TeacherQueryVo teacherQueryVo) { QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>(); queryWrapper.orderByAsc("sort", "id"); if (teacherQueryVo == null) { return baseMapper.selectPage(pageParam, queryWrapper); } String name = teacherQueryVo.getName(); Integer level = teacherQueryVo.getLevel(); String begin = teacherQueryVo.getBegin(); String end = teacherQueryVo.getEnd(); if (!StringUtils.isEmpty(name)) { queryWrapper.like("name", name); } if (null != level) { queryWrapper.eq("level", level); } if (!StringUtils.isEmpty(begin)) { queryWrapper.ge("gmt_create", begin); } if (!StringUtils.isEmpty(end)) { queryWrapper.le("gmt_create", end); } return baseMapper.selectPage(pageParam, queryWrapper); } /*********************************** * 用途说明:取得推荐讲师 * 返回值说明: * @return java.util.List<com.stu.service.edu.entity.Teacher> ***********************************/ @Cacheable(value = "index", key = "'listHotTeacher'") @Override public List<Teacher> listHotTeacher() { QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>(); queryWrapper.orderByDesc("sort"); queryWrapper = queryWrapper.last("limit 4"); return baseMapper.selectList(queryWrapper); } @Override public Map<String, Object> listPageTeachers(Page<Teacher> pageParam) { QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>(); queryWrapper.orderByDesc("gmt_modified").orderByDesc("sort"); baseMapper.selectPage(pageParam,queryWrapper); Map<String, Object> mapParam = new HashMap<>(); mapParam.put("items", pageParam.getRecords());//当前页记录 mapParam.put("current", pageParam.getCurrent());//当前页 mapParam.put("pages", pageParam.getPages());//共有多少页 mapParam.put("size", pageParam.getSize());//每页的记录数 mapParam.put("total", pageParam.getTotal());//总记录数 mapParam.put("hasNext", pageParam.hasNext());//是否有上一页 mapParam.put("hasPrevious", pageParam.hasPrevious());//是否有下一页 return mapParam; } }
3、方式二,复杂查询需要用到sql,涉及mapper文件和xml文件
controller
package com.stu.service.edu.controller.admin; import com.baomidou.mybatisplus.core.metadata.IPage; import com.stu.service.base.result.R; import com.stu.service.base.result.ResultCodeEnum; import com.stu.service.edu.entity.Course; import com.stu.service.edu.entity.CourseDescription; import com.stu.service.edu.entity.form.CourseInfoForm; import com.stu.service.edu.entity.vo.CoursePublishVo; import com.stu.service.edu.entity.vo.CourseQueryVo; import com.stu.service.edu.entity.vo.CourseVo; import com.stu.service.edu.service.CourseService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; /** * <p> * 课程 前端控制器 * </p> * * @author stu * @since 2022-05-05 */ @RestController @RequestMapping("/admin/edu/course") //@CrossOrigin public class CourseController { @Autowired private CourseService courseService; /*********************************** * 用途说明:更新课程基本信息和课程简介 * 返回值说明: com.stu.service.base.result.R ***********************************/ @PostMapping("updateCourseForm") private R updateCourseForm(@RequestBody CourseInfoForm courseInfoForm) { //非空判断 if (null == courseInfoForm || StringUtils.isEmpty(courseInfoForm.getId())) { return R.error().message(ResultCodeEnum.PARAM_ERROR.getMessage()); } //更新课程进步信息和课程简介 boolean updateCourseForm = courseService.updateCourseForm(courseInfoForm); if (updateCourseForm) { return R.ok(); } else { return R.error().message(ResultCodeEnum.UPDATE_ERROR.getMessage()); } } /*********************************** * 用途说明:保存课程基本信息和课程简介 * 返回值说明: com.stu.service.base.result.R ***********************************/ @PostMapping("saveCourseInfo") private R saveCourseInfo(@RequestBody CourseInfoForm courseInfoForm) { return R.ok().data("data", courseService.saveCourseInfo(courseInfoForm)); } /*********************************** * 用途说明:取得课程的基本信息和课程简介 * 返回值说明: com.stu.service.base.result.R ***********************************/ @GetMapping("getCourseForm/{courseId}") private R getCourseForm(@PathVariable(required = true) String courseId) { if (StringUtils.isEmpty(courseId)) { return R.error().message(ResultCodeEnum.PARAM_ERROR.getMessage()); } CourseInfoForm courseInfoForm = courseService.getCourseForm(courseId); if (null == courseInfoForm) { return R.error().message(ResultCodeEnum.DATA_NULL.getMessage()); } else { return R.ok().data("data", courseService.getCourseForm(courseId)); } } /*********************************** * 用途说明:去看发布课程的详情 * @param courseId * 返回值说明: * @return com.stu.service.base.result.R ***********************************/ @GetMapping("getPublishCourseByid/{courseId}") public R getPublishCourseByid(@PathVariable String courseId) { CoursePublishVo coursePublish = courseService.getCoursePublishById(courseId); if(null != coursePublish){ return R.ok().data("data",coursePublish); } return R.error().message(ResultCodeEnum.DATA_NULL.getMessage()); } /*********************************** * 用途说明:发布课程 * @param courseId * 返回值说明: * @return com.stu.service.base.result.R ***********************************/ @GetMapping("publishCourse/{courseId}") public R publishCourse(@PathVariable String courseId) { boolean result = courseService.publishCourse(courseId); if (result) { return R.ok(); } return R.error().message(ResultCodeEnum.UPDATE_ERROR.getMessage()); } /*********************************** * 用途说明:分页 * @param page * @param limit * @param courseQueryVo * 返回值说明: * @return com.stu.service.base.result.R ***********************************/ @GetMapping("pageCourses/{page}/{limit}") public R pageCourses(@PathVariable Long page, @PathVariable Long limit, CourseQueryVo courseQueryVo) { IPage<CourseVo> coursePublishVoIPage = courseService.pageCourses(page, limit, courseQueryVo); return R.ok().data("data",coursePublishVoIPage); } /*********************************** * 用途说明:根據id刪除課程(包括课时,小节,详情等) * @param id * 返回值说明: * @return com.stu.service.base.result.R ***********************************/ @DeleteMapping("deleteById/{id}") public R deleteById(@PathVariable String id) { boolean result = courseService.deleteById(id); if (result) { return R.ok(); } return R.error().message(ResultCodeEnum.UPDATE_ERROR.getMessage()); } }
service接口
package com.stu.service.edu.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.stu.service.base.dto.CourseDto; import com.stu.service.edu.entity.Course; import com.baomidou.mybatisplus.extension.service.IService; import com.stu.service.edu.entity.form.CourseInfoForm; import com.stu.service.edu.entity.vo.*; import java.util.List; import java.util.Map; /** * <p> * 课程 服务类 * </p> * * @author stu * @since 2022-05-05 */ public interface CourseService extends IService<Course> { /*********************************** * 用途说明:保存课程基本信息和课程简介 * 返回值说明: com.stu.service.edu.entity.Course ***********************************/ Course saveCourseInfo(CourseInfoForm courseInfoForm); /*********************************** * 用途说明:取得课程的基本信息和课程简介 * 返回值说明: com.stu.service.edu.entity.form.CourseInfoForm ***********************************/ CourseInfoForm getCourseForm(String id); /*********************************** * 用途说明:更新课程基本信息和课程简介 * 返回值说明: boolean ***********************************/ boolean updateCourseForm(CourseInfoForm courseInfoForm); /*********************************** * 用途说明:查询课程发布信息 * @param courseId * 返回值说明: * @return com.stu.service.edu.entity.vo.CoursePublishVo ***********************************/ CoursePublishVo getCoursePublishById(String courseId); /*********************************** * 用途说明:发布课程 * @param id * 返回值说明: * @return boolean ***********************************/ boolean publishCourse(String id); /*********************************** * 用途说明:分页 * @param page * @param limit * @param courseQueryVo * 返回值说明: * @return com.baomidou.mybatisplus.core.metadata.IPage<com.stu.service.edu.entity.vo.CoursePublishVo> ***********************************/ IPage<CourseVo> pageCourses(Long page, Long limit, CourseQueryVo courseQueryVo); /*********************************** * 用途说明:根据课程id删除课程(章节,详情,视频都需要删除) * @param id * 返回值说明: * @return boolean ***********************************/ boolean deleteById(String id); /*********************************** * 用途说明:取得热门课程 * 返回值说明: * @return java.util.List<com.stu.service.edu.entity.Course> ***********************************/ List<Course> listHotCourses(); /*********************************** * 用途说明:根据讲师id查询所有课程 * @param id * 返回值说明: * @return java.util.List<com.stu.service.edu.entity.Course> ***********************************/ List<Course> listCourseByTeacherId(String id); /*********************************** * 用途说明:前端课程分页nuxt * @param pageParam * @param courseQueryVo * 返回值说明: * @return java.util.Map<java.lang.String, java.lang.Object> ***********************************/ Map<String,Object> listPageCourses(Page<Course> pageParam, CourseQueryVo courseQueryVo); /*********************************** * 用途说明:根据课程id查询详情nuxt * @param id * 返回值说明: * @return com.stu.service.edu.entity.vo.ApiCourseVo ***********************************/ ApiCourseVo getApiCourseById(String id); /*********************************** * 用途说明:更新游览数 * @param id * 返回值说明: * @return boolean ***********************************/ boolean updateViewCount(String id); /*********************************** * 用途说明:根据课程id查询订单相关的课程信息 * @param id * 返回值说明: * @return com.stu.service.edu.entity.vo.CourseApiVo ***********************************/ CourseDto getCourseById(String id); /*********************************** * 用途说明:更新課程銷售量 * @param id * 返回值说明: * @return boolean ***********************************/ boolean updateBuyCountById(String id); }
service实现类
package com.stu.service.edu.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.stu.service.base.dto.CourseDto; import com.stu.service.base.exception.CustomException; import com.stu.service.base.result.ResultCodeEnum; import com.stu.service.edu.entity.Chapter; import com.stu.service.edu.entity.Course; import com.stu.service.edu.entity.CourseDescription; import com.stu.service.edu.entity.constant.CourseConstant; import com.stu.service.edu.entity.form.CourseInfoForm; import com.stu.service.edu.entity.vo.ApiCourseVo; import com.stu.service.edu.entity.vo.CoursePublishVo; import com.stu.service.edu.entity.vo.CourseQueryVo; import com.stu.service.edu.entity.vo.CourseVo; import com.stu.service.edu.mapper.CourseMapper; import com.stu.service.edu.service.ChapterService; import com.stu.service.edu.service.CourseDescriptionService; import com.stu.service.edu.service.CourseService; import com.stu.service.edu.service.VideoService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.HashMap; import java.util.List; import java.util.Map; /** * <p> * 课程 服务实现类 * </p> * * @author stu * @since 2022-05-05 */ @Service public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService { @Autowired private CourseDescriptionService courseDescriptionService; @Autowired private VideoService videoService; @Autowired private ChapterService chapterService; /*********************************** * 用途说明:保存课程基本信息和课程简介 * 返回值说明: com.stu.service.edu.entity.Course ***********************************/ @Transactional(rollbackFor = Exception.class) @Override public Course saveCourseInfo(CourseInfoForm courseInfoForm) { //保存课程基本信息 Course course = new Course(); BeanUtils.copyProperties(courseInfoForm, course); course.setStatus(CourseConstant.STATUS_DRAFT);//未发布 baseMapper.insert(course); //保存课程简介 CourseDescription courseDescription = new CourseDescription(); courseDescription.setDescription(courseInfoForm.getDescription()); //设置课程和描述是一对一的关系,即课程id和描述id相等。 //EduCourseDescription实体类主键生成策略改为input。 courseDescription.setId(course.getId()); courseDescriptionService.save(courseDescription); return course; } /*********************************** * 用途说明:取得课程的基本信息和课程简介 * 返回值说明: com.stu.service.edu.entity.form.CourseInfoForm ***********************************/ @Override public CourseInfoForm getCourseForm(String id) { return baseMapper.getCourseForm(id); } /*********************************** * 用途说明:更新课程基本信息和课程简介 * 返回值说明: boolean ***********************************/ @Transactional(rollbackFor = Exception.class) @Override public boolean updateCourseForm(CourseInfoForm courseInfoForm) { //课程基本信息 Course course = new Course(); BeanUtils.copyProperties(courseInfoForm, course); int countUpdate = baseMapper.updateById(course); //课程简介 CourseDescription courseDescription = new CourseDescription(); courseDescription.setId(course.getId()); courseDescription.setDescription(courseInfoForm.getDescription()); boolean countUpdateDescriptiong = courseDescriptionService.updateById(courseDescription); return (countUpdate > 0 && countUpdateDescriptiong); } /*********************************** * 用途说明: * @param courseId * 返回值说明:查询课程发布信息 * @return com.stu.service.edu.entity.vo.CoursePublishVo ***********************************/ @Override public CoursePublishVo getCoursePublishById(String courseId) { return baseMapper.getCoursePublishById(courseId); } @Override public boolean publishCourse(String id) { Course course = new Course(); course.setId(id); course.setStatus(CourseConstant.STATUS_NORMAL); return baseMapper.updateById(course) > 0; } /*********************************** * 用途说明:课程分页 * @param page * @param limit * @param courseQueryVo * 返回值说明: * @return com.baomidou.mybatisplus.core.metadata.IPage<com.stu.service.edu.entity.vo.CoursePublishVo> ***********************************/ @Override public IPage<CourseVo> pageCourses(Long page, Long limit, CourseQueryVo courseQueryVo) { QueryWrapper<CourseVo> queryWrapper = new QueryWrapper<>(); queryWrapper.orderByDesc("t1.gmt_modified").orderByDesc("t1.id"); String title = courseQueryVo.getTitle(); if (!StringUtils.isEmpty(title)) { queryWrapper.like("t1.title", title); } Page<CourseVo> pageParam = new Page<>(page, limit); List<CourseVo> coursePublishVos = baseMapper.pageCourses(pageParam, queryWrapper); pageParam.setRecords(coursePublishVos); return pageParam; } /*********************************** * 用途说明:根據id刪除課程(包括课时,小节,详情等) * @param id * 返回值说明: * @return boolean ***********************************/ @Transactional(rollbackFor = Exception.class) @Override public boolean deleteById(String id) { boolean result = false; //删除video课时 boolean videoResult = videoService.removeVideoByCourseId(id); //删除description课程详情 QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<Chapter>(); chapterQueryWrapper.eq("course_id", id); boolean chapterResult = chapterService.remove(chapterQueryWrapper); boolean desResult = courseDescriptionService.removeById(id); //删除course课程 int courseResult = baseMapper.deleteById(id); if (videoResult && chapterResult && courseResult > 0) { return true; } else { result = false; throw new CustomException(ResultCodeEnum.UPDATE_ERROR); } } /*********************************** * 用途说明:根据游览数量取得热门课程 * 返回值说明: * @return java.util.List<com.stu.service.edu.entity.Course> ***********************************/ @Cacheable(value = "index", key = "'listHotCourses'") @Override public List<Course> listHotCourses() { QueryWrapper<Course> queryWrapper = new QueryWrapper<>(); queryWrapper.orderByDesc("view_count"); queryWrapper.last("limit 8"); return baseMapper.selectList(queryWrapper); } /*********************************** * 用途说明:根据讲师id查询所有课程 * @param id * 返回值说明: * @return java.util.List<com.stu.service.edu.entity.Course> ***********************************/ @Override public List<Course> listCourseByTeacherId(String id) { QueryWrapper<Course> queryWrapper = new QueryWrapper<>(); queryWrapper.orderByDesc("gmt_modified"); queryWrapper.eq("teacher_id", id); return baseMapper.selectList(queryWrapper); } /*********************************** * 用途说明:前端课程分页nuxt * @param pageParam * @param courseQueryVo * 返回值说明: * @return java.util.Map<java.lang.String, java.lang.Object> ***********************************/ @Override public Map<String, Object> listPageCourses(Page<Course> pageParam, CourseQueryVo courseQueryVo) { QueryWrapper<Course> queryWrapper = new QueryWrapper<>(); if (null != courseQueryVo) { String subjectParentId = courseQueryVo.getSubjectParentId(); String subjectId = courseQueryVo.getSubjectId(); //"销量排序" String buyCountSort = courseQueryVo.getBuyCountSort(); //"最新时间排序" String gmtCreateSort = courseQueryVo.getGmtCreateSort(); //"价格排序" String priceSort = courseQueryVo.getPriceSort(); if (StringUtils.isNotEmpty(subjectParentId)) { queryWrapper.eq("subject_parent_id", subjectParentId); } if (StringUtils.isNotEmpty(subjectId)) { queryWrapper.eq("subject_id", subjectId); } if (StringUtils.isNotEmpty(buyCountSort)) { queryWrapper.orderByDesc("buy_count"); } if (StringUtils.isNotEmpty(gmtCreateSort)) { queryWrapper.orderByDesc("gmt_create"); } if (StringUtils.isNotEmpty(priceSort)) { queryWrapper.orderByDesc("price"); } } baseMapper.selectPage(pageParam, queryWrapper); Map<String, Object> mapParam = new HashMap<>(); mapParam.put("items", pageParam.getRecords());//当前页记录 mapParam.put("current", pageParam.getCurrent());//当前页 mapParam.put("pages", pageParam.getPages());//共有多少页 mapParam.put("size", pageParam.getSize());//每页的记录数 mapParam.put("total", pageParam.getTotal());//总记录数 mapParam.put("hasNext", pageParam.hasNext());//是否有上一页 mapParam.put("hasPrevious", pageParam.hasPrevious());//是否有下一页 return mapParam; } /*********************************** * 用途说明:根据课程id查询详情nuxt * @param id * 返回值说明: * @return com.stu.service.edu.entity.vo.ApiCourseVo ***********************************/ @Override public ApiCourseVo getApiCourseById(String id) { this.updateViewCount(id); return baseMapper.getApiCourseById(id); } /*********************************** * 用途说明:更新游览数 * @param id * 返回值说明: * @return boolean ***********************************/ @Override public boolean updateViewCount(String id) { Course course = baseMapper.selectById(id); course.setViewCount(course.getViewCount() + 1); return baseMapper.updateById(course) > 0; } /*********************************** * 用途说明:根据课程id查询订单相关的课程信息 * @param id * 返回值说明: * @return com.stu.service.edu.entity.vo.CourseApiVo ***********************************/ @Override public CourseDto getCourseById(String id) { return baseMapper.getCourseById(id); } /*********************************** * 用途说明:更新課程銷售量 * @param id * 返回值说明: * @return boolean ***********************************/ @Override public boolean updateBuyCountById(String id) { Course course = baseMapper.selectById(id); course.setBuyCount(course.getBuyCount() + 1); return baseMapper.updateById(course) > 0; } }
mapper文件
package com.stu.service.edu.mapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.stu.service.edu.entity.Course; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.stu.service.edu.entity.form.CourseInfoForm; import com.stu.service.edu.entity.vo.ApiCourseVo; import com.stu.service.base.dto.CourseDto; import com.stu.service.edu.entity.vo.CoursePublishVo; import com.stu.service.edu.entity.vo.CourseVo; import org.apache.ibatis.annotations.Param; import java.util.List; /** * <p> * 课程 Mapper 接口 * </p> * * @author stu * @since 2022-05-05 */ public interface CourseMapper extends BaseMapper<Course> { /*********************************** * 用途说明:取得课程的基本信息和课程简介 * @param id * 返回值说明: * @return com.stu.service.edu.entity.form.CourseInfoForm ***********************************/ CourseInfoForm getCourseForm(String id); /*********************************** * 用途说明:查询发布课程信息 * @param courseId * 返回值说明: * @return com.stu.service.edu.entity.vo.CoursePublishVo ***********************************/ CoursePublishVo getCoursePublishById(String courseId); /*********************************** * 用途说明:课程列表 * @param pageParam * @param queryWrapper * 返回值说明: * @return java.util.List<com.stu.service.edu.entity.vo.CoursePublishVo> ***********************************/ List<CourseVo> pageCourses(Page<CourseVo> pageParam, @Param(Constants.WRAPPER)QueryWrapper<CourseVo> queryWrapper); /*********************************** * 用途说明:根据课程id查询详情nuxt * @param id * 返回值说明: * @return com.stu.service.edu.entity.vo.ApiCourseVo ***********************************/ ApiCourseVo getApiCourseById(String id); /*********************************** * 用途说明:根据课程id查询订单相关的课程信息 * @param id * 返回值说明: * @return com.stu.service.edu.entity.vo.CourseApiVo ***********************************/ CourseDto getCourseById(String id); }
xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.stu.service.edu.mapper.CourseMapper"> <resultMap id="baseBesultMap" type="com.stu.service.edu.entity.form.CourseInfoForm"> <id column="id" property="id"/> <result column="teacher_id" property="teacherId"/> <result column="subject_id" property="subjectId"/> <result column="subject_parent_id" property="subjectParentId"/> <result column="title" property="title"/> <result column="price" property="price"/> <result column="lesson_num" property="lessonNum"/> <result column="cover" property="cover"/> <result column="description" property="description"/> </resultMap> <select id="getCourseForm" resultMap="baseBesultMap"> SELECT t1.id, t1.teacher_id, t1.subject_id, t1.subject_parent_id, t1.title, t1.price, t1.lesson_num, t1.cover, t2.description FROM edu_course t1 LEFT JOIN edu_course_description t2 ON t1.id = t2.id WHERE t1.id = #{id} </select> <select id="getCoursePublishById" parameterType="java.lang.String" resultType="com.stu.service.edu.entity.vo.CoursePublishVo"> SELECT t1.id, t1.title, t1.price, t1.cover, t1.lesson_num, td.description, t3.title AS subjectLevelOne, t4.title AS subjectLevelTwo, t2.NAME AS teacherName, t3.id, t1.subject_id, t4.parent_id FROM edu_course t1 LEFT JOIN edu_course_description td ON td.id = t1.id LEFT JOIN edu_teacher t2 ON t2.id = t1.teacher_id LEFT JOIN edu_subject t3 ON t3.id = t1.subject_parent_id LEFT JOIN edu_subject t4 ON t4.id = t1.subject_id WHERE t1.id = #{courseId} </select> <!--课程分页--> <select id="pageCourses" resultType="com.stu.service.edu.entity.vo.CourseVo"> SELECT t1.id, t1.title, t1.cover, t1.lesson_num AS lessonNum, t1.price, t1.buy_count AS buyCount, t1.view_count AS viewCount, t1.`status`, t1.gmt_create AS gmtCreate, t1.gmt_modified AS gmtModified, t3.title AS subjectParentTitle, t4.title AS subjectTitle, t2.NAME AS teacherName, td.description FROM edu_course t1 LEFT JOIN edu_course_description td ON td.id = t1.id LEFT JOIN edu_teacher t2 ON t2.id = t1.teacher_id LEFT JOIN edu_subject t3 ON t3.id = t1.subject_parent_id LEFT JOIN edu_subject t4 ON t4.id = t1.subject_id ${ew.customSqlSegment} </select> <!--课程详情--> <select id="getApiCourseById" parameterType="java.lang.String" resultType="com.stu.service.edu.entity.vo.ApiCourseVo"> SELECT t1.id, t1.title, t1.price, t1.cover, t1.lesson_num, t1.buy_count, t1.view_count, t1.STATUS, t1.subject_parent_id AS subjectParentId, t1.subject_id AS subjectId, td.description, t2.intro, t2.avatar, t2.id AS teacherId, t3.title AS subjectParentTitle, t4.title AS subjectTitle, t2.NAME AS teacherName FROM edu_course t1 LEFT JOIN edu_course_description td ON td.id = t1.id LEFT JOIN edu_teacher t2 ON t2.id = t1.teacher_id LEFT JOIN edu_subject t3 ON t3.id = t1.subject_parent_id LEFT JOIN edu_subject t4 ON t4.id = t1.subject_id WHERE t1.id = #{id} </select> <select id="getCourseById" resultType="com.stu.service.base.dto.CourseDto"> SELECT t1.id, t1.title, t1.price, t1.cover, t2.NAME AS teacherName FROM edu_course t1 LEFT JOIN edu_teacher t2 ON t2.id = t1.teacher_id WHERE t1.id = #{id} </select> </mapper>
标签:插件,MyBatisPlusConfig,分页,service,stu,edu,import,com,id From: https://www.cnblogs.com/konglxblog/p/16794090.html