当前使用版本(必填,否则不予处理)

3.5.2

该问题是如何引起的?(确定最新版也有问题再提!!!)

不知道这个是算个bug呢 ,还是由于特殊原因就要要求这样写的。

输出自定义模板文件输出路径格式与官网文档的代码不一致。

现在最新的版本代码输出的路径为 :entityName + File.separator + "%s",这样每次路径下都多出一个 entityName 的文件夹,如图: MyBatis-Plus 输出自定义模板文件输出路径格式与官网文档的代码不一致

源码实现如下: protected void outputCustomFile(@NotNull Map<String, String> customFile, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) { String entityName = tableInfo.getEntityName(); String otherPath = getPathInfo(OutputFile.other); customFile.forEach((key, value) -> { String fileName = String.format((otherPath + File.separator + entityName + File.separator + "%s"), key); outputFile(new File(fileName), objectMap, value, getConfigBuilder().getInjectionConfig().isFileOverride()); }); }

重现步骤(如果有就写完整)

  1. 自己编写VO模版
  2. InjectionConfig.Builder 配置自定义模版和输出路径 customFile.put("VO.java", "/templates/entityDTO.java.ftl");
  3. 生成模版

报错信息

Comment From: freshgeek

这个如何解决?源码里面:com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine#outputCustomFile 直接写死了 添加entityName?


    /**
     * 输出自定义模板文件
     *
     * @param customFile 自定义配置模板文件信息
     * @param tableInfo  表信息
     * @param objectMap  渲染数据
     * @since 3.5.1
     */
    protected void outputCustomFile(@NotNull Map<String, String> customFile, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
        String entityName = tableInfo.getEntityName();
        String otherPath = getPathInfo(OutputFile.other);
        customFile.forEach((key, value) -> {
            String fileName = String.format((otherPath + File.separator + entityName + File.separator + "%s"), key);
            outputFile(new File(fileName), objectMap, value);
        });
    }



Comment From: freshgeek

需要更新到3.5.3.2之后,通过formatNameFunction 来动态更改:

 .injectionConfig(
            builder -> {
              CustomFile customFile =
                  new CustomFile.Builder()
                      .fileName("Client.java")
                      .filePath(base + "client/src/main/java/com/boot")
                      .templatePath("templates/controllerClient.java.ftl")
                      .formatNameFunction(TableInfo::getControllerName)
                      .packageName("client.controller.crud")
                      .build();

              // 添加自定义接口模板
              builder
                  .customFile(customFile)
                  .customMap(MapUtil.of("clientPackage", "com.boot.client.controller.crud"));
            })