配置mybatisplus代码生成器
时间: 2023-09-08 14:16:32 浏览: 124
Sure, I can help you with configuring the MyBatis-Plus code generator. Here is a step-by-step guide:
Step 1: Add MyBatis-Plus dependency
First, make sure you have added the MyBatis-Plus dependency to your project's `pom.xml` file if you are using Maven, or `build.gradle` file if you are using Gradle. Here is an example for Maven:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>latest-version</version>
</dependency>
```
Replace `latest-version` with the latest version of MyBatis-Plus.
Step 2: Configure the Code Generator
Next, you need to configure the code generator. MyBatis-Plus provides a built-in code generator called `MyBatisGenerator`. You can configure it in the `mybatis-plus-generator.properties` file.
Create a file named `mybatis-plus-generator.properties` in the `src/main/resources` directory and add the following properties:
```properties
# DataSource configuration
mp.generator.jdbc.driver=com.mysql.jdbc.Driver
mp.generator.jdbc.url=jdbc:mysql://localhost:3306/db_name
mp.generator.jdbc.username=root
mp.generator.jdbc.password=password
# Package configuration
mp.generator.package.parent=com.example.project
mp.generator.package.entity=model
mp.generator.package.mapper=mapper
mp.generator.package.xml=mapper.xml
# Strategy configuration
mp.generator.strategy.super-entity-class=com.baomidou.mybatisplus.extension.activerecord.Model
mp.generator.strategy.super-mapper-class=com.baomidou.mybatisplus.core.mapper.BaseMapper
mp.generator.strategy.super-service-class=com.baomidou.mybatisplus.extension.service.IService
mp.generator.strategy.super-service-impl-class=com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
# Template configuration
mp.generator.template.mapper=templates/mapper.java.vm
mp.generator.template.xml=templates/mapper.xml.vm
mp.generator.template.entity=templates/entity.java.vm
mp.generator.template.service=templates/service.java.vm
mp.generator.template.service-impl=templates/serviceImpl.java.vm
```
Make sure to replace the values according to your project's settings and requirements.
Step 3: Generate code
Once the configuration is done, you can now generate code using the MyBatis-Plus code generator. You can do this by executing the `MyBatisGenerator` class in your project. Here is an example:
```java
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
public class CodeGenerator {
public static void main(String[] args) {
// Step 3: Generate code
AutoGenerator mpg = new AutoGenerator();
// Global configuration
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
gc.setAuthor("Your Name");
gc.setOpen(false);
mpg.setGlobalConfig(gc);
// DataSource configuration
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/db_name");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("password");
mpg.setDataSource(dsc);
// Package configuration
PackageConfig pc = new PackageConfig();
pc.setParent("com.example.project");
pc.setEntity("model");
pc.setMapper("mapper");
pc.setXml("mapper.xml");
mpg.setPackageInfo(pc);
// Strategy configuration
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("table_name"); // Specify the tables you want to generate code for
strategy.setSuperEntityClass("com.baomidou.mybatisplus.extension.activerecord.Model");
strategy.setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper");
strategy.setSuperServiceClass("com.baomidou.mybatisplus.extension.service.IService");
strategy.setSuperServiceImplClass("com.baomidou.mybatisplus.extension.service.impl.ServiceImpl");
mpg.setStrategy(strategy);
// Execute code generation
mpg.execute();
}
}
```
Replace the values accordingly, especially the database connection settings and the table names you want to generate code for.
That's it! You have now successfully configured the MyBatis-Plus code generator. When you run the `CodeGenerator` class, it will generate the required files based on the configuration provided.
阅读全文