代码生成:增加 tree 示例

This commit is contained in:
YunaiV 2023-11-16 20:40:13 +08:00
parent 01a55b93c2
commit 5b9879ca8d
14 changed files with 459 additions and 4 deletions

View File

@ -60,5 +60,10 @@ public interface ErrorCodeConstants {
// ========== 学生 1-001-201-000 ==========
ErrorCode DEMO01_CONTACT_NOT_EXISTS = new ErrorCode(1_001_201_000, "示例联系人不存在");
ErrorCode DEMO02_CATEGORY_NOT_EXISTS = new ErrorCode(1_001_201_001, "示例分类不存在");
ErrorCode DEMO02_CATEGORY_EXITS_CHILDREN = new ErrorCode(1_001_201_002, "存在存在子示例分类,无法删除");
ErrorCode DEMO02_CATEGORY_PARENT_NOT_EXITS = new ErrorCode(1_001_201_003,"父级示例分类不存在");
ErrorCode DEMO02_CATEGORY_PARENT_ERROR = new ErrorCode(1_001_201_004, "不能设置自己为父示例分类");
ErrorCode DEMO02_CATEGORY_NAME_DUPLICATE = new ErrorCode(1_001_201_005, "已经存在该名字的示例分类");
ErrorCode DEMO02_CATEGORY_PARENT_IS_CHILD = new ErrorCode(1_001_201_006, "不能设置自己的子示例分类为父示例分类");
}

View File

@ -0,0 +1,94 @@
package cn.iocoder.yudao.module.infra.controller.admin.demo.demo02;
import cn.iocoder.yudao.module.infra.controller.admin.demo.demo02.vo.Demo02CategoryListReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.demo.demo02.vo.Demo02CategoryRespVO;
import cn.iocoder.yudao.module.infra.controller.admin.demo.demo02.vo.Demo02CategorySaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.infra.controller.admin.demo02.vo.*;
import cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo02.Demo02CategoryDO;
import cn.iocoder.yudao.module.infra.service.demo.demo02.Demo02CategoryService;
@Tag(name = "管理后台 - 示例分类")
@RestController
@RequestMapping("/infra/demo02-category")
@Validated
public class Demo02CategoryController {
@Resource
private Demo02CategoryService demo02CategoryService;
@PostMapping("/create")
@Operation(summary = "创建示例分类")
@PreAuthorize("@ss.hasPermission('infra:demo02-category:create')")
public CommonResult<Long> createDemo02Category(@Valid @RequestBody Demo02CategorySaveReqVO createReqVO) {
return success(demo02CategoryService.createDemo02Category(createReqVO));
}
@PutMapping("/update")
@Operation(summary = "更新示例分类")
@PreAuthorize("@ss.hasPermission('infra:demo02-category:update')")
public CommonResult<Boolean> updateDemo02Category(@Valid @RequestBody Demo02CategorySaveReqVO updateReqVO) {
demo02CategoryService.updateDemo02Category(updateReqVO);
return success(true);
}
@DeleteMapping("/delete")
@Operation(summary = "删除示例分类")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('infra:demo02-category:delete')")
public CommonResult<Boolean> deleteDemo02Category(@RequestParam("id") Long id) {
demo02CategoryService.deleteDemo02Category(id);
return success(true);
}
@GetMapping("/get")
@Operation(summary = "获得示例分类")
@Parameter(name = "id", description = "编号", required = true, example = "1024")
@PreAuthorize("@ss.hasPermission('infra:demo02-category:query')")
public CommonResult<Demo02CategoryRespVO> getDemo02Category(@RequestParam("id") Long id) {
Demo02CategoryDO demo02Category = demo02CategoryService.getDemo02Category(id);
return success(BeanUtils.toBean(demo02Category, Demo02CategoryRespVO.class));
}
@GetMapping("/list")
@Operation(summary = "获得示例分类列表")
@PreAuthorize("@ss.hasPermission('infra:demo02-category:query')")
public CommonResult<List<Demo02CategoryRespVO>> getDemo02CategoryList(@Valid Demo02CategoryListReqVO listReqVO) {
List<Demo02CategoryDO> list = demo02CategoryService.getDemo02CategoryList(listReqVO);
return success(BeanUtils.toBean(list, Demo02CategoryRespVO.class));
}
@GetMapping("/export-excel")
@Operation(summary = "导出示例分类 Excel")
@PreAuthorize("@ss.hasPermission('infra:demo02-category:export')")
@OperateLog(type = EXPORT)
public void exportDemo02CategoryExcel(@Valid Demo02CategoryListReqVO listReqVO,
HttpServletResponse response) throws IOException {
List<Demo02CategoryDO> list = demo02CategoryService.getDemo02CategoryList(listReqVO);
// 导出 Excel
ExcelUtils.write(response, "示例分类.xls", "数据", Demo02CategoryRespVO.class,
BeanUtils.toBean(list, Demo02CategoryRespVO.class));
}
}

