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

Posted by Thunderfunk on Thu, 03 Mar 2022 09:19:01 +0100

1. Introduce 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

Create a random 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: plus – code generator

This is the latest version. The overall structure is like this. Let's take out my code generator and introduce their respective uses (actually available on the official website). The complete code is at the end:

1. Global configuration (GlobalConfig)

  • The create method needs to pass in the database address (if your MySQL version is 8, you must add the time zone after the database address, such as serverTimezone=Asia/Shanghai), user name and password; It will automatically build the DataSourceConfig based on these three parameters in the background without having to write it ourselves, as shown in the figure:

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

  • Author specifies the author

  • outputDir, which specifies where to output the generated file

  • 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 xml package is mapperXml on the official website, while the method in the actual code is 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()) // Freemarker engine template is used. The default is Velocity engine template

You can specify a template engine,

execute() executes the code generator and generates code

Configure different options according to the actual situation, and it is easy to complete according to the above. The suggestion is to look at the official website plus – code generator

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 -> {
                            .enableBaseResultMap()  //Generate general resultMap
                .templateEngine(new FreemarkerTemplateEngine()) // Freemarker engine template is used. The default is Velocity engine template

Topics: Javascript Front-end TypeScript Vue.js html