Use of Mybatis-plus's latest code generator (3.5.1+)

If you are Xiao Bai, this data set can help you become a bull. If you have rich development experience, this data set can help you break through the bottleneck.
2022 Web Full Video Tutorial Front End Architecture H5 vue node Applet Video+Materials+Code+Interview Questions.

1. Introduce dependencies:

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

Explain why two template dependencies are invoked:

?? Because the plus code generator requires a template engine, choose either velocity or freemarker. velocity is used by default in the generator and depends on your choice.

2. Write a constructor class

Create any class you want: just like a startup class, a psvm can run

public class PracticeApplication {

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


The next step is to write the generation logic. Simply, look at the official website: plus - Code Generator

This is the latest version, and the overall structure is like this. Next, take out my code generator, and show you what each of them is useful (actually on the official website), with the complete code at the end:

1. Global Config

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

  • The latest version of the generator uses lambda expressions, reactive programming, just a click away. Very convenient to write

  • author Specifies author

  • outputDir, specifies to which output the generated file

  • enableSwagger, support swagge (very nice, remember to quote swagger dependency)

  • commentDate Time Format

  • fileOveride overwrites previously generated files

    The effect of globalConfig is as follows:

2. PackageConfig

This is to configure which packages are generated:

Note: The way to configure xml packages is called mapperXml on the official website and xml() in the actual code.

3. StrategyConfig

addInclude() is the table for which code is generated, with several overloads:

  • A String's Table Name
  • Any number of table names (variable length parameters):'user','user1',...
  • List list

The so-called policy configuration is to configure policy, configure details

It places service, mapper, controller, entity in the policy configuration. Previous versions were in the global configuration

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

At the end of the code:

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

You can specify a template engine.

execute() executes the code generator to generate code

Configuring different options to suit your needs makes it easy to do just that. Suggestions or 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 -> {
          "Directional culture")               //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 a generic resultMap
                .templateEngine(new FreemarkerTemplateEngine()) // Use Freemarker engine template, default is Velocity engine template

Keywords: Javascript Front-end html

Added by Poomerio on Sun, 20 Feb 2022 09:54:38 +0200