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:

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

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<>();
        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("Directional culture")               //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 a generic resultMap
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");
                })
                .templateEngine(new FreemarkerTemplateEngine()) // Use Freemarker engine template, default is Velocity engine template
                .execute();
    }
}

Keywords: Javascript Front-end html

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