第一步,制作MybatisGenerator+Lombok 插件
首先创建一个新的maven项目导入mybatis-generator-maven-plugin包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.pluto</groupId> <artifactId>mybatis-generator-lombok</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>mybatis-generator-lombok</name> <description>mybatis-generator-lombok</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
其次创建LombokPlugin插件类
package com.fh.plugin.mybatis.generator.lombok.plugins;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.Plugin;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.java.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* @author FuHui
* @date 2020/09/12
*/
@SuppressWarnings("unused")
public class LombokPlugin extends PluginAdapter {
private Field field;
private TopLevelClass topLevelClass;
private IntrospectedColumn introspectedColumn;
private IntrospectedTable introspectedTable;
private ModelClassType modelClassType;
@Override
public boolean validate(List<String> list) {
return true;
}
@Override
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
//实体类的import
topLevelClass.addImportedType("java.io.Serializable");
topLevelClass.addImportedType("lombok.Data");
topLevelClass.addImportedType("lombok.Builder");
topLevelClass.addImportedType("lombok.NoArgsConstructor");
topLevelClass.addImportedType("lombok.AllArgsConstructor");
topLevelClass.addImportedType("io.swagger.annotations.ApiModelProperty");
//实体类的注解
topLevelClass.addAnnotation("@Data");
topLevelClass.addAnnotation("@Builder");
topLevelClass.addAnnotation("@NoArgsConstructor");
topLevelClass.addAnnotation("@AllArgsConstructor");
//实体类的注释
topLevelClass.addJavaDocLine("/**");
topLevelClass.addJavaDocLine(" * @author FuHui");
topLevelClass.addJavaDocLine(" * @date "+new SimpleDateFormat("yyyy/MM/dd/hh:mm").format(new Date()));
topLevelClass.addJavaDocLine(" */");
//接口
topLevelClass.addSuperInterface(new FullyQualifiedJavaType("java.io.Serializable"));
return true;
}
@Override
public boolean clientGenerated(Interface anInterface, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
//Mapper文件的注释
anInterface.addJavaDocLine("/**");
anInterface.addJavaDocLine(" * @author FuHui");
anInterface.addJavaDocLine(" * @date "+new SimpleDateFormat("yyyy/MM/dd/hh:mm").format(new Date()));
anInterface.addJavaDocLine(" */");
return true;
}
@Override
public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
//不生成getter
return false;
}
@Override
public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
//不生成setter
return false;
}
@Override
public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
this.field = field;
this.topLevelClass = topLevelClass;
this.introspectedColumn = introspectedColumn;
this.introspectedTable = introspectedTable;
this.modelClassType = modelClassType;
// 字段添加Swagger注解@ApiModelProperty
this.field.addAnnotation("@ApiModelProperty(name = "" + this.field.getName() + "", value = "" + introspectedColumn.getRemarks() + "", hidden = false)");
return true;
}
}
其中TopLevelClass中提供对类操作的方法,如导入包,增加类注解等
IntrospectedTable提供对应表的各种参数,如表名,表规则,主键等信息
Interface提供了对接口类操作的方法
Filed提供了对实体类属性的操作方法,包含注解,注释,类型等
IntrospectedColumn提供对应表中各字段的信息,如类型,名称备注等
配置好之后,在Terminal下输入 mvn clean package生成jar包
第二步,导入maven库
将jar包使用命令导入maven(替换自己的路径):
mvn install:install-file -Dfile=E:gitchanachanaservicesrcmainwebappWEB-INFlibmybatis-generator-lombok-1.0-SNAPSHOT.jar -DgroupId=com.fh.plugin -DartifactId=mybatis-generator-lombok -Dversion=1.0-SNAPSHOT -Dpackaging=jar
增加maven配置:
注:此处不用在公共配置增加,直接在generator所需要的依赖中添加即可
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <dependencies> <!--配置这个依赖主要是为了等下在配置mybatis-generator.xml的时候可以不用配置classPathEntry这样的一个属性,避免代码的耦合度太高--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>com.fh.plugin</groupId> <artifactId>mybatis-generator-lombok</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <executions> <execution> <id>Generate MyBatis Artifacts</id> <phase>package</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <!--允许移动生成的文件 --> <verbose>true</verbose> <!-- 是否覆盖 --> <overwrite>true</overwrite> <!-- 自动生成的配置 --> <configurationFile>src/main/resources/generator/mybatis-generator.xml</configurationFile> </configuration> </plugin>
第三步:配置插件
在mybatis-generator.xml配置文件中新增插件:
<!-- 整合lombok--> <plugin type="com.fh.plugin.mybatis.generator.lombok.plugins.LombokPlugin" > <property name="hasLombok" value="true"/> </plugin> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <!-- 通用mapper插件,在mapper层以及XML文件不会生成基本的增删改查方法(这里不用,需要可以放开注释) --> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="tk.mybatis.mapper.common.BaseMapper"/> </plugin>
执行mybatis-generator:generate结束。
本篇文章全程参考:https://blog.csdn.net/qq_41958793/article/details/89020093
因需要再实体类生成时,添加Swagger的字段注解,所以发此文章以作记录。