The template engine configuration is not mentioned in the official documentation, so you will need to download the template after you create the code generator using the tutorial:

  1. Open the link to download the zip package
  2. Unzip to find the file that willmybatis-plus-generator/src/main/resources/templatesCopy to your own project
  3. You can then specify the template path in the code generator, for exampleString templatePath = "/templates/mapper.xml.ftl";
package com.background.rbac.generator;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

// Demo example, execute main method console input module table name press enter automatically generated corresponding project directory
public class CodeGenerator {

    /** * 

* read the console contents *

*/
public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("Please enter" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) { returnipt; }}throw new MybatisPlusException("Please enter the correct one" + tip + "!"); } public static void main(String[] args) { // Code generator AutoGenerator mpg = new AutoGenerator(); // Global configuration GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); // Generate code path, gc.setOutputDir(projectPath + "/dbzn-bg-rbac/src/main/java"); // Author information gc.setAuthor("crazy zhang"); gc.setOpen(false); mpg.setGlobalConfig(gc); // Data source configuration DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/sys? useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("11111111"); mpg.setDataSource(dsc); / / package configuration PackageConfig pc = new PackageConfig(); pc.setModuleName(scanner("Module name")); pc.setParent("com.background"); // Set Mapper package name: / / the default generated mapper package name package. Com background. Rbac. Mapper // But I will use dao folder to save mapper, whose package name is package com.background.rbac.dao // If you want to use the default mapper configuration, you can remove it here, and also remove the configuration of the custom file path in foclist.add () pc.setMapper("dao"); mpg.setPackageInfo(pc); // Custom configuration InjectionConfig cfg = new InjectionConfig() { @Override public void initMap(a) { // to do nothing}};// If the template engine is freemarker String templatePath = "/templates/mapper.xml.ftl"; //String entityPath = "/templates/entity.java.ftl"; String mapperPath = "/templates/mapper.java.ftl"; // Customize the output configuration List<FileOutConfig> focList = new ArrayList<>(); // Custom configurations are printed first focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { // Customize the output file name. If your Entity has a prefix or suffix, note that the XML name will change accordingly!! //+ pc.getModuleName() return projectPath + "/src/main/resources/rbac/dao/" + "/" + tableInfo.getEntityName() + "Mapper"+ StringPool.DOT_XML; }});// DAO data access object focList.add(new FileOutConfig(mapperPath) { @Override public String outputFile(TableInfo tableInfo) { // Define the output file name and put the entity file entity class in the model return projectPath + "/src/main/java/com/background/rbac/dao" + "/" + tableInfo.getEntityName() + "Mapper"+ StringPool.DOT_JAVA; }}); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg);// Configure the template TemplateConfig templateConfig = new TemplateConfig(); // Configure a custom output template FTL /. Vm is automatically identified based on the template engine in use templateConfig.setEntity("templates/entity.java"); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // Policy configuration StrategyConfig strategy = new StrategyConfig(); // Table name generation policy (underline to camel name) strategy.setNaming(NamingStrategy.underline_to_camel); // Column name generation strategy (underline to camel name) strategy.setColumnNaming(NamingStrategy.underline_to_camel); // Define the entity parent class //strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity"); // Whether to enable Lombok configuration strategy.setEntityLombokModel(true); // Whether to enable RESTFUL configuration strategy.setRestControllerStyle(true); // Customize the controller parent class //strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController"); // Public fields written in the parent class strategy.setSuperEntityColumns("id"); strategy.setInclude(scanner("Table name, separated by multiple Commas").split(",")); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(newFreemarkerTemplateEngine()); mpg.execute(); }}Copy the code