Use of the latest code generator (3.5.1 +) from mybatis plus

1. Import dependency:

<!--mybatis-plus Code generator -->
<!--velocity Template-->
<!--freemarker Template-->

Explain why two template dependencies are cited:

🐰 Because the plus code generator needs a template engine, choose either velocity or freemaker. Velocity is used by default in the generator and depends on your choice.

2. Write a constructor class

Just create a class: just have a psvm like the startup class that can run

public class PracticeApplication {

    public static void main(String[] args) {
        Code generator....;


The next step is to write the generation logic. It's very simple. See the official website:

This is the latest version. The overall structure is like this. Let's take out my code generator and introduce what they are used for (actually on the official website). The complete code is at the end:

1. Global configuration (GlobalConfig)

  • The create method needs to be passed in, including database address, user name and password; It will automatically build DataSourceConfig based on these three parameters in the background without writing it ourselves, as shown in the figure:

  • The latest version of the generator uses lambda expressions, reactive programming, and just click. It's very convenient to write

  • Author specifies the author

  • outputDir, which specifies where the generated file is output

  • enableSwagger supports swagge (very nice, remember to rely on swagger)

  • commentDate time format

  • fileOveride overwrites previously generated files

    The effect of globalConfig is shown in the figure below:

2. Package configuration (PackageConfig)

This is to configure which packages are generated:

Note: the method of configuring the xml package is called mapperXml on the official website, while the method in the actual code is called xml()

3. Policy configuration (StrategyConfig)

addInclude() specifies which tables to generate code for. There are several overloads:

  • Table name of a String
  • Any number of table names (variable length parameters): "user", "user1",... Such as
  • List list

The so-called policy configuration is to configure the policy and configuration details

It puts the service, mapper, controller and entity into the policy configuration. The previous version was in the global configuration

There is also an injection configuration, which seems not to be commonly used.....

At the end of the code:

.templateEngine(new FreemarkerTemplateEngine()) // Use the Freemarker engine template. The default is the Velocity engine template

You can specify a template engine,

execute() executes the code generator to generate code

Configure different options according to the actual situation. It is easy to complete according to the above. The suggestion is to look at the official website

design sketch:

Full code:

package com.xp.practice.generator;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UserGenerator {
    public static void main(String[] args) {
        List<String> tables = new ArrayList<>();

                .globalConfig(builder -> {
          "Xiang Pei")               //author
                            .outputDir(System.getProperty("user.dir")+"\\src\\main\\java")    //Output path (write to java directory)
                            .enableSwagger()           //Open swagger
                            .fileOverride();            //Open files generated before overwriting

                .packageConfig(builder -> {
                .strategyConfig(builder -> {
                .templateEngine(new FreemarkerTemplateEngine()) // Use the Freemarker engine template. The default is the Velocity engine template

Tags: Java Mybatis mybatis-plus

Posted on Wed, 06 Oct 2021 12:24:51 -0400 by paulnaj