使用Mybatis的小伙伴对Generator一定非常熟悉, 在这里我便不多做介绍了.
但generator自动生成的domain.java里面默认带有了所有get/set方法, 让人眼花缭乱.
如何让生成domain没有get/set方法, 同时自动贴上lombok的几个常用标签呢.
话不多说, 下面直接开始.
首先, 先继承Mybatis的插件适配器, 写一个自己的插件
package org.mybatis.generator.plugins;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
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 java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* @author Liweizhou 2018/6/6
*/
public class LombokPlugin extends PluginAdapter {
@Override
public boolean validate(List<String> list) {
return true;
}
@Override
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
//添加domain的import
topLevelClass.addImportedType("lombok.Data");
topLevelClass.addImportedType("lombok.Builder");
topLevelClass.addImportedType("lombok.NoArgsConstructor");
topLevelClass.addImportedType("lombok.AllArgsConstructor");
//添加domain的注解
topLevelClass.addAnnotation("@Data");
topLevelClass.addAnnotation("@Builder");
topLevelClass.addAnnotation("@NoArgsConstructor");
topLevelClass.addAnnotation("@AllArgsConstructor");
//添加domain的注释
topLevelClass.addJavaDocLine("/**");
topLevelClass.addJavaDocLine("* Created by Mybatis Generator on " + date2Str(new Date()));
topLevelClass.addJavaDocLine("*/");
return true;
}
@Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
//Mapper文件的注释
interfaze.addJavaDocLine("/**");
interfaze.addJavaDocLine("* Created by Mybatis Generator on " + date2Str(new Date()));
interfaze.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;
}
private String date2Str(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
return sdf.format(date);
}
}
首先在本机编译这份java文件, 生成的字节码文件(.class), 先到maven仓库的这个目录
org\mybatis\generator\mybatis-generator-core\1.3.5
(版本无所谓, 根据自己情况修改)
找到这个mybatis-generator-core-1.3.5.jar
文件
把字节码塞到 jar包对应的包文件夹下面
然后再mybatis-generator.xml配置文件中加上这句话
<plugin type="org.mybatis.generator.plugins.LombokPlugin" >
<property name="hasLombok" value="true"/>
</plugin>
此时mybatis-generator.xml
大概如下: 留意<plugin>标签的位置, 我用的是pg, 直接复制粘贴的同学请修改成数据库类型
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="postgresql" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.LombokPlugin" >
<property name="hasLombok" value="true"/>
</plugin>
<!--关闭注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接信息 -->
<jdbcConnection driverClass="org.postgresql.Driver"
connectionURL=""
userId="" password="">
</jdbcConnection>
<!--生成的model 包路径 -->
<javaModelGenerator targetPackage="com.lwz.dao.generator.model"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaModelGenerator>
<!--生成xml mapper文件 路径 -->
<sqlMapGenerator targetPackage="mapper/generator" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成的Dao接口 的包路径 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.lwz.dao.generator.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--对应数据库表名 -->
<table tableName="example">
</table>
</context>
</generatorConfiguration>
然后生成一遍
/**
* Created by Mybatis Generator on 2018/06/06
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Example{
搞定啦.
偷懒的朋友可以到这里下载jar包,直接使用
MyBatis-Generator 自动生成Lombok标签 不生成Get/Set方法-CSDN下载
网友评论