当前位置: 首页> 汽车> 行情 > Mybatis-plus 创建自定义 FreeMarker 模板详细教程

Mybatis-plus 创建自定义 FreeMarker 模板详细教程

时间:2025/7/11 19:29:50来源:https://blog.csdn.net/qq_42631788/article/details/141572372 浏览次数: 0次

FreeMarker 自定义模板官方步骤

网址:https://baomidou.com/reference/new-code-generator-configuration/#%E6%A8%A1%E6%9D%BF%E9%85%8D%E7%BD%AE-templateconfig
在这里插入图片描述
(页面往最下面拉为自定义模板相关内容)

创建自定义FreeMarker 模板及使用步骤

要创建一个自定义的控制器模板并使用它生成代码,可以按照以下步骤操作:

1. 创建自定义 FreeMarker 模板

首先,在项目的 resources 目录下创建一个名为 templates 的文件夹。在该文件夹中,创建一个新的 FreeMarker 模板文件 controller.java.ftl。这是你的自定义控制器模板。

controller.java.ftl 文件中,编写你的模板代码,例如:

package ${packageName}.controller;import ${packageName}.service.${entity}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/${entity}")
public class ${entity}Controller {@Autowiredprivate ${entity}Service ${entity?uncap_first}Service;@GetMapping("/{id}")public ${entity} getById(@PathVariable Long id) {return ${entity?uncap_first}Service.getById(id);}// 其他控制器方法...}

2. 创建自定义的 Freemarker 模板引擎类

创建一个自定义的 Freemarker 模板引擎类 EnhanceFreemarkerTemplateEngine,以支持自定义模板的输出。

import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import org.jetbrains.annotations.NotNull;import java.io.File;
import java.util.Map;public class EnhanceFreemarkerTemplateEngine extends FreemarkerTemplateEngine {@Overrideprotected void outputCustomFile(@NotNull Map<String, String> customFile, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {String entityName = tableInfo.getEntityName();String otherPath = this.getPathInfo(OutputFile.other);customFile.forEach((key, value) -> {String fileName = String.format(otherPath + File.separator + entityName + "%s", key);this.outputFile(new File(fileName), objectMap, value);});}
}

3. 配置代码生成器

在你的代码生成器配置中,使用自定义模板生成控制器类。

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;public class CodeGenerator {public static void main(String[] args) {String url = "jdbc:mysql://localhost:3306/db_test";String username = "root";String password = "password";String finalProjectPath = "E://Projects/API/user-service";FastAutoGenerator.create(url, username, password).globalConfig(builder -> {builder.author("abc").enableSwagger().fileOverride().disableOpenDir().outputDir(finalProjectPath + "/src/main/java");}).packageConfig(builder -> {builder.parent("com.user_service").entity("entity").mapper("mapper").service("service").serviceImpl("service.impl").other("controller").pathInfo(Collections.singletonMap(OutputFile.xml, finalProjectPath + "/src/main/resources/mapper"));}).injectionConfig(consumer -> {Map<String, String> customFile = new HashMap<>();customFile.put("Controller.java", "/templates/controller.java.ftl");consumer.customFile(customFile);}).templateEngine(new EnhanceFreemarkerTemplateEngine()) .execute();}
}

4. 执行代码生成器

运行你的代码生成器,生成的代码将会包含根据你自定义模板生成的控制器类。

5. 生成的控制器文件示例

生成的控制器文件内容会类似于以下内容:

package com.user_service.controller;import com.user_service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/User")
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/{id}")public User getById(@PathVariable Long id) {return userService.getById(id);}// 其他控制器方法...
}

通过这种方式,你可以根据项目需求定制生成控制器类。

关键字:Mybatis-plus 创建自定义 FreeMarker 模板详细教程

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: