作品管理-作品信息管理
  TEZNKK3IfmPf 2024年03月29日 35 0

修改 ​​CodeGeneratorTest.java​​ 复制出一个全新的测试方式,就是用来存储上一个章节中新建的表,如下

作品管理-作品信息管理

/**
* @author BNTang
*/
public class CodeGeneratorTest {
@Test
public void generate_video_author() {
// 1、创建代码生成器
AutoGenerator mpg = new AutoGenerator();
// 2、全局配置
GlobalConfig gc = new GlobalConfig();

gc.setOutputDir("D:\\Develop\\IdeaPro\\video_parent\\service\\service_video\\src\\main\\java");
gc.setAuthor("BNTang");
// 生成后是否打开资源管理器
gc.setOpen(false);
// 重新生成时文件是否覆盖
gc.setFileOverride(false);

// mp生成 service 层代码,默认接口名称第一个字母有 I
gc.setControllerName("%sController");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");

// 主键策略
gc.setIdType(IdType.ID_WORKER_STR);
// 定义生成的实体类中日期类型
gc.setDateType(DateType.ONLY_DATE);
// 开启Swagger2模式
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);

// 3、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/video_db?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();
// 模块名
pc.setModuleName("service_video");
pc.setParent("top.it6666");
pc.setController("controller");
pc.setEntity("entity");
pc.setService("service");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);

// 5、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("video_author");
// 数据库表映射到实体的命名策略,驼峰命名
strategy.setNaming(NamingStrategy.underline_to_camel);
// 生成实体时去掉表前缀
strategy.setTablePrefix("video_");
// 数据库表字段映射到实体的命名策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// lombok 模型 @Accessors(chain = true) setter链式操作
strategy.setEntityLombokModel(true);
// restful api风格控制器
strategy.setRestControllerStyle(true);
// url中驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
mpg.setStrategy(strategy);

// 6、执行
mpg.execute();
}

@Test
public void generate_video_category() {
// 1、创建代码生成器
AutoGenerator mpg = new AutoGenerator();
// 2、全局配置
GlobalConfig gc = new GlobalConfig();

gc.setOutputDir("D:\\Develop\\IdeaPro\\video_parent\\service\\service_video\\src\\main\\java");
gc.setAuthor("BNTang");
// 生成后是否打开资源管理器
gc.setOpen(false);
// 重新生成时文件是否覆盖
gc.setFileOverride(false);

// mp生成 service 层代码,默认接口名称第一个字母有 I
gc.setControllerName("%sController");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");

// 主键策略
gc.setIdType(IdType.ID_WORKER_STR);
// 定义生成的实体类中日期类型
gc.setDateType(DateType.ONLY_DATE);
// 开启Swagger2模式
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);

// 3、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/video_db?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();
// 模块名
pc.setModuleName("service_video");
pc.setParent("top.it6666");
pc.setController("controller");
pc.setEntity("entity");
pc.setService("service");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);

// 5、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("video_category");
// 数据库表映射到实体的命名策略,驼峰命名
strategy.setNaming(NamingStrategy.underline_to_camel);
// 生成实体时去掉表前缀
strategy.setTablePrefix("video_");
// 数据库表字段映射到实体的命名策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// lombok 模型 @Accessors(chain = true) setter链式操作
strategy.setEntityLombokModel(true);
// restful api风格控制器
strategy.setRestControllerStyle(true);
// url中驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
mpg.setStrategy(strategy);

// 6、执行
mpg.execute();
}

@Test
public void generate_video_chapter_video_content_video_content_description_video_content_video() {
// 1、创建代码生成器
AutoGenerator mpg = new AutoGenerator();
// 2、全局配置
GlobalConfig gc = new GlobalConfig();

gc.setOutputDir("D:\\Develop\\IdeaPro\\video_parent\\service\\service_video\\src\\main\\java");
gc.setAuthor("BNTang");
// 生成后是否打开资源管理器
gc.setOpen(false);
// 重新生成时文件是否覆盖
gc.setFileOverride(false);

// mp生成 service 层代码,默认接口名称第一个字母有 I
gc.setControllerName("%sController");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");

// 主键策略
gc.setIdType(IdType.ID_WORKER_STR);
// 定义生成的实体类中日期类型
gc.setDateType(DateType.ONLY_DATE);
// 开启Swagger2模式
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);

// 3、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/video_db?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();
// 模块名
pc.setModuleName("service_video");
pc.setParent("top.it6666");
pc.setController("controller");
pc.setEntity("entity");
pc.setService("service");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);

// 5、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("video_chapter", "video_content", "video_content_description", "video_content_video");
// 数据库表映射到实体的命名策略,驼峰命名
strategy.setNaming(NamingStrategy.underline_to_camel);
// 生成实体时去掉表前缀
strategy.setTablePrefix("video_");
// 数据库表字段映射到实体的命名策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// lombok 模型 @Accessors(chain = true) setter链式操作
strategy.setEntityLombokModel(true);
// restful api风格控制器
strategy.setRestControllerStyle(true);
// url中驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
mpg.setStrategy(strategy);

// 6、执行
mpg.execute();
}
}

直接拷贝我的即可,你也可以自行修改,供参考

作品管理-作品信息管理

