代码生成器:mybatis-plus-generator自定义模板生成 dto、vo、convertor
时间: 2023-08-06 22:01:43 浏览: 68
Mybatis-Plus Generator 是Mybatis-Plus 提供的代码生成器,可以快速生成 Mybatis-Plus 所需的 Entity、Mapper、Service、Controller 等类文件。但是默认生成的代码只包含 Entity 和 Mapper,如果需要生成其他文件如 DTO、VO、Convertor 等,则需要自定义模板来实现。
以下是使用 Mybatis-Plus Generator 自定义模板生成 DTO、VO、Convertor 的步骤:
1. 在 pom.xml 中添加依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
```
2. 在 resources 目录下创建 mybatis-plus-generator 目录,并创建 generatorConfig.xml 配置文件,在其中配置需要生成的表名、文件路径、包名等信息。示例配置如下:
```xml
<generatorConfiguration>
<dataSource type="com.zaxxer.hikari.HikariDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
<packageInfo>
<parent>com.example.demo</parent>
<dto>dto</dto>
<vo>vo</vo>
<convertor>convertor</convertor>
</packageInfo>
<strategy>
<tablePrefix>tbl_</tablePrefix>
<include>tbl_user</include>
</strategy>
<template>
<mapper>/templates/mapper.xml.vm</mapper>
<entity>/templates/entity.java.vm</entity>
<dto>/templates/dto.java.vm</dto>
<vo>/templates/vo.java.vm</vo>
<convertor>/templates/convertor.java.vm</convertor>
</template>
<globalConfig>
<outputDir>src/main/java</outputDir>
<fileOverride>true</fileOverride>
<open>false</open>
<enableCache>false</enableCache>
<author>mybatis-plus-generator</author>
<kotlin>false</kotlin>
</globalConfig>
</generatorConfiguration>
```
其中:
- dataSource:数据源配置
- packageInfo:包名配置,dto、vo、convertor 分别对应生成的文件夹
- strategy:生成策略,tablePrefix 为表前缀,include 为需要生成的表名
- template:模板配置,mapper.xml、entity、dto、vo、convertor 分别对应生成的文件类型和模板文件路径
- globalConfig:全局配置,outputDir 为输出文件夹,fileOverride 为是否覆盖已有文件,author 为作者名
3. 在 templates 目录下创建 dto.java.vm、vo.java.vm、convertor.java.vm 模板文件,并根据需要进行自定义。例如生成 DTO 文件的模板如下:
```java
package $packageInfo.dto;
import lombok.Data;
@Data
public class $!{tableInfo.name}DTO {
#foreach($field in $tableInfo.fields)
private $field.propertyType $field.propertyName;
#end
}
```
其中 $!{tableInfo.name} 表示表名,$field.propertyType 和 $field.propertyName 分别表示字段类型和字段名。
4. 在代码中使用 Mybatis-Plus Generator 生成代码。例如在 main 方法中调用 generate() 方法:
```java
public static void main(String[] args) {
AutoGenerator generator = new AutoGenerator();
generator.setTemplateEngine(new VelocityTemplateEngine());
generator.setTemplate(new TemplateConfig().setXml(null));
// 使用配置文件生成代码
generator.setConfig(new FileGeneratorConfigBuilder().getConfig());
generator.execute();
}
```
其中 setTemplateEngine() 设置模板引擎为 Velocity,setTemplate() 设置生成的 XML 文件为空,setConfig() 从配置文件中获取配置。
5. 运行 main 方法,即可生成对应的 DTO、VO、Convertor 文件。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)