View File

@ -0,0 +1,25 @@
package cn.iocoder.yudao.module.infra.controller.admin.demo.demo02.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@Schema(description = "管理后台 - 示例分类列表 Request VO")
@Data
public class Demo02CategoryListReqVO {
@Schema(description = "名字", example = "芋艿")
private String name;
@Schema(description = "父级编号", example = "6080")
private Long parentId;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;
}

View File

@ -0,0 +1,31 @@
package cn.iocoder.yudao.module.infra.controller.admin.demo.demo02.vo;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
@Schema(description = "管理后台 - 示例分类 Response VO")
@Data
@ExcelIgnoreUnannotated
public class Demo02CategoryRespVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "10304")
@ExcelProperty("编号")
private Long id;
@Schema(description = "名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋艿")
@ExcelProperty("名字")
private String name;
@Schema(description = "父级编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "6080")
@ExcelProperty("父级编号")
private Long parentId;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
@ExcelProperty("创建时间")
private LocalDateTime createTime;
}

View File

@ -0,0 +1,24 @@
package cn.iocoder.yudao.module.infra.controller.admin.demo.demo02.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@Schema(description = "管理后台 - 示例分类新增/修改 Request VO")
@Data
public class Demo02CategorySaveReqVO {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "10304")
private Long id;
@Schema(description = "名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋艿")
@NotEmpty(message = "名字不能为空")
private String name;
@Schema(description = "父级编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "6080")
@NotNull(message = "父级编号不能为空")
private Long parentId;
}

View File

@ -0,0 +1,41 @@
package cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo02;
import lombok.*;
import java.util.*;
import java.time.LocalDateTime;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.*;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
/**
* 示例分类 DO
*
* @author 芋道源码
*/
@TableName("infra_demo02_category")
@KeySequence("infra_demo02_category_seq") // 用于 OraclePostgreSQLKingbaseDB2H2 数据库的主键自增如果是 MySQL 等数据库可不写
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Demo02CategoryDO extends BaseDO {
public static final Long PARENT_ID_ROOT = 0L;
/**
* 编号
*/
@TableId
private Long id;
/**
* 名字
*/
private String name;
/**
* 父级编号
*/
private Long parentId;
}

View File

@ -1 +0,0 @@
package cn.iocoder.yudao.module.infra.dal.dataobject.demo;

View File

@ -0,0 +1,35 @@
package cn.iocoder.yudao.module.infra.dal.mysql.demo02;
import java.util.*;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.infra.controller.admin.demo.demo02.vo.Demo02CategoryListReqVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo02.Demo02CategoryDO;
import org.apache.ibatis.annotations.Mapper;
/**
* 示例分类 Mapper
*
* @author 芋道源码
*/
@Mapper
public interface Demo02CategoryMapper extends BaseMapperX<Demo02CategoryDO> {
default List<Demo02CategoryDO> selectList(Demo02CategoryListReqVO reqVO) {
return selectList(new LambdaQueryWrapperX<Demo02CategoryDO>()
.likeIfPresent(Demo02CategoryDO::getName, reqVO.getName())
.eqIfPresent(Demo02CategoryDO::getParentId, reqVO.getParentId())
.betweenIfPresent(Demo02CategoryDO::getCreateTime, reqVO.getCreateTime())
.orderByDesc(Demo02CategoryDO::getId));
}
default Demo02CategoryDO selectByParentIdAndName(Long parentId, String name) {
return selectOne(Demo02CategoryDO::getParentId, parentId, Demo02CategoryDO::getName, name);
}
default Long selectCountByParentId(Long parentId) {
return selectCount(Demo02CategoryDO::getParentId, parentId);
}
}

