mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-23 07:25:07 +08:00
整理 vm 模板
提交 test 生成的示例
This commit is contained in:
@ -1 +0,0 @@
|
||||
package cn.iocoder.dashboard.modules.infra.convert;
|
@ -0,0 +1 @@
|
||||
package cn.iocoder.dashboard.modules.system.controller.test;
|
@ -0,0 +1,30 @@
|
||||
package cn.iocoder.dashboard.modules.system.controller.test.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 字典类型 Base VO,提供给添加、修改、详细的子 VO 使用
|
||||
* 如果子 VO 存在差异的字段,请不要添加到这里,影响 Swagger 文档生成
|
||||
*/
|
||||
@Data
|
||||
public class SysTestDemoBaseVO {
|
||||
|
||||
@ApiModelProperty(value = "字典名称", required = true, example = "性别额")
|
||||
@NotNull(message = "字典名称不能为空")
|
||||
private String name;
|
||||
|
||||
@ApiModelProperty(value = "字典类型", required = true, example = "sys_sex")
|
||||
@NotNull(message = "字典类型不能为空")
|
||||
private String dictType;
|
||||
|
||||
@ApiModelProperty(value = "状态", required = true, example = "1")
|
||||
@NotNull(message = "状态不能为空")
|
||||
private Integer status;
|
||||
|
||||
@ApiModelProperty(value = "备注", example = "我是备注")
|
||||
private String remark;
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package cn.iocoder.dashboard.modules.system.controller.test.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
@ApiModel("字典类型创建 Request VO")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
public class SysTestDemoCreateReqVO extends SysTestDemoBaseVO {
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package cn.iocoder.dashboard.modules.system.controller.test.vo;
|
||||
|
||||
import cn.iocoder.dashboard.common.pojo.PageParam;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
||||
|
||||
@ApiModel("字典类型分页 Request VO")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
public class SysTestDemoPageReqVO extends PageParam {
|
||||
|
||||
@ApiModelProperty(value = "字典名称", example = "性别额")
|
||||
private String name;
|
||||
|
||||
@ApiModelProperty(value = "字典类型", example = "sys_sex")
|
||||
private String dictType;
|
||||
|
||||
@ApiModelProperty(value = "状态", example = "1")
|
||||
private Integer status;
|
||||
|
||||
@ApiModelProperty(value = "备注", example = "我是备注")
|
||||
private String remark;
|
||||
|
||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
||||
@ApiModelProperty(value = "开始创建时间")
|
||||
private Date beginCreateTime;
|
||||
|
||||
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
|
||||
@ApiModelProperty(value = "结束创建时间")
|
||||
private Date endCreateTime;
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package cn.iocoder.dashboard.modules.system.controller.test.vo;
|
||||
|
||||
import lombok.*;
|
||||
import java.util.*;
|
||||
import io.swagger.annotations.*;
|
||||
|
||||
@ApiModel("字典类型 Response VO")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
public class SysTestDemoRespVO extends SysTestDemoBaseVO {
|
||||
|
||||
@ApiModelProperty(value = "字典主键", required = true, example = "1")
|
||||
private Long id;
|
||||
|
||||
@ApiModelProperty(value = "创建时间", required = true)
|
||||
private Date createTime;
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package cn.iocoder.dashboard.modules.system.controller.test.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@ApiModel("字典类型更新 Request VO")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
public class SysTestDemoUpdateReqVO extends SysTestDemoBaseVO {
|
||||
|
||||
@ApiModelProperty(value = "字典主键", required = true, example = "1")
|
||||
@NotNull(message = "字典主键不能为空")
|
||||
private Long id;
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package cn.iocoder.dashboard.modules.system.dal.mysql.dao.test;
|
||||
|
||||
import cn.iocoder.dashboard.common.pojo.PageResult;
|
||||
import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX;
|
||||
import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.test.SysTestDemoDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoPageReqVO;
|
||||
|
||||
/**
|
||||
* 字典类型 Mapper
|
||||
*
|
||||
* @author 芋艿
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysTestDemoMapper extends BaseMapperX<SysTestDemoDO> {
|
||||
|
||||
default PageResult<SysTestDemoDO> selectPage(SysTestDemoPageReqVO reqVO) {
|
||||
return selectPage(reqVO, new QueryWrapperX<SysTestDemoDO>()
|
||||
.likeIfPresent("name", reqVO.getName())
|
||||
.eqIfPresent("dict_type", reqVO.getDictType())
|
||||
.eqIfPresent("status", reqVO.getStatus())
|
||||
.eqIfPresent("remark", reqVO.getRemark())
|
||||
.betweenIfPresent("create_time", reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.test;
|
||||
|
||||
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* 字典类型 DO
|
||||
*
|
||||
* @author 芋艿
|
||||
*/
|
||||
@TableName("sys_test_demo")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SysTestDemoDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 字典主键
|
||||
*/
|
||||
@TableId
|
||||
private Long id;
|
||||
/**
|
||||
* 字典名称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 字典类型
|
||||
*/
|
||||
private String dictType;
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
private String remark;
|
||||
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package cn.iocoder.dashboard.modules.system.service.test;
|
||||
|
||||
import cn.iocoder.dashboard.common.pojo.PageResult;
|
||||
import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoCreateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoPageReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoUpdateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.test.SysTestDemoDO;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 字典类型 Service 接口
|
||||
*
|
||||
* @author 芋艿
|
||||
*/
|
||||
public interface SysTestDemoService {
|
||||
|
||||
/**
|
||||
* 创建字典类型
|
||||
*
|
||||
* @param createReqVO 创建信息
|
||||
* @return 编号
|
||||
*/
|
||||
Long createTestDemo(@Valid SysTestDemoCreateReqVO createReqVO);
|
||||
|
||||
/**
|
||||
* 更新字典类型
|
||||
*
|
||||
* @param updateReqVO 更新信息
|
||||
*/
|
||||
void updateTestDemo(@Valid SysTestDemoUpdateReqVO updateReqVO);
|
||||
|
||||
/**
|
||||
* 删除字典类型
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
void deleteTestDemo(Long id);
|
||||
|
||||
/**
|
||||
* 获得字典类型
|
||||
*
|
||||
* @param id 编号
|
||||
* @return 字典类型
|
||||
*/
|
||||
SysTestDemoDO getTestDemo(Long id);
|
||||
|
||||
/**
|
||||
* 获得字典类型列表
|
||||
*
|
||||
* @param ids 编号
|
||||
* @return 字典类型列表
|
||||
*/
|
||||
List<SysTestDemoDO> getTestDemoList(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 获得字典类型分页
|
||||
*
|
||||
* @param pageReqVO 分页查询
|
||||
* @return 字典类型分页
|
||||
*/
|
||||
PageResult<SysTestDemoDO> getTestDemoPage(SysTestDemoPageReqVO pageReqVO);
|
||||
|
||||
}
|
@ -17,6 +17,8 @@ import org.springframework.stereotype.Component;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
|
||||
import static cn.hutool.core.text.CharSequenceUtil.*;
|
||||
|
||||
/**
|
||||
* 代码生成器的 Builder,负责:
|
||||
* 1. 将数据库的表 {@link ToolInformationSchemaTableDO} 定义,构建成 {@link ToolCodegenTableDO}
|
||||
@ -25,6 +27,18 @@ import java.util.*;
|
||||
@Component
|
||||
public class ToolCodegenBuilder {
|
||||
|
||||
/**
|
||||
* Module 名字的映射 TODO 后续梳理到配置类
|
||||
*
|
||||
* key:模块的完整名
|
||||
* value:模块的缩写名
|
||||
*/
|
||||
private static final Map<String, String> moduleNames = MapUtil.<String, String>builder()
|
||||
.put("system", "sys")
|
||||
.put("infra", "inf")
|
||||
.put("tool", "tool")
|
||||
.build();
|
||||
|
||||
/**
|
||||
* 字段名与 {@link ToolCodegenColumnListConditionEnum} 的默认映射
|
||||
* 注意,字段的匹配以后缀的方式
|
||||
@ -93,9 +107,9 @@ public class ToolCodegenBuilder {
|
||||
CREATE_OPERATION_EXCLUDE_COLUMN.addAll(BASE_DO_FIELDS);
|
||||
UPDATE_OPERATION_EXCLUDE_COLUMN.addAll(BASE_DO_FIELDS);
|
||||
LIST_OPERATION_EXCLUDE_COLUMN.addAll(BASE_DO_FIELDS);
|
||||
LIST_OPERATION_EXCLUDE_COLUMN.remove("create_time"); // 创建时间,还是可能需要传递的
|
||||
LIST_OPERATION_EXCLUDE_COLUMN.remove("createTime"); // 创建时间,还是可能需要传递的
|
||||
LIST_OPERATION_RESULT_EXCLUDE_COLUMN.addAll(BASE_DO_FIELDS);
|
||||
LIST_OPERATION_RESULT_EXCLUDE_COLUMN.remove("create_time"); // 创建时间,还是需要返回的
|
||||
LIST_OPERATION_RESULT_EXCLUDE_COLUMN.remove("createTime"); // 创建时间,还是需要返回的
|
||||
}
|
||||
|
||||
public ToolCodegenTableDO buildTable(ToolInformationSchemaTableDO schemaTable) {
|
||||
@ -110,13 +124,12 @@ public class ToolCodegenBuilder {
|
||||
* @param table 表定义
|
||||
*/
|
||||
private void initTableDefault(ToolCodegenTableDO table) {
|
||||
table.setModuleName(StrUtil.subBefore(table.getTableName(),
|
||||
'_', false)); // 第一个 _ 前缀的前面,作为 module 名字
|
||||
table.setBusinessName(StrUtil.subAfter(table.getTableName(),
|
||||
'_', false)); // 第一个 _ 前缀的后面,作为 module 名字
|
||||
table.setBusinessName(StrUtil.toCamelCase(table.getBusinessName())); // 可能存在多个 _ 的情况,转换成驼峰
|
||||
table.setClassName(StrUtil.upperFirst(StrUtil.toCamelCase(table.getTableName()))); // 驼峰 + 首字母大写
|
||||
table.setClassComment(StrUtil.subBefore(table.getTableComment(), // 去除结尾的表,作为类描述
|
||||
table.setModuleName(getFullModuleName(StrUtil.subBefore(table.getTableName(),
|
||||
'_', false))); // 第一个 _ 前缀的前面,作为 module 名字
|
||||
table.setBusinessName(toCamelCase(subAfter(table.getTableName(),
|
||||
'_', false))); // 第一步,第一个 _ 前缀的后面,作为 module 名字; 第二步,可能存在多个 _ 的情况,转换成驼峰
|
||||
table.setClassName(upperFirst(toCamelCase(table.getTableName()))); // 驼峰 + 首字母大写
|
||||
table.setClassComment(subBefore(table.getTableComment(), // 去除结尾的表,作为类描述
|
||||
'表', true));
|
||||
table.setAuthor("芋艿"); // TODO 稍后改成创建人
|
||||
table.setTemplateType(ToolCodegenTemplateTypeEnum.CRUD.getType());
|
||||
@ -144,7 +157,7 @@ public class ToolCodegenBuilder {
|
||||
|
||||
private void processColumnJava(ToolCodegenColumnDO column) {
|
||||
// 处理 javaField 字段
|
||||
column.setJavaField(StrUtil.toCamelCase(column.getColumnName()));
|
||||
column.setJavaField(toCamelCase(column.getColumnName()));
|
||||
// 处理 dictType 字段,暂无
|
||||
// 处理 javaType 字段
|
||||
String dbType = StrUtil.subBefore(column.getColumnType(), '(', false);
|
||||
@ -194,4 +207,27 @@ public class ToolCodegenBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得模块的缩略名
|
||||
*
|
||||
* @param fullModuleName 模块的完整名
|
||||
* @return 缩略名
|
||||
*/
|
||||
public String getSimpleModuleName(String fullModuleName) {
|
||||
return moduleNames.getOrDefault(fullModuleName, fullModuleName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得模块的完整名
|
||||
*
|
||||
* @param shortModuleName 模块的缩略名
|
||||
* @return 完整名
|
||||
*/
|
||||
public String getFullModuleName(String shortModuleName) {
|
||||
return moduleNames.entrySet().stream()
|
||||
.filter(entry -> entry.getValue().equals(shortModuleName)) // 匹配
|
||||
.findFirst().map(Map.Entry::getKey) // 返回 key
|
||||
.orElse(shortModuleName); // 兜底返回 shortModuleName
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import cn.iocoder.dashboard.util.collection.CollectionUtils;
|
||||
import cn.iocoder.dashboard.util.date.DateUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -31,6 +32,9 @@ import static cn.hutool.core.text.CharSequenceUtil.*;
|
||||
@Component
|
||||
public class ToolCodegenEngine {
|
||||
|
||||
@Resource
|
||||
private ToolCodegenBuilder codegenBuilder;
|
||||
|
||||
/**
|
||||
* 模板引擎,由 hutool 实现
|
||||
*/
|
||||
@ -69,9 +73,15 @@ public class ToolCodegenEngine {
|
||||
Map<String, Object> bindingMap = new HashMap<>(globalBindingMap);
|
||||
bindingMap.put("table", table);
|
||||
bindingMap.put("columns", columns);
|
||||
bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, ToolCodegenColumnDO::getPrimaryKey));
|
||||
bindingMap.put("simpleClassName", upperFirst(toCamelCase(subAfter( // 去掉第一个驼峰,例如说 SysUser 去掉后是 User
|
||||
toUnderlineCase(table.getClassName()), '_', false))));
|
||||
bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, ToolCodegenColumnDO::getPrimaryKey)); // 主键字段
|
||||
String simpleModuleName = codegenBuilder.getSimpleModuleName(table.getModuleName());
|
||||
bindingMap.put("simpleModuleName", simpleModuleName); // 将 system 转成 sys
|
||||
// className 相关
|
||||
String simpleClassName = subAfter(table.getClassName(), upperFirst(simpleModuleName)
|
||||
, false); // 将 TestDictType 转换成 DictType. 因为在 create 等方法后,不需要带上 Test 前缀
|
||||
bindingMap.put("simpleClassName", simpleClassName);
|
||||
bindingMap.put("simpleClassName_underlineCase", toUnderlineCase(simpleClassName)); // 将 DictType 转换成 dict_type
|
||||
bindingMap.put("classNameVar", lowerFirst(simpleClassName)); // 将 DictType 转换成 dictType,用于变量
|
||||
// 执行生成
|
||||
// String result = templateEngine.getTemplate("codegen/dal/do.vm").render(bindingMap);
|
||||
// String result = templateEngine.getTemplate("codegen/dal/mapper.vm").render(bindingMap);
|
||||
@ -80,7 +90,10 @@ public class ToolCodegenEngine {
|
||||
// String result = templateEngine.getTemplate("codegen/controller/vo/createReqVO.vm").render(bindingMap);
|
||||
// String result = templateEngine.getTemplate("codegen/controller/vo/updateReqVO.vm").render(bindingMap);
|
||||
// String result = templateEngine.getTemplate("codegen/controller/vo/respVO.vm").render(bindingMap);
|
||||
// String result = templateEngine.getTemplate("codegen/convert/convert.vm").render(bindingMap);
|
||||
// String result = templateEngine.getTemplate("codegen/enums/errorcode.vm").render(bindingMap);
|
||||
String result = templateEngine.getTemplate("codegen/service/service.vm").render(bindingMap);
|
||||
// String result = templateEngine.getTemplate("codegen/service/serviceImpl.vm").render(bindingMap);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user