摸鱼专属!!星球后端模板项目代码生成器增强,可一次性生成多条数据、自动生成dto,vo属性

想必大家在使用星球后端模板代码跟我一样,每次创建dto、vo类都要做重复且繁琐的步骤,消耗了很多无意义的时间。

刚好趁着鱼皮更新了后端模板项目,增加了代码生成器的代码,让我们可以更好的摸鱼。我也把代码拉到本地尝试使用这个代码生成器,节省了好些时间,不用像之前一样复制替换了。但是每次重新写,创建dto、vo让我摸鱼并没有那么顺利,为了以后可以更愉快的摸鱼,于是就有了增强版代码生成器。可一次性生成多条数据、自动生成dto,vo属性、我只需要根据需求删除字段就可以了,不用复制来复制去了,又能愉快的摸鱼了!!

现在只需简单配置我们要生成的数据类,一键启动!!,就可以生成我们需要的代码!!

https://codecopy.cn/post/evj6p1

/** * 用法:追加process(数据类.class, "数据别名"); */ public static void main(String[] args) { // 代码生成处理器 new GenerateProcessor() // 生成项目路径 .packageName("com.yupi.springbootinit") // 排除字段策略 .exclusionStrategy("serialVersionUID", "isDelete") .process(Post.class, "帖子") .process(User.class, "用户"); // ..继续追加process(数据类.class, "数据别名"); }

生成的目录结构如图:

image.png

生成内容如图:

image.png

核心代码如下:

/** * 代码生成处理器 * * @author <a href="https://github.com/liyupi">程序员鱼皮</a> * @from <a href="https://www.codefather.cn">编程导航学习圈</a> */ public class GenerateProcessor { /** * 生成程序包名称 */ private String packageName; /** * 排除字段策略 */ private final List<String> exclusionStrategy = new ArrayList<>(); public GenerateProcessor packageName(String packageName) { this.packageName = packageName; return this; } public GenerateProcessor exclusionStrategy(String... exclusionFieldName) { exclusionStrategy.addAll(Arrays.asList(exclusionFieldName)); return this; } /** * 过程 * * @param clazz 生成数据类 * @param dataName 数据名称 * @return {@link GenerateProcessor } */ @SneakyThrows public GenerateProcessor process(Class<?> clazz, String dataName) { if (StringUtils.isBlank(packageName)) { throw new RuntimeException("packageName 不能为空"); } Field[] fields = clazz.getDeclaredFields(); String upperDataKey = clazz.getSimpleName(); String dataKey = StrUtil.lowerFirst(upperDataKey); // 获取属性的修饰符、类型和名称,并拼接成字符串 StringBuilder strBuilder = new StringBuilder(); for (Field field : fields) { // 排除字段 if (exclusionStrategy.stream().anyMatch(field.getName()::equals)) { continue; } int modifiers = field.getModifiers(); String modifierString = Modifier.toString(modifiers); String typeString = getTypeString(field.getGenericType()); String nameString = field.getName(); // 构建属性字符串 String fieldString = modifierString + " " + typeString + " " + nameString + ";\n\n "; strBuilder.append(fieldString); } strBuilder.deleteCharAt(strBuilder.length() - 6); // 封装生成参数 Map<String, Object> dataModel = new HashMap<>(); dataModel.put("packageName", packageName); dataModel.put("dataName", dataName); dataModel.put("dataKey", dataKey); dataModel.put("upperDataKey", upperDataKey); dataModel.put("field", strBuilder.toString()); // 生成路径默认值 String projectPath = System.getProperty("user.dir"); // 参考路径,可以自己调整下面的 outputPath String inputPath = projectPath + File.separator + "src/main/resources/templates/模板名称.java.ftl"; String outputPath = String.format("%s/generator/包名/%s类后缀.java", projectPath, upperDataKey); // 1、生成 Controller // 指定生成路径 inputPath = projectPath + File.separator + "src/main/resources/templates/TemplateController.java.ftl"; outputPath = String.format("%s/generator/controller/%sController.java", projectPath, upperDataKey); // 生成 doGenerate(inputPath, outputPath, dataModel); System.out.println("生成 Controller 成功,文件路径:" + outputPath); // 2、生成 Service 接口和实现类 // 生成 Service 接口 inputPath = projectPath + File.separator + "src/main/resources/templates/TemplateService.java.ftl"; outputPath = String.format("%s/generator/service/%sService.java", projectPath, upperDataKey); doGenerate(inputPath, outputPath, dataModel); System.out.println("生成 Service 接口成功,文件路径:" + outputPath); // 生成 Service 实现类 inputPath = projectPath + File.separator + "src/main/resources/templates/TemplateServiceImpl.java.ftl"; outputPath = String.format("%s/generator/service/impl/%sServiceImpl.java", projectPath, upperDataKey); doGenerate(inputPath, outputPath, dataModel); System.out.println("生成 Service 实现类成功,文件路径:" + outputPath); // 3、生成数据模型封装类(包括 DTO 和 VO) // 生成 DTO inputPath = projectPath + File.separator + "src/main/resources/templates/model/TemplateAddRequest.java.ftl"; outputPath = String.format("%s/generator/model/dto/%s/%sAddRequest.java", projectPath, dataKey, upperDataKey); doGenerate(inputPath, outputPath, dataModel); inputPath = projectPath + File.separator + "src/main/resources/templates/model/TemplateQueryRequest.java.ftl"; outputPath = String.format("%s/generator/model/dto/%s/%sQueryRequest.java", projectPath, dataKey, upperDataKey); doGenerate(inputPath, outputPath, dataModel); inputPath = projectPath + File.separator + "src/main/resources/templates/model/TemplateEditRequest.java.ftl"; outputPath = String.format("%s/generator/model/dto/%s/%sEditRequest.java", projectPath, dataKey, upperDataKey); doGenerate(inputPath, outputPath, dataModel); inputPath = projectPath + File.separator + "src/main/resources/templates/model/TemplateUpdateRequest.java.ftl"; outputPath = String.format("%s/generator/model/dto/%s/%sUpdateRequest.java", projectPath, dataKey, upperDataKey); doGenerate(inputPath, outputPath, dataModel); System.out.println("生成 DTO 成功,文件路径:" + outputPath); // 生成 VO inputPath = projectPath + File.separator + "src/main/resources/templates/model/TemplateVO.java.ftl"; outputPath = String.format("%s/generator/model/vo/%sVO.java", projectPath, upperDataKey); doGenerate(inputPath, outputPath, dataModel); System.out.println("生成 VO 成功,文件路径:" + outputPath); return this; } public void doGenerate(String inputPath, String outputPath, Object model) throws IOException, TemplateException { // new 出 Configuration 对象,参数为 FreeMarker 版本号 Configuration configuration = new Configuration(Configuration.VERSION_2_3_31); // 指定模板文件所在的路径 File templateDir = new File(inputPath).getParentFile(); configuration.setDirectoryForTemplateLoading(templateDir); // 设置模板文件使用的字符集 configuration.setDefaultEncoding("utf-8"); // 创建模板对象,加载指定模板 String templateName = new File(inputPath).getName(); Template template = configuration.getTemplate(templateName); // 文件不存在则创建文件和父目录 if (!FileUtil.exist(outputPath)) { FileUtil.touch(outputPath); } // 生成 Writer out = new FileWriter(outputPath); template.process(model, out); // 生成文件后别忘了关闭哦 out.close(); } /** * 获取类型字符串 * * @param type 类型 * @return {@link String } */ private String getTypeString(Type type) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type rawType = parameterizedType.getRawType(); Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); StringBuilder sb = new StringBuilder(); sb.append(getSimpleName(rawType.getTypeName())); if (actualTypeArguments.length > 0) { sb.append("<"); for (int i = 0; i < actualTypeArguments.length; i++) { sb.append(getTypeString(actualTypeArguments[i])); if (i < actualTypeArguments.length - 1) { sb.append(", "); } } sb.append(">"); } return sb.toString(); } else { return getSimpleName(type.getTypeName()); } } /** * 获取简化后的类型名称,移除类似java.lang.前缀 * * @param typeName 类型名称 * @return {@link String } */ private String getSimpleName(String typeName) { int lastDotIndex = typeName.lastIndexOf('.'); if (lastDotIndex != -1) { return typeName.substring(lastDotIndex + 1); } return typeName; } }

0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
柒木
下载 APP