-
- 2.1、生成的实体类
-
- 2.1.1、eduCourse
-
2.1.2、eduCourseDescription
-
3、创建vo类,封装添加课程信息的表单提交的数据
-
4、完善控制层,编写添加课程信息代码
-
5、完善业务层代码
-
- 5.1、完善业务层接口代码
-
5.1、完善业务层实现类代码
-
6、测试
======================================================================
链接:https://pan.baidu.com/s/1Q72hP6aCHvAS25flkvqUuQ
提取码:daqk
复制这段内容后打开百度网盘手机App,操作更方便哦
==============================================================================
代码生成器
package com.nonglin.demo;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;
/**
-
@author
-
@since 2021/2/21
*/
public class CodeGenerator {
@Test
public void run() {
// 1、创建代码生成器
AutoGenerator mpg = new AutoGenerator();
// 2、全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty(“user.dir”);
//修改1、projectPath:替换代码生成的绝对路径
gc.setOutputDir(“E:\workspaces\IDEA\SpringBoot\cui_parent\service\service_edu” + “/src/main/java”);
gc.setAuthor(“Lenovo”);//生成代码作者的名字
gc.setOpen(false); //生成后是否打开资源管理器
gc.setFileOverride(false); //重新生成时文件是否覆盖
gc.setServiceName(“%sService”); //去掉Service接口的首字母I
gc.setIdType(IdType.ID_WORKER_STR); //主键策略
gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
gc.setSwagger2(true);//开启Swagger2模式
mpg.setGlobalConfig(gc);
// 3、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
//修改2、
dsc.setUrl(“jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8”);
dsc.setDriverName(“com.mysql.cj.jdbc.Driver”);
dsc.setUsername(“root”);
dsc.setPassword(“root”);
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 4、包配置
PackageConfig pc = new PackageConfig();
//修改3、
//会生成com.nonglin.eduservice包
pc.setModuleName(“eduservice”); //模块名
pc.setParent(“com.nonglin”);
pc.setController(“controller”);//会生成com.nonglin.eduservice.controller包
pc.setEntity(“entity”);//会生成com.nonglin.eduservice.entity包
pc.setService(“service”);//会生成com.nonglin.eduservice.service包
pc.setMapper(“mapper”);//会生成com.nonglin.eduservice.mapper包
mpg.setPackageInfo(pc);
// 5、策略配置
StrategyConfig strategy = new StrategyConfig();
//修改4、
//对应到数据库中的表
strategy.setInclude(“edu_chapter”,“edu_course”,“edu_course_description”,“edu_video”);
strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
strategy.setTablePrefix(pc.getModuleName() + “_”); //生成实体时去掉表前缀
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
strategy.setRestControllerStyle(true); //restful api风格控制器
strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
mpg.setStrategy(strategy);
// 6、执行
mpg.execute();
}
}
2.1.1、eduCourse
package com.nonglin.eduservice.entity;
import java.math.BigDecimal;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
-
-
课程
-
@author Lenovo
-
@since 2021-03-09
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value=“EduCourse对象”, description=“课程”)
public class EduCourse implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = “课程ID”)
@TableId(value = “id”, type = IdType.ID_WORKER_STR)
private String id;
@ApiModelProperty(value = “课程讲师ID”)
private String teacherId;
@ApiModelProperty(value = “课程专业ID”)
private String subjectId;
@ApiModelProperty(value = “课程专业父级ID”)
private String subjectParentId;
@ApiModelProperty(value = “课程标题”)
private String title;
@ApiModelProperty(value = “课程销售价格,设置为0则可免费观看”)
private BigDecimal price;
@ApiModelProperty(value = “总课时”)
private Integer lessonNum;
@ApiModelProperty(value = “课程封面图片路径”)
private String cover;
@ApiModelProperty(value = “销售数量”)
private Long buyCount;
@ApiModelProperty(value = “浏览数量”)
private Long viewCount;
@ApiModelProperty(value = “乐观锁”)
private Long version;
@ApiModelProperty(value = “课程状态 Draft未发布 Normal已发布”)
private String status;
@ApiModelProperty(value = “逻辑删除 1(true)已删除, 0(false)未删除”)
private Integer isDeleted;
@ApiModelProperty(value = “创建时间”)
@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;
@ApiModelProperty(value = “更新时间”)
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;
}
2.1.2、eduCourseDescription
package com.nonglin.eduservice.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
-
-
课程简介
-
@author Lenovo
-
@since 2021-03-09
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value=“EduCourseDescription对象”, description=“课程简介”)
public class EduCourseDescription implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = “课程ID”)
@TableId(value = “id”, type = IdType.INPUT)
private String id;
@ApiModelProperty(value = “课程简介”)
private String description;
@ApiModelProperty(value = “创建时间”)
@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;
@ApiModelProperty(value = “更新时间”)
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;
}
=======================================================================================
因为添加课程信息的表单中包含的课程简介信息,课程简介信息在课程表(edu_course)中没有设置该字段,而是存在一张单独的表edu_course_description表中,所以需要设置一个封装类,用于封装添加课程表单所有信息。
代码:
package com.nonglin.eduservice.entity.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
/**
-
@Description :封装添加课程信息表单的数据
-
@Author :lenovo
-
@Date :2021/3/9 11:04
*/
@ApiModel(value = “课程基本信息”, description = “编辑课程基本信息的表单对象”)
@Data
public class CourseInfoVo implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = “课程ID”)
private String id;
@ApiModelProperty(value = “课程讲师ID”)
private String teacherId;
@ApiModelProperty(value = “课程专业ID”)
private String subjectId;
@ApiModelProperty(value = “课程标题”)
private String title;
@ApiModelProperty(value = “课程销售价格,设置为0则可免费观看”)
private BigDecimal price;
@ApiModelProperty(value = “总课时”)
private Integer lessonNum;
@ApiModelProperty(value = “课程封面图片路径”)
private String cover;
@ApiModelProperty(value = “课程简介”)
private String description;
}
=================================================================================
package com.nonglin.eduservice.controller;
import com.nonglin.commonutils.R;
import com.nonglin.eduservice.entity.vo.CourseInfoVo;
import com.nonglin.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
-
-
课程 前端控制器
-
@author Lenovo
-
@since 2021-03-09
*/
@Api(description=“课程管理”)
@RestController
@RequestMapping(“/eduservice/course”)
@CrossOrigin
public class EduCourseController {
@Autowired
private EduCourseService courseService;
//添加课程信息
@ApiOperation(value = “添加课程信息”)
@PostMapping(“addCourseInfo”)
public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
courseService.saveCourseInfo(courseInfoVo);
return R.ok();
}
}
========================================================================
package com.nonglin.eduservice.service;
import com.nonglin.eduservice.entity.EduCourse;
import com.baomidou.mybatisplus.extension.service.IService;
import com.nonglin.eduservice.entity.vo.CourseInfoVo;