View File

@ -0,0 +1,55 @@
package cn.iocoder.yudao.module.infra.service.demo.demo02;
import java.util.*;
import javax.validation.*;
import cn.iocoder.yudao.module.infra.controller.admin.demo.demo02.vo.Demo02CategoryListReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.demo.demo02.vo.Demo02CategorySaveReqVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo02.Demo02CategoryDO;
/**
* 示例分类 Service 接口
*
* @author 芋道源码
*/
public interface Demo02CategoryService {
/**
* 创建示例分类
*
* @param createReqVO 创建信息
* @return 编号
*/
Long createDemo02Category(@Valid Demo02CategorySaveReqVO createReqVO);
/**
* 更新示例分类
*
* @param updateReqVO 更新信息
*/
void updateDemo02Category(@Valid Demo02CategorySaveReqVO updateReqVO);
/**
* 删除示例分类
*
* @param id 编号
*/
void deleteDemo02Category(Long id);
/**
* 获得示例分类
*
* @param id 编号
* @return 示例分类
*/
Demo02CategoryDO getDemo02Category(Long id);
/**
* 获得示例分类列表
*
* @param listReqVO 查询条件
* @return 示例分类列表
*/
List<Demo02CategoryDO> getDemo02CategoryList(Demo02CategoryListReqVO listReqVO);
}

View File

@ -0,0 +1,134 @@
package cn.iocoder.yudao.module.infra.service.demo.demo02;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.infra.controller.admin.demo.demo02.vo.Demo02CategoryListReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.demo.demo02.vo.Demo02CategorySaveReqVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.demo.demo02.Demo02CategoryDO;
import cn.iocoder.yudao.module.infra.dal.mysql.demo02.Demo02CategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
/**
* 示例分类 Service 实现类
*
* @author 芋道源码
*/
@Service
@Validated
public class Demo02CategoryServiceImpl implements Demo02CategoryService {
@Resource
private Demo02CategoryMapper demo02CategoryMapper;
@Override
public Long createDemo02Category(Demo02CategorySaveReqVO createReqVO) {
// 校验父级编号的有效性
validateParentDemo02Category(null, createReqVO.getParentId());
// 校验名字的唯一性
validateDemo02CategoryNameUnique(null, createReqVO.getParentId(), createReqVO.getName());
// 插入
Demo02CategoryDO demo02Category = BeanUtils.toBean(createReqVO, Demo02CategoryDO.class);
demo02CategoryMapper.insert(demo02Category);
// 返回
return demo02Category.getId();
}
@Override
public void updateDemo02Category(Demo02CategorySaveReqVO updateReqVO) {
// 校验存在
validateDemo02CategoryExists(updateReqVO.getId());
// 校验父级编号的有效性
validateParentDemo02Category(updateReqVO.getId(), updateReqVO.getParentId());
// 校验名字的唯一性
validateDemo02CategoryNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getName());
// 更新
Demo02CategoryDO updateObj = BeanUtils.toBean(updateReqVO, Demo02CategoryDO.class);
demo02CategoryMapper.updateById(updateObj);
}
@Override
public void deleteDemo02Category(Long id) {
// 校验存在
validateDemo02CategoryExists(id);
// 校验是否有子示例分类
if (demo02CategoryMapper.selectCountByParentId(id) > 0) {
throw exception(DEMO02_CATEGORY_EXITS_CHILDREN);
}
// 删除
demo02CategoryMapper.deleteById(id);
}
private void validateDemo02CategoryExists(Long id) {
if (demo02CategoryMapper.selectById(id) == null) {
throw exception(DEMO02_CATEGORY_NOT_EXISTS);
}
}
private void validateParentDemo02Category(Long id, Long parentId) {
if (parentId == null || Demo02CategoryDO.PARENT_ID_ROOT.equals(parentId)) {
return;
}
// 1. 不能设置自己为父示例分类
if (Objects.equals(id, parentId)) {
throw exception(DEMO02_CATEGORY_PARENT_ERROR);
}
// 2. 父示例分类不存在
Demo02CategoryDO parentDemo02Category = demo02CategoryMapper.selectById(parentId);
if (parentDemo02Category == null) {
throw exception(DEMO02_CATEGORY_PARENT_NOT_EXITS);
}
// 3. 递归校验父示例分类如果父示例分类是自己的子示例分类则报错避免形成环路
if (id == null) { // id 为空说明新增不需要考虑环路
return;
}
for (int i = 0; i < Short.MAX_VALUE; i++) {
// 3.1 校验环路
parentId = parentDemo02Category.getParentId();
if (Objects.equals(id, parentId)) {
throw exception(DEMO02_CATEGORY_PARENT_IS_CHILD);
}
// 3.2 继续递归下一级父示例分类
if (parentId == null || Demo02CategoryDO.PARENT_ID_ROOT.equals(parentId)) {
break;
}
parentDemo02Category = demo02CategoryMapper.selectById(parentId);
if (parentDemo02Category == null) {
break;
}
}
}
private void validateDemo02CategoryNameUnique(Long id, Long parentId, String name) {
Demo02CategoryDO demo02Category = demo02CategoryMapper.selectByParentIdAndName(parentId, name);
if (demo02Category == null) {
return;
}
// 如果 id 为空说明不用比较是否为相同 id 的示例分类
if (id == null) {
throw exception(DEMO02_CATEGORY_NAME_DUPLICATE);
}
if (!Objects.equals(demo02Category.getId(), id)) {
throw exception(DEMO02_CATEGORY_NAME_DUPLICATE);
}
}
@Override
public Demo02CategoryDO getDemo02Category(Long id) {
return demo02CategoryMapper.selectById(id);
}
@Override
public List<Demo02CategoryDO> getDemo02CategoryList(Demo02CategoryListReqVO listReqVO) {
return demo02CategoryMapper.selectList(listReqVO);
}
}

