Mybatis使用自定义插件去掉POJO的Getter和Setter方法

来源:http://www.bj9420.com

编者:吴理琪(wRitchie)

一、Mybatis generator 自定义插件(ngs)开发

1、新建maven Java工程ngs(noGetterSetter)

2、pom.xm引入mybatis-generator-core依赖


xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven4.0.0.
xsd">
4.0.0
com.bj9420
ngs
1.0.0
1.3.7
org.mybatis.generator
mybatisgeneratorcoreartifactId>
${mybatis.generator.core.version}

3、在java目录下创NoGetterSetterPlugin.java

package com.bj9420;
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.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import java.util.List;
* @Title: NoGetterSetterPlugin.java
* @Description: Mybatis使用插件去掉POJO对象的Getter和Setter,并加上
@Data注解
* @author: wRitchie
* @date: 2019/3/6 16:09
* @version: V1.0
* @Copyright (c): 2019 http://www.bj9420.com All rights reserved.
public class NoGetterSetterPlugin extends PluginAdapter {
* @Author: wRitchie
* @Description: validate implement abstract method
'validate(List)' in 'Plugin'
* @Param: [list]
* @return: boolean
* @Date: 2019/3/7 10:21
public boolean validate(List list) {
return true;

* @Author: wRitchie
* @Description: modelBaseRecordClassGenerated 该代码表示在生成
class的时候,向topLevelClass添加一个@Data /@Setter和@Getter注解
* @Param: [topLevelClass, introspectedTable]
* @return: boolean
* @Date: 2019/3/6 16:21
@Override
public boolean modelBaseRecordClassGenerated(TopLevelClass
topLevelClass, IntrospectedTable introspectedTable) {
topLevelClass.addAnnotation("import lombok.Data;\n@Data");
return super.modelBaseRecordClassGenerated(topLevelClass,
introspectedTable);

* @Author: wRitchie
* @Description: modelGetterMethodGenerated 该方法在生成每一个属性
的getter方法时候调用,如果我们不想生成getter,直接返回false即可;
* @Param: [method, topLevelClass, introspectedColumn,
introspectedTable, modelClassType]
* @return: boolean
* @Date: 2019/3/6 16:20
@Override
public boolean modelGetterMethodGenerated(Method method,
TopLevelClass topLevelClass, IntrospectedColumn
introspectedColumn, IntrospectedTable introspectedTable,
ModelClassType modelClassType) {
return false;

* @Author: wRitchie
* @Description: modelSetterMethodGenerated 该方法在生成每一个属性
的setter方法时候调用,如果我们不想生成setter,直接返回false即可;
* @Param: [method, topLevelClass, introspectedColumn,
introspectedTable, modelClassType]
* @return: boolean
* @Date: 2019/3/6 16:20
@Override
public boolean modelSetterMethodGenerated(Method
method,TopLevelClass topLevelClass, IntrospectedColumn
introspectedColumn,IntrospectedTable introspectedTable,
ModelClassType modelClassType) {
return false;

4、安装到本地仓库:idea右侧点击Maven,双击执行install

5、执行成功如下图

二、Mybatis generator 自定义插件的使用

1、在实际的项目中,对Mybatis-generator代码插件,增加自定义插件至配置文件

generatorConfig.xml中:

type="com.bj9420.NoGetterSetterPlugin">,如下图:

2、pom.xm引入ngs依赖

3、点击Maven,双击mybatis-generator:generate,自动生成POJO、Mapper、XML文

4、执行完如下图:

5、自动生成的POJO如下图: