首先本项目将实体类单独放在model包中,方便后期集体的调用:
整个项目的建包结构都是在java包中建立com.lxl.ggkt包,方便后期包路径的扫描。
上述有三个包:
enums包规定了一些状态规定,比如优惠券是否使用,规定code=0为未使用,code=1为使用等等,这些不展开说明。
model则为对应的实体类。
vo是对数据的封装,往往前端需要我们返回对应数据的集合,这样就需要对得到的数据进行集合封装。
vod最终配置文件如下:
server.port=8301
spring.application.name=service-vod
spring.profiles.active=dev
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/glkt_vod?characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456lxl
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
spring.servlet.multipart.max-file-size=1024MB
spring.servlet.multipart.max-request-size=1024MB
tencent.cos.file.region=ap-beijing
tencent.cos.file.secretid=AKIDQjskRxPgs1HPNl0LSM3c2GF3jGbkiXCh
tencent.cos.file.secretkey=TsBVoARdensvvqlfVI2ueJwjP1a3Tl07
tencent.cos.file.bucketname=ggkt-1312247070
tencent.video.appid=1312247070
mybatis-plus.mapper-locations=classpath:com/lxl/ggkt/vod/mapper/xml/*.xml
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
TeacherController:
主要涉及到查询所有讲师,删除讲师信息,条件分页查询讲师信息,增加讲师信息,修改讲师信息,批量删除讲师信息。
package com.lxl.ggkt.vod.controller;
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.lxl.ggkt.exception.GgktException;
import com.lxl.ggkt.model.vod.Teacher;
import com.lxl.ggkt.result.Result;
import com.lxl.ggkt.vo.vod.TeacherQueryVo;
import com.lxl.ggkt.vod.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* <p>
* 讲师 前端控制器
* </p>
*
* @author lxl
* @since 2022-07-16
*/
@Api(tags = "讲师管理接口")
@RestController
@RequestMapping("/admin/vod/teacher")
//@CrossOrigin
public class TeacherController {
@Autowired
private TeacherService teacherService;
//查询所有讲师
// @ApiOperation("查询所有讲师")
// @GetMapping("findAll")
// public List<Teacher> findAllTeacher(){
// List<Teacher> list = teacherService.list();
// return list;
// }
@ApiOperation("查询所有讲师")
@GetMapping("findAll")
public Result findAllTeacher() {
// try{
// int i = 10/0;
// }catch (Exception e){
// throw new GgktException(201,"执行了自定义异常处理GgktException");
// }
List<Teacher> list = teacherService.list();
return Result.ok(list).message("查询数据成功");
}
//删除讲师
@ApiOperation("逻辑删除讲师")
@DeleteMapping("remove/{id}")
public Result removeTeacher(@ApiParam(name = "id", value = "ID", required = true) @PathVariable Long id){
boolean isSuccess = teacherService.removeById(id);
if (isSuccess){
return Result.ok(null);
}else{
return Result.fail(null);
}
}
//条件查询分页
@ApiOperation("条件查询分页")
@PostMapping("findQueryPage/{current}/{limit}")
public Result findPage(@PathVariable long current,
@PathVariable long limit,
@RequestBody(required = false) TeacherQueryVo teacherQueryVo){
Page<Teacher> pageParam = new Page<>(current, limit);
if (teacherQueryVo == null){
IPage<Teacher> pageModel = teacherService.page(pageParam,null);
return Result.ok(pageModel);
}else{
String name = teacherQueryVo.getName();
Integer level = teacherQueryVo.getLevel();
String joinDateBegin = teacherQueryVo.getJoinDateBegin();
String joinDateEnd = teacherQueryVo.getJoinDateEnd();
QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
if (!StringUtils.isEmpty(name)){
wrapper.like("name",name);
}
if (!StringUtils.isEmpty(level)){
wrapper.like("level",level);
}
if (!StringUtils.isEmpty(joinDateBegin)){
wrapper.ge("join_date",joinDateBegin);
}
if (!StringUtils.isEmpty(joinDateEnd)){
wrapper.le("join_date",joinDateEnd);
}
IPage<Teacher> pageModel = teacherService.page(pageParam, wrapper);
return Result.ok(pageModel);
}
}
//增加讲师
@ApiOperation("增加讲师")
@PostMapping("saveTeacher")
public Result saveTeacher(@RequestBody Teacher teacher){
boolean isSuccess = teacherService.save(teacher);
if (isSuccess){
return Result.ok(null);
}else{
return Result.fail(null);
}
}
//修改讲师
@ApiOperation("根据id查询")
@GetMapping("getTeacher/{id}")
public Result getTeacher(@PathVariable Long id){
Teacher teacher = teacherService.getById(id);
return Result.ok(teacher);
}
@ApiOperation("修改最终实现")
@PostMapping("updateTeacher")
public Result updateTeacher(@RequestBody Teacher teacher){
boolean isSuccess = teacherService.updateById(teacher);
if (isSuccess){
return Result.ok(null);
}else{
return Result.fail(null);
}
}
//批量删除讲师
@ApiOperation("批量删除讲师")
@DeleteMapping("removeBatch")
public Result removeBatch(@RequestBody List<Long> idList){
boolean isSuccess = teacherService.removeByIds(idList);
if (isSuccess){
return Result.ok(null);
}else{
return Result.fail(null);
}
}
}
TeacherServiceImpl:
简单继承对应接口,因为TeacherController调用方法都为mybatis-plus提供的方法,所以无需其他方法构造。
package com.lxl.ggkt.vod.service.impl;
import com.lxl.ggkt.model.vod.Teacher;
import com.lxl.ggkt.vod.mapper.TeacherMapper;
import com.lxl.ggkt.vod.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* 讲师 服务实现类
* </p>
*
* @author atguigu
* @since 2022-07-16
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
}
TeacherQueryVo的数据封装:
@Data
public class TeacherQueryVo {
@ApiModelProperty(value = "讲师姓名")
private String name;
@ApiModelProperty(value = "头衔 1高级讲师 2首席讲师")
private Integer level;
@ApiModelProperty(value = "入驻时间")
private String joinDateBegin;
@ApiModelProperty(value = "入驻时间")
private String joinDateEnd;
}