确认

  • [x] 我使用的版本是最新版, 并且使用插件确认过项目里无依赖版本冲突
  • [x] 我已经在 issue 中搜索过, 确认问题没有被提出过
  • [x] 我已经修改标题, 将标题中的 描述 替换为遇到的问题(不得删除 描述 前面的部分)

当前程序版本

3.5.12

问题描述

通过FastAutoGenerator的templateConfig来引用模板但无效; 1. 希望controller使用我的模板生成,其他的service使用默认 2. builder.disable();(期望关闭所有的默认生成,但没有生效)

<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.3</version>
        </dependency>
 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.12</version>
        </dependency>

代码

package com.demo;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.sql.Types;
import java.util.Collections;

/**
 * @author Henry
 * @time 2025/7/22
 * @see CodeGenerator
 */
public class CodeGenerator {
    public static void main(String[] args) {
        // 基本信息
        String parent = "com.demo" ;
        FastAutoGenerator.create("jdbc:mysql://XX", "XXX", "XXX")
                // 全局配置
                .globalConfig(builder -> {
                    builder.author("XXX")
                            // 代码生成后不打开文件夹
                            .disableOpenDir()
                            // 时间日期格式,默认为 "yyyy-MM-dd"
                            .commentDate("yyyy-MM-dd HH:mm:ss")
                            // 文件生成位置
                            .outputDir(System.getProperty("user.dir") + "\\src\\main\\java");
                })
                // 数据库配置
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);
                }))
                // 包配置
                .packageConfig(builder -> {
                    // 包路径
                    builder.parent(parent)
                            // entity 命名(controller/service/mapper 一样适用)
//                            .entity("pojo")
                            // 路径配置信息
                            .pathInfo(Collections.singletonMap(OutputFile.xml,
                                    System.getProperty("user.dir") + "src\\main\\resources\\mapper"));

                })


                // 策略配置
                .strategyConfig(builder -> {
                    // 精准添加生成代码的表(也可以进行模糊匹配 like/模糊排除表 noLike/精准排除 addExclude)
                    builder.addInclude("user")
                            // 过滤表前缀
                            .addTablePrefix("aq_")
                            // 过滤表后缀
//                            .addTableSuffix("_end")
                            // 实体类配置
                            .entityBuilder()
                            // 开启 lombok 生成 @Getter@Setter
                            .enableLombok()
                            .enableFileOverride()
                            // 字段注解
                            .enableTableFieldAnnotation()
                            .idType(IdType.AUTO)
                            // 表字段自动填充
                            .addTableFills(new Column("create_time", FieldFill.INSERT))
                            .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))
                            .build()

                            // 控制器配置
                            .controllerBuilder()
                            .enableFileOverride()
                            .enableRestStyle()

                            .mapperBuilder()
                            .enableFileOverride()
                            // 开启 @Mapper ,建议包扫描模式
//                            .enableMapperAnnotation()
                            .serviceBuilder()
                            .enableFileOverride()
                            .build();

                })
                // 使用默认 Velocity 引擎模板生成代码
                .templateConfig(builder -> {
                    builder.disable();
                    builder.controller("/templates/custom/controller.java");
                })
                .templateEngine(new VelocityTemplateEngine())
                .execute();
    }
}

模板位置

D:\XX\src\main\resources\templates\custom\controller.java.vm

详细堆栈日志

18:40:50.236 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================准备生成文件...==========================
18:40:51.913 [main] WARN org.apache.velocity.deprecation - configuration key 'file.resource.loader.unicode' has been deprecated in favor of 'resource.loader.file.unicode'
18:40:51.914 [main] WARN org.apache.velocity.deprecation - configuration key 'file.resource.loader.class' has been deprecated in favor of 'resource.loader.file.class'
18:40:52.109 [main] DEBUG org.apache.velocity - Initializing Velocity, Calling init()...
18:40:52.109 [main] DEBUG org.apache.velocity - Starting Apache Velocity v2.3
18:40:52.111 [main] DEBUG org.apache.velocity - Default Properties resource: org/apache/velocity/runtime/defaults/velocity.properties
18:40:52.123 [main] DEBUG org.apache.velocity - ResourceLoader instantiated: org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
18:40:52.124 [main] DEBUG org.apache.velocity - initialized (class org.apache.velocity.runtime.resource.ResourceCacheImpl) with class java.util.Collections$SynchronizedMap cache map.
18:40:52.127 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Stop
18:40:52.128 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Define
18:40:52.128 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Break
18:40:52.129 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Evaluate
18:40:52.130 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Macro
18:40:52.131 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Parse
18:40:52.132 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Include
18:40:52.132 [main] DEBUG org.apache.velocity - Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
18:40:52.148 [main] DEBUG org.apache.velocity.parser - Created '20' parsers.
18:40:52.162 [main] DEBUG org.apache.velocity.macro - "velocimacro.library.path" is not set. Trying default library: velocimacros.vtl
18:40:52.164 [main] DEBUG org.apache.velocity.loader.file - Could not load resource 'velocimacros.vtl' from ResourceLoader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
18:40:52.164 [main] DEBUG org.apache.velocity.macro - Default library velocimacros.vtl not found. Trying old default library: VM_global_library.vm
18:40:52.165 [main] DEBUG org.apache.velocity.loader.file - Could not load resource 'VM_global_library.vm' from ResourceLoader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
18:40:52.166 [main] DEBUG org.apache.velocity.macro - Old default library VM_global_library.vm not found.
18:40:52.166 [main] DEBUG org.apache.velocity.macro - allowInline = true: VMs can be defined inline in templates
18:40:52.166 [main] DEBUG org.apache.velocity.macro - allowInlineToOverride = false: VMs defined inline may NOT replace previous VM definitions
18:40:52.166 [main] DEBUG org.apache.velocity.macro - allowInlineLocal = false: VMs defined inline will be global in scope if allowed.
18:40:52.166 [main] DEBUG org.apache.velocity.macro - autoload off: VM system will not automatically reload global library macros
18:40:52.192 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/entity.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
18:40:52.204 [main] DEBUG com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine - 模板:/templates/entity.java.vm;  文件:XXX\src\main\java\com\test\\entity\User.java
18:40:52.208 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/mapper.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
18:40:52.213 [main] DEBUG com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine - 模板:/templates/mapper.java.vm;  文件:XXX\src\main\java\com\test\\mapper\UserMapper.java
18:40:52.216 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/mapper.xml.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
18:40:52.217 [main] DEBUG com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine - 模板:/templates/mapper.xml.vm;  文件:XXX\src\main\resources\mapper\UserMapper.xml
18:40:52.218 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/service.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
18:40:52.219 [main] DEBUG com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine - 模板:/templates/service.java.vm;  文件:XXX\src\main\java\com\test\\service\IUserService.java
18:40:52.221 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/serviceImpl.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
18:40:52.222 [main] DEBUG com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine - 模板:/templates/serviceImpl.java.vm;  文件:XXX\src\main\java\com\test\\service\impl\UserServiceImpl.java
18:40:52.225 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/controller.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
18:40:52.226 [main] DEBUG com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine - 模板:/templates/controller.java.vm;  文件:XXX\src\main\java\com\test\\controller\UserController.java
18:40:52.227 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================文件生成完成!!!==========================

Comment From: tuhang

3.5.6后templateConfig的配置项合并到StrategyConfig 官网文档的全局参数可查