编写后端

保存基本信息, 接收请求, 在接收之前首先先添加一个 VO 实体,实体代码如下

/**
* @author BNTang
*/
@Data
public class ContentVO {
private String id;

/**
* 标题
*/
private String title;

/**
* 二级分类ID
*/
private String categoryId;

/**
* 一级分类ID
*/
private String categoryParentId;

/**
* 作者id
*/
private String authorId;

/**
* 总视频数
*/
private Integer contentNum;

/**
* 简介
*/
private String description;

/**
* 封面
*/
private String cover;

/**
* 价格
*/
private BigDecimal price;
}

作品管理-作品信息管理

紧接着修改 ​​ContentController​​ 内容如下

/**
* <p>
* 作品表 前端控制器
* </p>
*
* @author BNTang
* @since 2021-04-08
*/
@RestController
@RequestMapping("/service_video/content")
@Api(tags = "作品组")
@CrossOrigin
public class ContentController {
@Resource
private ContentService contentService;

@PostMapping("/addContentInfo")
public ResponseResult addContentInfo(ContentVO contentVO) {
// 调用contentService处理添加作品信息业务
return ResponseResult.ok().data("contentId", contentService.saveContentInfo(contentVO));
}
}

作品管理-作品信息管理

在对应的接口当中添加对应的方法如下不贴图了,不重要的部分

/**
* <p>
* 作品表 服务类
* </p>
*
* @author BNTang
* @since 2021-04-08
*/
public interface ContentService extends IService<Content> {

/**
* <p>
* 保存基本信息
* </p>
*
* @param contentVO 基本信息实体VO
* @return 添加之后生成的ID
*/
String saveContentInfo(ContentVO contentVO);
}

实现业务方法,分为两步,一,先保存基本信息,获取自增ID后再保存简介设置简介ID为刚刚自增之后得到的ID入库

/**
* <p>
* 作品表 服务实现类
* </p>
*
* @author BNTang
* @since 2021-04-08
*/
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements ContentService {
/**
* 注入简介的业务 保存简介
*/
@Resource
private ContentDescriptionService contentDescriptionService;

@Override
public String saveContentInfo(ContentVO contentVO) {
// 1.保存基本信息
Content content = new Content();
BeanUtils.copyProperties(contentVO, content);
// 1.1.保存到数据当中
baseMapper.insert(content);

// 2.保存简介
// 2.1.获取刚保存基本信息的id,之后再设置自己的简介id
String id = content.getId();
ContentDescription contentDescription = new ContentDescription();
contentDescription.setId(id);
contentDescription.setDescription(contentVO.getDescription());
contentDescriptionService.save(contentDescription);
return id;
}
}

基本信息修改,后端接口,获取一条数据,ContentController

/**
* <p>
* 根据ID获取一条作品简介信息数据
* </p>
*/
@GetMapping("/getContentWithInfoId/{id}")
public ResponseResult getContentWithInfoId(@PathVariable String id) {
return ResponseResult.ok().data("contentVO", contentService.getContentWithInfoId(id));
}

修改 ContentService

/**
* <p>
* 根据ID获取一条作品简介信息数据
* </p>
*
* @param id 作品ID
* @return 作品简介信息
*/
ContentVO getContentWithInfoId(String id);

修改 ContentServiceImpl

@Override
public ContentVO getContentWithInfoId(String id) {
ContentVO contentVO = new ContentVO();
// 查询基本信息
Content content = baseMapper.selectById(id);
BeanUtils.copyProperties(content, contentVO);

// 查询简介信息
ContentDescription description = contentDescriptionService.getById(id);
contentVO.setDescription(description.getDescription());
return contentVO;
}

添加,修改作品基本信息接口,修改 ContentController

/**
* <p>
* 修改作品基本信息
* </p>
*/
@PostMapping("updateContentInfo")
public ResponseResult updateContentInfo(@RequestBody ContentVO contentVO) {
contentService.updateContentInfo(contentVO);
return ResponseResult.ok();
}

修改 ContentService

/**
* <p>
* 修改作品基本信息
* </p>
*
* @param contentVO 基本信息VO实体
*/
void updateContentInfo(ContentVO contentVO);

修改 ContentServiceImpl

@Override
public void updateContentInfo(ContentVO contentVO) {
// 1.修改基本信息
Content content = new Content();
BeanUtils.copyProperties(contentVO, content);
baseMapper.updateById(content);

// 2.修改描述信息
ContentDescription contentDescription = new ContentDescription();
contentDescription.setId(content.getId());
contentDescription.setDescription(contentVO.getDescription());
contentDescriptionService.updateById(contentDescription);
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年03月29日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   25   0   0 mysql
  TEZNKK3IfmPf   2024年05月17日   49   0   0 sqlmysql
  TEZNKK3IfmPf   2024年05月31日   29   0   0 数据库mysql
  TEZNKK3IfmPf   2024年05月17日   48   0   0 查询mysql索引
  TEZNKK3IfmPf   2024年05月17日   50   0   0 jsonmysql
  TEZNKK3IfmPf   2024年05月17日   48   0   0 mysqlphp
  TEZNKK3IfmPf   2024年05月31日   27   0   0 数据库mysql
TEZNKK3IfmPf