查询所有课程 接口信息如下
查询DTO QueryCourseParamsDTO @Data public class QueryCourseParamsDTO implements Serializable { private static final long serialVersionUID = 1L ; private String auditStatus; private String courseName; private String publishStatus; }
定义Service 找到CourseBaseService
接口,定义课程查询接口
CourseBaseService public interface CourseBaseService extends IService <CourseBase> { public PageResult<CourseBase> queryCourseBaseList (PageParam pageParam, QueryCourseParamsDTO dto) ; }
实现该方法,找到其实现类CourseBaseServiceImp
CourseBaseServiceImpl @Service public class CourseBaseServiceImpl extends ServiceImpl <CourseBaseMapper, CourseBase> implements CourseBaseService { @Override public PageResult<CourseBase> queryCourseBaseList (PageParam pageParam, QueryCourseParamsDTO dto) { LambdaQueryWrapper<CourseBase> wrapper = Wrappers.<CourseBase>lambdaQuery() .like(StringUtils.hasText(dto.getCourseName()), CourseBase::getName, dto.getCourseName()) .eq(StringUtils.hasText(dto.getAuditStatus()), CourseBase::getAuditStatus, dto.getAuditStatus()) .eq(StringUtils.hasText(dto.getPublishStatus()), CourseBase::getStatus, dto.getPublishStatus()); Page<CourseBase> page = new Page <>(pageParam.getPageNo(), pageParam.getPageSize()); Page<CourseBase> pageResult = page(page, wrapper); return new PageResult <>(pageResult.getRecords(), pageResult.getTotal(), pageParam); } }
配置分页插件 在learning-online-content-service
工程下的com.swx.content.config
包下创建分页配置类,其内容如下:
MybatisPlusConfig @Configuration @MapperScan("com.swx.content.mapper") public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor () { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor (); interceptor.addInnerInterceptor(new PaginationInnerInterceptor (DbType.MYSQL)); return interceptor; } }
Mybatis分页插件的原理
首先分页参数放到ThreadLocal中,拦截执行的sql,根据数据库类型添加对应的分页语句重写sql,例如:(select * from table where a)转换为(select count(*) from table where a)和(select * from table where a limit ,),计算出total总条数,pageNum当前第几页,pageSize每页大小和当前页数据,是否为首页,是否为尾页,总页数等。
定义Controller CourseBaseInfoController @Api(value = "课程基本信息管理接口") @RestController @ResponseResult @RequestMapping("/course") public class CourseBaseInfoController { private final CourseBaseService courseBaseService; public CourseBaseInfoController (CourseBaseService courseBaseService) { this .courseBaseService = courseBaseService; } @ApiOperation("课程查询接口") @PostMapping("/list") public PageResult<CourseBase> list (PageParam pageParam, @RequestBody(required = false) QueryCourseParamsDTO dto) { return courseBaseService.queryCourseBaseList(pageParam, dto); } }
课程查询信息服务以提供,打开前端项目查看是否成功
Push到Git
新增课程信息 接口信息如下
新增DTO QueryCourseParamsDTO @Data @ApiModel(value = "AddCourseDTO", description = "新增课程基本信息") public class AddCourseDTO { @NotEmpty(message = "课程名称不能为空") @ApiModelProperty(value = "课程名称", required = true) private String name; @NotEmpty(message = "适用人群不能为空") @Size(message = "适用人群内容过少", min = 10) @ApiModelProperty(value = "适用人群", required = true) private String users; @ApiModelProperty(value = "课程标签") private String tags; @NotEmpty(message = "课程分类不能为空") @ApiModelProperty(value = "课程大分类", required = true) private String mt; @NotEmpty(message = "课程分类不能为空") @ApiModelProperty(value = "课程小分类", required = true) private String st; @NotEmpty(message = "课程等级不能为空") @ApiModelProperty(value = "课程等级", required = true) private String grade; @NotEmpty(message = "课程等级不能为空") @ApiModelProperty(value = "教学模式(普通,录播,直播等)", required = true) private String teachmode; @ApiModelProperty(value = "课程介绍") private String description; @ApiModelProperty(value = "课程图片") private String pic; @NotEmpty(message = "收费规则不能为空") @ApiModelProperty(value = "收费规则,对应数据字典", required = true) private String charge; @ApiModelProperty(value = "价格") private Float price; @ApiModelProperty(value = "原价") private Float originPrice; @ApiModelProperty(value = "QQ") private String qq; @ApiModelProperty(value = "微信") private String wechat; @ApiModelProperty(value = "电话") private String phone; @ApiModelProperty(value = "有效期") private Integer validDays; }
视图VO CourseBaseInfoVO @Data @EqualsAndHashCode(callSuper = true) public class CourseBaseInfoVO extends CourseBase { private String users; private String tags; private String mt; private String st; private String mtName; private String stName; private String grade; private String teachmode; private String description; private String pic; private String charge; private Float price; private Float originPrice; private String qq; private String wechat; private String phone; private String validDays; }
定义Service 找到CourseBaseService
接口,定义课程新增接口
CourseBaseService public CourseBaseInfoVO createCourseBase (Long companyId, AddCourseDTO dto) ;
实现该方法,找到其实现类CourseBaseServiceImp
,添加如下代码:
CourseBaseServiceImpl private final CourseMarketService courseMarketService;private final CourseCategoryService courseCategoryService;public CourseBaseServiceImpl (CourseMarketService courseMarketService, CourseCategoryService courseCategoryService) { this .courseMarketService = courseMarketService; this .courseCategoryService = courseCategoryService; }@Override @Transactional(rollbackFor = Exception.class) public CourseBaseInfoVO createCourseBase (Long companyId, AddCourseDTO dto) { CourseBase courseBase = new CourseBase (); BeanUtils.copyProperties(dto, courseBase); courseBase.setCompanyId(companyId); courseBase.setCreateDate(LocalDateTime.now()); courseBase.setStatus("203001" ); courseBase.setAuditStatus("202002" ); boolean save = save(courseBase); if (!save) { throw new BizException ("添加课程失败" ); } CourseMarket courseMarket = new CourseMarket (); BeanUtils.copyProperties(dto, courseMarket); courseMarket.setId(courseBase.getId()); courseMarketService.saveOrUpdateCourseMarket(courseMarket); return getCourseBaseInfo(courseMarket.getId()); }public CourseBaseInfoVO getCourseBaseInfo (long courseId) { CourseBase courseBase = getById(courseId); if (courseBase == null ) { return null ; } CourseMarket courseMarket = courseMarketService.getById(courseId); CourseBaseInfoVO courseBaseInfoVO = new CourseBaseInfoVO (); BeanUtils.copyProperties(courseBase, courseBaseInfoVO); BeanUtils.copyProperties(courseMarket, courseBaseInfoVO); CourseCategory mtCategory = courseCategoryService.getById(courseBase.getMt()); CourseCategory stCategory = courseCategoryService.getById(courseBase.getSt()); courseBaseInfoVO.setMtName(mtCategory == null ? "" : mtCategory.getName()); courseBaseInfoVO.setStName(stCategory == null ? "" : stCategory.getName()); return courseBaseInfoVO; }
定义Controller 找到CourseBaseInfoController
,添加新增课程服务
CourseBaseInfoController @ApiOperation("新增课程") @PostMapping("") public CourseBaseInfoVO createCourseBase (@RequestBody @Validated(ValidationGroup.Insert.class) AddCourseDTO dto) { Long companyId = 1232141425L ; return courseBaseService.createCourseBase(companyId, dto); }
新增课程信息服务以提供,打开前端项目查看是否成功
Push到Git
修改课程信息 接口信息如下
定义Service 找到CourseBaseService
接口,定义课程新增接口
CourseBaseService CourseBaseInfoVO updateCourseBase (Long companyId, EditCourseDTO dto) ;
实现该方法,找到其实现类CourseBaseServiceImp
,添加如下代码:
CourseBaseServiceImpl @Override @Transactional(rollbackFor = Exception.class) public CourseBaseInfoVO updateCourseBase (Long companyId, EditCourseDTO dto) { CourseBase dbCourseBase = getById(dto.getId()); if (dbCourseBase == null ) { throw new BizException (ResultCodeEnum.DATA_NOT_EXIST); } if (!companyId.equals(dbCourseBase.getCompanyId())) { throw new BizException ("本机构只能修改本机构的课程" ); } BeanUtils.copyProperties(dto, dbCourseBase); dbCourseBase.setCreateDate(LocalDateTime.now()); boolean updateBase = updateById(dbCourseBase); CourseMarket courseMarket = new CourseMarket (); BeanUtils.copyProperties(dto, courseMarket); boolean updateMarket = courseMarketService.saveOrUpdateCourseMarket(courseMarket); if (!updateBase || !updateMarket) { throw new BizException ("修改课程失败" ); } return getCourseBaseInfo(dbCourseBase.getId()); }
定义Controller 找到CourseBaseInfoController
,添加新增课程服务
CourseBaseInfoController @ApiOperation("修改课程") @PutMapping("") public CourseBaseInfoVO updateCourseBase (@RequestBody @Validated EditCourseDTO dto) { Long companyId = 1232141425L ; return courseBaseService.updateCourseBase(companyId, dto); }
新增课程信息服务以提供,打开前端项目查看是否成功
Push到Git
查询具体课程 接口信息如下
定义Service 找到CourseBaseService
接口,定义课程新增接口
CourseBaseService public CourseBaseInfoVO getCourseBaseInfo (Long courseId) ;
实现该方法,找到其实现类CourseBaseServiceImp
,修改代码如下:
CourseBaseServiceImpl @Override public CourseBaseInfoVO getCourseBaseInfo (Long courseId) { CourseBase courseBase = getById(courseId); if (courseBase == null ) { return null ; } CourseMarket courseMarket = Optional.ofNullable(courseMarketService.getById(courseId)).orElse(new CourseMarket ()); CourseBaseInfoVO courseBaseInfoVO = new CourseBaseInfoVO (); BeanUtils.copyProperties(courseMarket, courseBaseInfoVO); BeanUtils.copyProperties(courseBase, courseBaseInfoVO); CourseCategory mtCategory = courseCategoryService.getById(courseBase.getMt()); CourseCategory stCategory = courseCategoryService.getById(courseBase.getSt()); courseBaseInfoVO.setMtName(mtCategory == null ? "" : mtCategory.getName()); courseBaseInfoVO.setStName(stCategory == null ? "" : stCategory.getName()); return courseBaseInfoVO; }
定义Controller接口信息 找到CourseBaseInfoController
,添加新增课程服务
CourseBaseInfoController @ApiOperation("根据课程id查询接口") @GetMapping("/{courseId}") public CourseBaseInfoVO list (@PathVariable("courseId") Long courseId) { return courseBaseService.getCourseBaseInfo(courseId); }
更新课程信息 接口信息如下
定义Service 找到CourseBaseService
接口,定义课程新增接口
CourseBaseService CourseBaseInfoVO updateCourseBase (Long companyId, EditCourseDTO dto) ;
实现该方法,找到其实现类CourseBaseServiceImp
,添加如下代码:
CourseBaseServiceImpl @Override @Transactional(rollbackFor = Exception.class) public CourseBaseInfoVO updateCourseBase (Long companyId, EditCourseDTO dto) { CourseBase dbCourseBase = getById(dto.getId()); if (dbCourseBase == null ) { throw new BizException (ResultCodeEnum.DATA_NOT_EXIST); } if (!companyId.equals(dbCourseBase.getCompanyId())) { throw new BizException ("本机构只能修改本机构的课程" ); } BeanUtils.copyProperties(dto, dbCourseBase); dbCourseBase.setChangeDate(LocalDateTime.now()); boolean updateBase = updateById(dbCourseBase); CourseMarket courseMarket = new CourseMarket (); BeanUtils.copyProperties(dto, courseMarket); boolean updateMarket = courseMarketService.saveOrUpdateCourseMarket(courseMarket); if (!updateBase || !updateMarket) { throw new BizException ("修改课程失败" ); } return getCourseBaseInfo(dbCourseBase.getId()); }
定义Controller 找到CourseBaseInfoController
,添加新增课程服务
CourseBaseInfoController @ApiOperation("修改课程") @PutMapping("") public CourseBaseInfoVO updateCourseBase (@RequestBody @Validated EditCourseDTO dto) { Long companyId = 1232141425L ; return courseBaseService.updateCourseBase(companyId, dto); }
Push到Git