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:

<!--mybatisPlus-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3.4</version>
</dependency>
<!--mybatis-plus Code generator -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.5.1</version>
</dependency>
<!--velocity Template-->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.3</version>
</dependency>
<!--freemarker Template-->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
</dependency>
<!--mysql-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

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
.execute();

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<>();
        tables.add("p_user");
        tables.add("p_question");
        tables.add("p_answer");
        tables.add("p_correct");

        FastAutoGenerator.create("jdbc:mysql://localhost:3306/xpa","root","111111")
                .globalConfig(builder -> {
                    builder.author("Xiang Pei")               //author
                            .outputDir(System.getProperty("user.dir")+"\src\main\java")    //Output path (write to java directory)
                            .enableSwagger()           //Open swagger
                            .commentDate("yyyy-MM-dd")
                            .fileOverride();            //Open files generated before overwriting

                })
                .packageConfig(builder -> {
                    builder.parent("com.xp")
                            .moduleName("practice")
                            .entity("entity")
                            .service("service")
                            .serviceImpl("serviceImpl")
                            .controller("controller")
                            .mapper("mapper")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml,System.getProperty("user.dir")+"\src\main\resources\mapper"));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables)
                            .addTablePrefix("p_")
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .entityBuilder()
                            .enableLombok()
                            .logicDeleteColumnName("deleted")
                            .enableTableFieldAnnotation()
                            .controllerBuilder()
                            .formatFileName("%sController")
                            .enableRestStyle()
                            .mapperBuilder()
                            .enableBaseResultMap()  //Generate general resultMap
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");
                })
                .templateEngine(new FreemarkerTemplateEngine()) // Freemarker engine template is used. The default is Velocity engine template
                .execute();
    }
}

Topics: Javascript Front-end TypeScript Vue.js html