1. Import 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>
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: https://mp.baomidou.com/guide/generator-new.html#%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8
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 .execute();
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 https://mp.baomidou.com/guide/generator-new.html#%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8
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() .superClass(BaseMapper.class) .formatMapperFileName("%sMapper") .enableMapperAnnotation() .formatXmlFileName("%sMapper"); }) .templateEngine(new FreemarkerTemplateEngine()) // Use the Freemarker engine template. The default is the Velocity engine template .execute(); } }