基本完成代码生成器

This commit is contained in:
YunaiV
2021-02-12 21:01:45 +08:00
parent 937c51f4ec
commit 0c8f92fa95
21 changed files with 411 additions and 105 deletions

View File

@ -44,14 +44,19 @@ public class DictConvert implements Converter<Object> {
@Override
public CellData<String> convertToExcelData(Object object, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
GlobalConfiguration globalConfiguration) {
// 空时,返回空
if (object == null) {
return new CellData<>("");
}
// 使用字典格式化
SysDictTypeEnum type = getType(contentProperty);
String value = String.valueOf(object);
SysDictDataDO dictData = DictUtils.getDictDataFromCache(type.getValue(), value);
if (dictData == null) {
log.error("[convertToExcelData][type({}) 转换不了 label({})]", type, value);
return null;
return new CellData<>("");
}
// 生成 Excel 小表格
return new CellData<>(dictData.getLabel());

View File

@ -1 +0,0 @@
package cn.iocoder.dashboard.framework.excel.core;

View File

@ -42,11 +42,12 @@ public class SysMenuBaseVO {
@ApiModelProperty(value = "菜单图标", example = "/menu/list", notes = "仅菜单类型为菜单或者目录时,才需要传")
private String icon;
/**
* 组件路径
*/
@ApiModelProperty(value = "组件路径", example = "system/post/index", notes = "仅菜单类型为菜单时,才需要传")
@Size(max = 200, message = "组件路径不能超过255个字符")
private String component;
@ApiModelProperty(value = "状态", required = true, example = "1", notes = "见 SysCommonStatusEnum 枚举")
@NotNull(message = "状态不能为空")
private Integer status;
}

View File

@ -51,7 +51,6 @@ public class ToolCodegenController {
@ApiImplicitParam(name = "tableName", required = true, example = "yudao", dataTypeClass = String.class),
@ApiImplicitParam(name = "tableComment", required = true, example = "芋道", dataTypeClass = String.class)
})
// @PreAuthorize("@ss.hasPermi('tool:gen:list')") TODO 权限
public CommonResult<List<ToolSchemaTableRespVO>> getSchemaTableList(
@RequestParam(value = "tableName", required = false) String tableName,
@RequestParam(value = "tableComment", required = false) String tableComment) {

View File

@ -31,10 +31,6 @@ public class ToolCodegenTableBaseVO {
@NotNull(message = "业务名不能为空")
private String businessName;
@ApiModelProperty(value = "业务包", required = true, example = "codegen")
@NotNull(message = "业务包不能为空")
private String businessPackage;
@ApiModelProperty(value = "类名称", required = true, example = "ToolCodegenTable")
@NotNull(message = "类名称不能为空")
private String className;
@ -51,4 +47,7 @@ public class ToolCodegenTableBaseVO {
@NotNull(message = "模板类型不能为空")
private Integer templateType;
@ApiModelProperty(value = "父菜单编号", example = "1024")
private Long parentMenuId;
}

View File

@ -3,6 +3,7 @@ package cn.iocoder.dashboard.modules.tool.controller.test;
import cn.iocoder.dashboard.common.pojo.CommonResult;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.framework.excel.core.util.ExcelUtils;
import cn.iocoder.dashboard.framework.logger.operatelog.core.annotations.OperateLog;
import cn.iocoder.dashboard.modules.tool.controller.test.vo.*;
import cn.iocoder.dashboard.modules.tool.convert.test.ToolTestDemoConvert;
import cn.iocoder.dashboard.modules.tool.dal.dataobject.test.ToolTestDemoDO;
@ -10,6 +11,7 @@ import cn.iocoder.dashboard.modules.tool.service.test.ToolTestDemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@ -21,8 +23,9 @@ import java.util.Collection;
import java.util.List;
import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
import static cn.iocoder.dashboard.framework.logger.operatelog.core.enums.OperateTypeEnum.EXPORT;
@Api(tags = "字典类型")
@Api(tags = "测试示例")
@RestController
@RequestMapping("/tool/test-demo")
@Validated
@ -31,58 +34,66 @@ public class ToolTestDemoController {
@Resource
private ToolTestDemoService testDemoService;
@ApiOperation("创建字典类型")
@PostMapping("/create")
public CommonResult<Long> createTestDemo(@Valid ToolTestDemoCreateReqVO createReqVO) {
@ApiOperation("创建测试示例")
@PreAuthorize("@ss.hasPermission('tool:test-demo:create')")
public CommonResult<Long> createTestDemo(@Valid @RequestBody ToolTestDemoCreateReqVO createReqVO) {
return success(testDemoService.createTestDemo(createReqVO));
}
@ApiOperation("更新字典类型")
@PutMapping("/update")
public CommonResult<Boolean> updateTestDemo(@Valid ToolTestDemoUpdateReqVO updateReqVO) {
@ApiOperation("更新测试示例")
@PreAuthorize("@ss.hasPermission('tool:test-demo:update')")
public CommonResult<Boolean> updateTestDemo(@Valid @RequestBody ToolTestDemoUpdateReqVO updateReqVO) {
testDemoService.updateTestDemo(updateReqVO);
return success(true);
}
@ApiOperation("删除字典类型")
@DeleteMapping("/delete")
@ApiOperation("删除测试示例")
@ApiImplicitParam(name = "id", value = "编号", required = true)
@PreAuthorize("@ss.hasPermission('tool:test-demo:delete')")
public CommonResult<Boolean> deleteTestDemo(@RequestParam("id") Long id) {
testDemoService.deleteTestDemo(id);
return success(true);
}
@GetMapping("/get")
@ApiOperation("获得字典类型")
@ApiOperation("获得测试示例")
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('tool:test-demo:query')")
public CommonResult<ToolTestDemoRespVO> getTestDemo(@RequestParam("id") Long id) {
ToolTestDemoDO testDemo = testDemoService.getTestDemo(id);
return success(ToolTestDemoConvert.INSTANCE.convert(testDemo));
}
@GetMapping("/list")
@ApiOperation("获得字典类型列表")
@ApiOperation("获得测试示例列表")
@ApiImplicitParam(name = "ids", value = "编号列表", required = true, dataTypeClass = List.class)
@PreAuthorize("@ss.hasPermission('tool:test-demo:query')")
public CommonResult<List<ToolTestDemoRespVO>> getTestDemoList(@RequestParam("ids") Collection<Long> ids) {
List<ToolTestDemoDO> list = testDemoService.getTestDemoList(ids);
return success(ToolTestDemoConvert.INSTANCE.convertList(list));
}
@ApiOperation("获得字典类型分页")
@GetMapping("/page")
@ApiOperation("获得测试示例分页")
@PreAuthorize("@ss.hasPermission('tool:test-demo:query')")
public CommonResult<PageResult<ToolTestDemoRespVO>> getTestDemoPage(@Valid ToolTestDemoPageReqVO pageVO) {
PageResult<ToolTestDemoDO> pageResult = testDemoService.getTestDemoPage(pageVO);
return success(ToolTestDemoConvert.INSTANCE.convertPage(pageResult));
}
@GetMapping("/export-excel")
@ApiOperation("导出字典类型 Excel")
@ApiOperation("导出测试示例 Excel")
@PreAuthorize("@ss.hasPermission('tool:test-demo:export')")
@OperateLog(type = EXPORT)
public void exportTestDemoExcel(@Valid ToolTestDemoExportReqVO exportReqVO,
HttpServletResponse response) throws IOException {
List<ToolTestDemoDO> list = testDemoService.getTestDemoList(exportReqVO);
// 导出 Excel
List<ToolTestDemoExcelVO> datas = ToolTestDemoConvert.INSTANCE.convertList02(list);
ExcelUtils.write(response, "字典类型.xls", "数据", ToolTestDemoExcelVO.class, datas);
ExcelUtils.write(response, "测试示例.xls", "数据", ToolTestDemoExcelVO.class, datas);
}
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.dashboard.modules.tool.controller.test.vo;
import cn.iocoder.dashboard.framework.excel.core.annotations.DictFormat;
import cn.iocoder.dashboard.framework.excel.core.convert.DictConvert;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@ -9,7 +10,7 @@ import java.util.Date;
import static cn.iocoder.dashboard.modules.system.enums.dict.SysDictTypeEnum.*;
/**
* 字典类型 Excel VO
* 测试示例 Excel VO
*
* @author 芋艿
*/
@ -22,15 +23,15 @@ public class ToolTestDemoExcelVO {
@ExcelProperty("名字")
private String name;
@ExcelProperty("状态")
@ExcelProperty(value = "状态", converter = DictConvert.class)
@DictFormat(SYS_COMMON_STATUS)
private Integer status;
@ExcelProperty("类型")
@ExcelProperty(value = "类型", converter = DictConvert.class)
@DictFormat(SYS_OPERATE_TYPE)
private Integer type;
@ExcelProperty("分类")
@ExcelProperty(value = "分类", converter = DictConvert.class)
@DictFormat(INF_REDIS_TIMEOUT_TYPE)
private Integer category;

View File

@ -53,14 +53,6 @@ public class ToolCodegenTableDO extends BaseDO {
* 例如说user、permission、dict 等等
*/
private String businessName;
/**
* 业务包,自定义二级目录
*
* 例如说,我们希望将 dictType 和 dictData 归类成 dict 业务
*
* 如果不需要的情况下businessName 和 businessPackage 是等价的
*/
private String businessPackage;
/**
* 类名称(首字母大写)
*

View File

@ -128,7 +128,6 @@ public class ToolCodegenBuilder {
'_', false))); // 第一个 _ 前缀的前面,作为 module 名字
table.setBusinessName(toCamelCase(subAfter(table.getTableName(),
'_', false))); // 第一步,第一个 _ 前缀的后面,作为 module 名字; 第二步,可能存在多个 _ 的情况,转换成驼峰
table.setBusinessPackage(table.getBusinessPackage());
table.setClassName(upperFirst(toCamelCase(table.getTableName()))); // 驼峰 + 首字母大写
table.setClassComment(subBefore(table.getTableComment(), // 去除结尾的表,作为类描述
'表', true));

View File

@ -11,6 +11,7 @@ import cn.iocoder.dashboard.common.pojo.PageParam;
import cn.iocoder.dashboard.common.pojo.PageResult;
import cn.iocoder.dashboard.framework.codegen.config.CodegenProperties;
import cn.iocoder.dashboard.framework.excel.core.annotations.DictFormat;
import cn.iocoder.dashboard.framework.excel.core.convert.DictConvert;
import cn.iocoder.dashboard.framework.excel.core.util.ExcelUtils;
import cn.iocoder.dashboard.framework.logger.operatelog.core.annotations.OperateLog;
import cn.iocoder.dashboard.framework.logger.operatelog.core.enums.OperateTypeEnum;
@ -27,11 +28,9 @@ import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import static cn.hutool.core.map.MapUtil.getStr;
import static cn.hutool.core.text.CharSequenceUtil.*;
/**
@ -82,7 +81,7 @@ public class ToolCodegenEngine {
javaFilePath("service/${table.businessName}/impl/${table.className}ServiceImpl"))
// Vue
.put(vueTemplatePath("views/index.vue"),
vueFilePath("views/${table.moduleName}/${table.businessName}/index.vue"))
vueFilePath("views/${table.moduleName}/${classNameVar}/index.vue"))
.put(vueTemplatePath("api/api.js"),
vueFilePath("api/${table.moduleName}/${classNameVar}.js"))
// SQL
@ -131,6 +130,7 @@ public class ToolCodegenEngine {
globalBindingMap.put("ServiceExceptionUtilClassName", ServiceExceptionUtil.class.getName());
globalBindingMap.put("DateUtilsClassName", DateUtils.class.getName());
globalBindingMap.put("ExcelUtilsClassName", ExcelUtils.class.getName());
globalBindingMap.put("DictConvertClassName", DictConvert.class.getName());
globalBindingMap.put("OperateLogClassName", OperateLog.class.getName());
globalBindingMap.put("OperateTypeEnumClassName", OperateTypeEnum.class.getName());
}
@ -167,13 +167,18 @@ public class ToolCodegenEngine {
}
private String formatFilePath(String filePath, Map<String, Object> bindingMap) {
filePath = StrUtil.replace(filePath, "${basePackage}", ((String) bindingMap.get("basePackage")).replaceAll("\\.", "/"));
filePath = StrUtil.replace(filePath, "${basePackage}",
getStr(bindingMap, "basePackage").replaceAll("\\.", "/"));
filePath = StrUtil.replace(filePath, "${simpleModuleName_upperFirst}",
getStr(bindingMap, "simpleModuleName_upperFirst"));
filePath = StrUtil.replace(filePath, "${classNameVar}",
getStr(bindingMap, "classNameVar"));
// table 包含的字段
ToolCodegenTableDO table = (ToolCodegenTableDO) bindingMap.get("table");
filePath = StrUtil.replace(filePath, "${simpleModuleName_upperFirst}", (String) bindingMap.get("simpleModuleName_upperFirst"));
filePath = StrUtil.replace(filePath, "${table.moduleName}", table.getModuleName());
filePath = StrUtil.replace(filePath, "${table.businessName}", table.getBusinessName());
filePath = StrUtil.replace(filePath, "${table.className}", table.getClassName());
filePath = StrUtil.replace(filePath, "${classNameVar}", (String) bindingMap.get("classNameVar"));
return filePath;
}