1. Import dependencies

<! --MyBatisPlus dependency module -->
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>3.3.2 rainfall distribution on 10-12</version>
</dependency>
<! --MyBatisPlus code generator -->
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-generator</artifactId>
  <version>3.3.2 rainfall distribution on 10-12</version>
</dependency>
<! -- Freemarker template -->
<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.29</version>
</dependency>
Copy the code

2. Code generators

package com.example.demo;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * com.avatar.admin.genercode
 * Description:
 *
 * @author jack
 * @date2021/06/25 15:56 * /
public class GeneratorCodeTest {

    @Test
    public void generatorCode(a) {
        // Define the location of the output directory
        String projectPath = "/Users/Documents";
        // Define the code generator
        AutoGenerator autoGenerator = new AutoGenerator();

        // Global configuration
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(projectPath);
        // Author name
        gc.setAuthor("jack");
        // Whether to override
        gc.setFileOverride(false);
        Change the ActiveRecord feature to false if it is not required
        gc.setActiveRecord(false);
        // XML level 2 cache
        gc.setEnableCache(false);
        // XML ResultMap
        gc.setBaseResultMap(true);
        // XML columnList
        gc.setBaseColumnList(true);
        // Sets whether Service starts with I
        gc.setServiceName("%sService");
        // Set the primary key to an increment type
        gc.setIdType(IdType.AUTO);
        // Set the global configuration to the code generator
        autoGenerator.setGlobalConfig(gc);

        // Data source configuration
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        // Set the database type
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123456");
        dataSourceConfig.setUrl("JDBC: mysql: / / 127.0.0.1:3306 / erupt? useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GM T%2b8");
        // Configure the data source into the code generator
        autoGenerator.setDataSource(dataSourceConfig);

        / / package configuration
        PackageConfig packageConfig = new PackageConfig();
        // Parent path
        packageConfig.setParent("com.example.demo");
        packageConfig.setController("controller");
        packageConfig.setEntity("entity");
        packageConfig.setMapper("mapper");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        // pc.setXml("mapperXml");
        // Configure the package policy into the code generator
        autoGenerator.setPackageInfo(packageConfig);

        // Policy configuration
        StrategyConfig strategyConfig = new StrategyConfig();
        // Global uppercase name ORACLE note
        strategyConfig.setCapitalMode(true);
        // The name of the generated table
        strategyConfig.setInclude("e_upms_menu");
        // Table name generation policy
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        // Set entity classes to use Lombok format
        strategyConfig.setEntityLombokModel(true);
        // Configure the policy information into the code generator
        autoGenerator.setStrategy(strategyConfig);


        CFG. ABC can be used in the VM.
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap(a) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("abc".this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                this.setMap(map); }}; autoGenerator.setCfg(cfg); TemplateConfig templateConfig =new TemplateConfig();
        FTL /. Vm is automatically identified based on the template engine in use
        templateConfig.setService("/templates/service");
        templateConfig.setController("/templates/controller");
        templateConfig.setMapper("/templates/mapper");
        autoGenerator.setTemplate(templateConfig);
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());

        // Perform the buildautoGenerator.execute(); }}Copy the code

Refer to the official documentation of MybatisPlus

Code generation templates

Refer to the files in the mybatis- plus-Generator package for templates

service.ftl

packageThe ${package.Service};

importThe ${package.Entity}.${entity};
import ${superServiceClassPackage};

/**
* ${package.Service}
* Description:
*
* @author ${author}
* @date ${date}
*/< #if kotlin>
interface ${table.serviceName} : ${superServiceClass}<${entity}>
<#else>
public interface ${table.serviceName} extends ${superServiceClass}<${entity}> {

}
</#if>
Copy the code

mapper.ftl

packageThe ${package.Mapper};

importThe ${package.Entity}.${entity};
import ${superMapperClassPackage};


/**
* ${package.Mapper}
* Description:
*
* @author ${author}
* @date ${date}
*/< #if kotlin>
interface ${table.mapperName} : ${superMapperClass}<${entity}>
<#else>
public interface ${table.mapperName} extends ${superMapperClass}<${entity}> {

}
</#if>

Copy the code

contorller.ftl

packageThe ${package.Controller};

import org.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController; < #ifsuperControllerClassPackage?? >import${superControllerClassPackage}; < / #if>

/**
* ${package.Controller}
* Description:
*
* @author ${author}
* @date ${date}
*/
@RestController
@RequestMapping("<#if package.ModuleName?? && package.ModuleName ! = "">/${package.ModuleName}
      /<#if controllerMappingHyphenStyle?? >${controllerMappingHyphen}<#else>${table.entityPath}
      ")< #if kotlin>
class ${table.controllerName}<#ifsuperControllerClass?? > : ${superControllerClass}()</#if> < #else> < #ifsuperControllerClass?? >public class ${table.controllerName} extends ${superControllerClass} {
    <#else>
public class ${table.controllerName} {
    </#if>} < / #if>

Copy the code