1 introduction
MyBatis Plus Starter has recently been updated to version 3.4.3.1, while MyBatis Plug Generator has been updated to version 3.5.0. However, the official documentation has not updated the Generator code, and the use of MyBatis Plus has been introduced in the previous article. So here is a follow-up article on using code generators in conjunction with Spring Boot.
Why code generators
Code generators can be used to generate code for fixed templates, such as:
Controller
Layer codeService
Layer codemapper
- Entity class
For example, a User class could generate code like this:
3 environmental
This example uses the following environment:
Spring Boot 2.5.1
MyBaits Plus 3.4.3.1
MyBatis Plus the Generator 3.5.0
4 Prepare data tables
Here we create a User table User for ease of use with Workbench:
5 Create a project and import dependencies
Create a new Spring Boot project and import the following dependencies:
implementation 'com. Baomidou: mybatis - plus - the boot - starter: 3.4.3.1'
implementation 'com. Baomidou: mybatis - plus - generator: 3.5.0'
implementation 'org. Apache. Velocity, velocity - engine - core: 2.3'
implementation 'org.realityforge.org.jetbrains.annotations:org.jetbrains.annotations:1.7.0'
Copy the code
In addition to the starter and generator, you need a template engine (optional Velocity, Freemarker, Beetl, the default Velocity) and an annotation dependency (JetBrains. Annotations).
Maven versions are as follows:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.realityforge.org.jetbrains.annotations</groupId>
<artifactId>org.jetbrains.annotations</artifactId>
<version>1.7.0</version>
</dependency>
Copy the code
6 Create a new generator class
This class is used to configure code generation:
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
public class MyBatisPlusGenerator {
public static void main(String[] args) {
DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder("jdbc:mysql://localhost:3306/test"."root"."123456").build();
String projectPath = System.getProperty("user.dir");
GlobalConfig globalConfig = new GlobalConfig.Builder().outputDir(projectPath+"/src/main/java").openDir(false).build();
PackageConfig packageConfig = new PackageConfig.Builder().moduleName("test").parent("com.example.test").build();
AutoGenerator autoGenerator = newAutoGenerator(dataSourceConfig); autoGenerator.global(globalConfig).packageInfo(packageConfig); autoGenerator.execute(); }}Copy the code
The code generator configuration uses Builder mode instead of setter mode compared to previous versions:
DataSourceConfig dataSourceConfig = new DataSourceConfig(); // In version 3.5.0, the default constructor became private and failed to compile
dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/ant? useUnicode=true&useSSL=false&characterEncoding=utf8");
dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("password");
Copy the code
The data source, output code path, and output package name are set in the above code, and can be modified as needed:
DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder("jdbc:mysql://localhost:3306/test"."root"."123456").build();
String projectPath = System.getProperty("user.dir");
GlobalConfig globalConfig = new GlobalConfig.Builder().outputDir(projectPath+"/src/main/java").openDir(false).build();
PackageConfig packageConfig = new PackageConfig.Builder().moduleName("test").parent("com.example.test").build();
Copy the code
When you are ready to run main directly, you will generate a test folder under SRC /main/ Java:
7 Test Run
First modify the configuration file and add the data source:
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: 123456
Copy the code
Modify UserController as follows and add a test method:
@RestController
@RequestMapping("/test/user")
public class UserController {
@Resource
private UserServiceImpl userService;
@GetMapping("/")
public String test(a) {
return userService.getById(1).toString(); }}Copy the code
If you try to run the main method, the following error will be reported:
If UserMapper is not found, add @mapper to UserMapper:
@Mapper
public interface UserMapper extends BaseMapper<User> {}Copy the code
Or add a @mapperscan to the main class, plus the fully qualified path where the mapper is:
@SpringBootApplication
@MapperScan("com.example.test.test.mapper")
public class TestApplication {
public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); }}Copy the code
Such access localhost: 8080 / test/user/can access to the user id = 1:
8 Other Configurations
This is just the simplest generator configuration, but the complete code generator configuration is as follows:
DataSourceConfig
: Data source configuration, including database type, driver, and connectionURL
, user name, password, etcStrategyConfig
: Database table configuration (but the name is really not obvious), you can specify which tables to generate code for or exclude code from, you can set the generated field prefix, you can also support fuzzy matching table names (exclude or include), and so onPackageConfig
: package configuration, specifying the module name, package name,mapper
Naming,service
Naming,controller
Name etc.TemplateConfig
: template configuration, you can customize the generated templates, including entity class templates,service
Templates,controller
Templates,mapper
Templates,mapper xml
Templates, etc.GlobalConfig
: global configuration, can specify the output code directory, whether to overwrite files and other configurations, while supportingKotlin
andSwagger2
InjectionConfig
: Injection configuration, you can customize the configurationMap
Object etc.
Please check the official documentation for details.
Nine source
Reference source code, Java version:
- Github
- Yards cloud
10 Reference Links
- MyBatis Plus official documentation