View File

@ -1 +0,0 @@
package cn.iocoder.yudao.module.infra.service.demo;

View File

@ -7,4 +7,5 @@ ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_EXITS_CHILDREN = new Er
ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_PARENT_NOT_EXITS = new ErrorCode(TODO 补充编号,"父级${table.classComment}不存在");
ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_PARENT_ERROR = new ErrorCode(TODO 补充编号, "不能设置自己为父${table.classComment}");
ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_${treeNameColumn_javaField_underlineCase.toUpperCase()}_DUPLICATE = new ErrorCode(TODO 补充编号, "已经存在该${treeNameColumn.columnComment}的${table.classComment}");
ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_PARENT_IS_CHILD = new ErrorCode(TODO 补充编号, "不能设置自己的子${table.className}为父${table.className}");
#end

View File

@ -207,7 +207,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
if (id == null) {
throw exception(${simpleClassName_underlineCase.toUpperCase()}_${treeNameColumn_javaField_underlineCase.toUpperCase()}_DUPLICATE);
}
if (ObjectUtil.notEqual(${classNameVar}.getId(), id)) {
if (!Objects.equals(${classNameVar}.getId(), id)) {
throw exception(${simpleClassName_underlineCase.toUpperCase()}_${treeNameColumn_javaField_underlineCase.toUpperCase()}_DUPLICATE);
}
}

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.infra.dal.mysql.demo02.Demo02CategoryMapper">
<!--
一般情况下,尽可能使用 Mapper 进行 CRUD 增删改查即可。
无法满足的场景,例如说多表关联查询,才使用 XML 编写 SQL。
代码生成器暂时只生成 Mapper XML 文件本身,更多推荐 MybatisX 快速开发插件来生成查询。
文档可见https://www.iocoder.cn/MyBatis/x-plugins/
-->
</mapper>