先一键生成实体类,并在在mapper、service、controller搭建好MyBatis-Plus的环境
在主键上添加上@TableId和自动递增。ById方法才能识别到。添加的时候也可以自动递增
package com.mashang.elearing.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.util.Date;
@Data
public class MsLesson {
@TableId(type = IdType.AUTO)
private Long lessonId;
private Long courseId;
private Long chapterId;
private String lessonName;
private Long lessonSort;
private String playName;
private String playUrl;
private String coursewareName;
private String coursewareUrl;
private String homeworkPublish;
private String homework;
private String delFlag;
private String createBy;
private Date createTime;
private String updateBy;
private Date updateTime;
private String remark;
}
添加不需要传lesson_id,可以设置自动递增
要传课程和章id,来确定是哪个章的哪个课程下的节
package com.mashang.elearing.domain.params.lesson;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@ApiModel("节添加参数")
@Data
public class MsLessonCreate {
@ApiModelProperty(value = "课程Id",required = true)
@NotBlank(message = "课程ID不能为空")
private String courseId;
@ApiModelProperty(value = "章id",required = true)
@NotNull(message = "章id不能为空")
private Long ChapterId;
@ApiModelProperty("节的名称")
@NotBlank(message = "节名称不能为空")
private String lessonName;
@ApiModelProperty(value = "序号")
private Long lessonSort;
}
添加要进行实体类转化,将MsLessonCreate 转成 MsLesson
package com.mashang.elearing.mapping;
import com.mashang.elearing.domain.MsLesson;
import com.mashang.elearing.domain.params.lesson.MsLessonCreate;
import org.mapstruct.factory.Mappers;
@Mapper
public interface MsLessonMapping {
MsLessonMapping INSTANCE = Mappers.getMapper(MsLessonMapping.class);
//plus是针对所有节信息进行操作
MsLesson to(MsLessonCreate create);
}
在mapper和service写上简单的添加接口,去Impl实现,节的添加涉及到的序号的变化
同章添加相同,先查询要填加的节序号是否存在,存在的话先把>=该序号的序号自增1
再执行操作
public void updateSort(MsLesson msLesson){
//查询当前节存不存在该序号
LambdaQueryWrapper qw = new LambdaQueryWrapper<>();
qw.ne(MsLesson::getDelFlag,"2");//排除假删除
qw.eq(MsLesson::getChapterId,msLesson.getChapterId());//从当前章找
qw.eq(MsLesson::getLessonSort,msLesson.getLessonSort());//需要如果有相同的话会查出来
Long count = msLessonMapper.selectCount(qw);
//如果有该节,需要将该节后面的sort+1
if (count > 0){
//将该章后面的sort+1
LambdaUpdateWrapper uw = new LambdaUpdateWrapper<>();
uw.ge(MsLesson::getLessonSort, msLesson.getLessonSort());
uw.setSql("lesson_sort = lesson_sort + 1");
msLessonMapper.update(null,uw);
}
}
//节的添加类似于章
@Override
public int create(MsLessonCreate create) {
//更新操作需要MsLessonCreate传参,然后传入需要用MsLesson
MsLesson msLesson = MsLessonMapping.INSTANCE.to(create);
//先调用updateSort方法将序号空出来
updateSort(msLesson);
//再执行添加操作
int insert = msLessonMapper.insert(msLesson);
return insert;
}
@Api(tags = "节模块")
@RestController
@RequestMapping("/lesson")
public class MsLessonController extends BaseController {
//注意这里注入的是Service的接口,不是它的实现类
@Autowired
private IMsLessonService msLessonService;
@ApiOperation("添加")
@PostMapping
public Result create(@RequestBody @Validated MsLessonCreate create){
//serviceImpl转过了这里就不用转了
//添加只需要返回简单的成功失败就行
return Result.to(msLessonService.create(create));
}
}
修改的需要的参数较多
修改的时候,courseId和chapter已经在添加的时候加上了,就不需要再传入了
package com.mashang.elearing.domain.params.lesson;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@ApiModel("节修改参数")
@Data
public class MsLessonupdate {
@ApiModelProperty(value = "节Id",required = true)
@NotNull(message = "节ID不能为空")
private Long lessonId;
@ApiModelProperty("节的名称")
@NotBlank(message = "节名称不能为null")
private String lessonName;
@ApiModelProperty(value = "序号")
private Long lessonSort;
@ApiModelProperty("视频文件昵称")
private String playName;
@ApiModelProperty("视频播放地址")
private String playUrl;
@ApiModelProperty("课件名称")
private String coursewareName;
@ApiModelProperty("课件地址")
private String coursewareUrl;
}
修改的操作跟添加都一样
mapping转化
MsLesson to(MsLessonupdate update);
service写上对应方法
int update(MsLessonupdate update);
@Override
public int update(MsLessonupdate update) {
MsLesson msLesson = MsLessonMapping.INSTANCE.to(update);
updateSort(msLesson);
return msLessonMapper.updateById(msLesson);
}
@ApiOperation("修改")
@PutMapping
public Result update(@RequestBody @Validated MsLessonupdate update){
return Result.to(msLessonService.update(update));
}
修改操作固定要先查询出之前的内容再修改
同修改相同,把要修改的内容查出来
package com.mashang.elearing.domain.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel("节详情列表")
@Data
public class MsLessonsDtlVo {
@ApiModelProperty("节id")
private Long lessonId;
@ApiModelProperty("节名称")
private String lessonName;
@ApiModelProperty("节排序")
private Long lessonSort;
@ApiModelProperty("视频文件昵称")
private String playName;
@ApiModelProperty("视频播放地址")
private String playUrl;
@ApiModelProperty("课件名称")
private String coursewareName;
@ApiModelProperty("课件地址")
private String coursewareUrl;
}
把MsLesson 转成MsLessonDtlVo
MsLessonsDtlVo to(MsLesson msLesson);
@ApiOperation("详情")
@GetMapping("/{lessonId}")
public Result getById(@PathVariable Long lessonId){
MsLesson msLesson = msLessonService.getById(lessonId);
return Result.success(MsLessonMapping.INSTANCE.to(msLesson));
}
节直接删除就行,不用像章一样需要考虑有关联到其他内容
单表,plus直接构造,在controller删掉就行
//删除接口用的假删,调用的是updateById,不是removeById
@ApiOperation("删除")
@DeleteMapping("/{lessonId}")
public Result deleteById(@PathVariable Long lessonId){
MsLesson msLesson = new MsLesson();
msLesson.setDelFlag("2");
msLesson.setLessonId(lessonId);
return Result.to(msLessonService.updateById(msLesson));
}
作业模块是依附于小节的,小节创建的时候默认作业为空,所以添加作业也就是修改作业
package com.mashang.elearing.domain.params.lesson;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@ApiModel("节作业添加修改参数")
@Data
public class MsLessonHomeworkUpdate {
@ApiModelProperty(value = "节Id",required = true)
@NotBlank(message = "节ID不能为空")
private String lessonId;
@ApiModelProperty(value = "发布状态,1发布,0补发布",required = true)
private Long homeworkPublish;
@ApiModelProperty(value = "作业内容",required = true)
private String homework;
}
实体类转化
MsLesson to(MsLessonHomeworkUpdate update);
@ApiOperation("添加/修改作业")
@PutMapping("/homework")
public Result update(@RequestBody @Validated MsLessonHomeworkUpdate update) {
MsLesson msLesson = MsLessonMapping.INSTANCE.to(update);
return Result.to(msLessonService.updateById(msLesson));
}
与添加的传参实体类内容一样,创建一个返回值的Vo实体类
package com.mashang.elearing.domain.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel("节作业详情")
@Data
public class MsLessonsHomeworkVo {
@ApiModelProperty("节id")
private Long lessonId;
@ApiModelProperty("是否发布(1发布、0不发布)")
private String homeworkPublish;
@ApiModelProperty("作业内容")
private String homework;
}
查询出来的是MsLesson,要转成MsLessonsHomeworkVo再返回
MsLessonsHomeworkVo toHomework(MsLesson msLesson);
//上面有一个查询节详情的,要区分开
@ApiOperation("查询作业详情")
@GetMapping("/homework/{lessonId}")
public Result getHomeworkById(@PathVariable Long lessonId){
MsLesson msLesson = msLessonService.getById(lessonId);
return Result.success(MsLessonMapping.INSTANCE.toHomework(msLesson));
}
批改前先把作业的信息查出来
这里要把所有的学生信息都查出来,谁提交了,谁没提交一目了然
将作业提交表(MsHomeworkSubm)导入domain
package com.mashang.elearing.domain;
import lombok.Data;
import java.util.Date;
@Data
public class MsHomeworkSubm {
private String homeworkName;
private Long homeworkSubmId;
private Long studentId;
private Long lessonId;
private String homeworkUrl;
private String pass;
private String comment;
private String delFlag;
private String createBy;
private Date createTime;
private String updateBy;
private Date updateTime;
private String remark;
}
在各个层搭建好plus的内容
批改前先查出作业提交信息
sys_user作为主表,左关联查询作业提交表,关联的时候要把假删的信息排除、也只关联当前小节的信息
SELECT
t1.user_id,
t1.nick_name,
t2.homework_subm_id,
t2.homework_url,
t2.pass,
t2.homework_name,
t2.`comment`
from sys_user t1
LEFT JOIN ms_homework_subm t2
on t1.user_id = t2.student_id
AND t2.lesson_id = #{lessonId}
AND t2.del_flag != '2'
-- 被关联的表需要的信息需要在关联的时候写上,不能在where后面写
-- 如果在where后面写,关联的内容是空的话,where后面怎么写,内容都查不出来
--lessonId是在作业提交表里的,如果要查出对应的节,必须在关联的时候写上
-- 因为只有这个时候,即使t2没被关联出来,内容也不是空的
where t1.class_id = #{classId}
AND t1.del_flag != '2'
ORDER BY t2.create_by DESC
-- 班级id是用来查出学生数据,节Id是用来查出对应节的作业信息
一个学生可以提交多次作业,是一对多的关联查询
package com.mashang.elearing.domain.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
import java.util.List;
@ApiModel("作业提交信息")
@Data
public class MshomeworkSubmVo {
@ApiModelProperty("用户id")
private Long userId;
@ApiModelProperty("学生姓名")
private String nickName;
@ApiModelProperty("作业提交详情")
private List homeworks;
}
package com.mashang.elearing.domain.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel("作业提交信息-详细信息")
public class MsHomeworkSubmDtlVo {
@ApiModelProperty("作业提交主键id")
private Long homeworkSubmId;
@ApiModelProperty("作业提交地址")
private String homeworkUrl;
@ApiModelProperty("是否通过")
private String pass;
@ApiModelProperty("评语")
private String comment;
@ApiModelProperty("作业名称")
private String homeworkName;
}
package com.mashang.elearing.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mashang.elearing.domain.MsHomeworkSubm;
import com.mashang.elearing.domain.vo.MshomeworkSubmVo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface MsHomeworkSubmMapper extends BaseMapper {
//mapper层里多个字段要用@Param区分
//classId用来找出对应学生信息,lessonId用来查出对应节的信息
List list(@Param("classId") Long classId, @Param("lessonId") Long lessonId);
}
package com.mashang.elearing.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.mashang.elearing.domain.MsHomeworkSubm;
import com.mashang.elearing.domain.vo.MshomeworkSubmVo;
import java.util.List;
public interface IMsHomeworkSubmService extends IService {
//service这里不需要用@param标记
List list(Long classId,Long lessonId);
}
package com.mashang.elearing.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mashang.elearing.domain.MsHomeworkSubm;
import com.mashang.elearing.domain.vo.MshomeworkSubmVo;
import com.mashang.elearing.mapper.MsHomeworkSubmMapper;
import com.mashang.elearing.service.IMsHomeworkSubmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MsHomeworkSubmServiceImpl extends ServiceImpl
implements IMsHomeworkSubmService {
@Autowired
private MsHomeworkSubmMapper msHomeworkSubmMapper;
@Override
public List list(Long classId, Long lessonId) {
return msHomeworkSubmMapper.list(classId, lessonId);
}
}
package com.mashang.elearing.controller;
import com.mashang.elearing.core.Result;
import com.mashang.elearing.domain.vo.MsHomeworkSubmDtlVo;
import com.mashang.elearing.service.IMsHomeworkSubmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Api(tags = "作业提交模块")
@RestController
@RequestMapping("/homework/submit")
public class MsHomeworkSubmController {
@Autowired
private IMsHomeworkSubmService msHomeworkSubmService;
@ApiOperation("查询小节作业提交详情")
@GetMapping("/list")
public Result> list(Long classId, Long lessonId){
//这里查出来的就是MsHomeworkSubmDtlVo,不需要转
return Result.success(msHomeworkSubmService.list(classId,lessonId));
}
}