springboot集成mybatis-generator加lombok免get/set方法


springboot集成mybatis-generator加lombok

集成mybatis-generator

1.编辑springboot的pom配置文件

2.编辑generatorConfig配置文件

详细教程请看:Springboot/mybatis-generator

集成lombok

1.引入相关依赖

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

2.自定义generator-lombok工具

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.Field;
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.internal.util.StringUtility;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: LombokPlugin
 * @author: Wayen
 * @create: 2020-04-27 18:52
 **/
public class LombokPlugin extends PluginAdapter {
    
    public LombokPlugin() {
    }
    
    public boolean validate(List<String> list) {
        return true;
    }

    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        
        // domain import
        topLevelClass.addImportedType("lombok.Data");

        // domain description
        topLevelClass.addJavaDocLine("/**");
        String remarks = introspectedTable.getRemarks();
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            for (String remarkLine : remarkLines) {
                topLevelClass.addJavaDocLine(" * @tableRemark " + remarkLine);
            }
        }
        StringBuilder sb = new StringBuilder();
        sb.append(" * @tableName ").append(introspectedTable.getFullyQualifiedTable());
        topLevelClass.addJavaDocLine(sb.toString());
        sb.setLength(0);
        sb.append(" * @author Wayen-generator-lombok");
        topLevelClass.addJavaDocLine(sb.toString());
        sb.setLength(0);
        sb.append(" * @date ");
        sb.append(getDateString());
        topLevelClass.addJavaDocLine(sb.toString());
        topLevelClass.addJavaDocLine(" */");
        
        // domain annotation
        topLevelClass.addAnnotation("@Data");
        
        return true;
    }

    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
            IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        
        // field description        
        field.addJavaDocLine("/**");
        String remarks = introspectedColumn.getRemarks();
        if (StringUtility.stringHasValue(remarks)) {
            String[] remarkLines = remarks.split(System.getProperty("line.separator"));
            for (String remarkLine : remarkLines) {
                field.addJavaDocLine(" * " + remarkLine);
            }
        }
        field.addJavaDocLine(" */");
        
        return true;
    }

    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        
        // mapper import
        interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper"));
        
        // mapper description
        topLevelClass.addJavaDocLine("/**");
        StringBuilder sb = new StringBuilder();
        sb.append(" * @author Wayen-generator-lombok");
        topLevelClass.addJavaDocLine(sb.toString());
        sb.setLength(0);
        sb.append(" * @date ");
        sb.append(getDateString());
        topLevelClass.addJavaDocLine(sb.toString());
        topLevelClass.addJavaDocLine(" */");
        
        // mapper annotation
        interfaze.addAnnotation("@Mapper");
        
        return true;
    }

    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
            IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        // no getter
        return false;
    }

    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
            IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        // no setter
        return false;
    }
    
    protected String getDateString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }
}

3.将工具类单独编译

javac -cp *\mybatis-generator-core-1.4.0.jar LombokPlugin.java

将*替换为mybatis-generator-core-1.4.0.jar的绝对路径

4.将编译生成的LombokPlugin.class文件加入mybatis-generator-core包中orgmybatisgeneratorplugins路径下,可以使用压缩工具打开jar包

5.在mybatis-generator-maven-plugin中引入修改后的mybatis-generator-core

<dependency>
    <groupId>org.mybatis.generator </groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.4.0</version>
</dependency>

6.在generator配置文件中添加自定义工具

<plugin type="org.mybatis.generator.plugins.LombokPlugin" />

使用方法

正常点击mybatis-generator工具使用

生成的实体类文件截图

声明:Wayen|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - springboot集成mybatis-generator加lombok免get/set方法


-当你感到最无助困难的时候,那就是离成功最近的时候。-Martin Frohm