MyBatis自动生成插件 生成批量插入 生成PO注释 自动分页limit

MyBatis自动生成插件 生成批量插入 生成PO注释 自动分页limit

Mapper生成批量插入 batchInsert batchInsertSelective


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.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author wuguoqiong
 */
public class BatchInsertPlugin extends PluginAdapter {
    public boolean validate(List<String> list) {
        return true;
    }

    /**
     * 修改Mapper类
     */
    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        addBatchInsertMethod(interfaze, introspectedTable);

        addDeleteMethon(interfaze, introspectedTable);
        return true;
    }

    private void addDeleteMethon(Interface interfaze, IntrospectedTable introspectedTable) {
        boolean noGenerate = true;
        String columnName = "";
        String deleteFiledType = "";

        for (IntrospectedColumn column : introspectedTable.getAllColumns()) {
            if ("is_deleted".equals(column.getActualColumnName())) {
                noGenerate = false;
                deleteFiledType = column.getJdbcTypeName();
            }
            if ("id".equals(column.getActualColumnName())) {
                columnName = column.getActualColumnName();
            }
        }

        if (noGenerate) {
            return;
        }

        if (columnName == null || "".equals(columnName)) {
            return;
        }

        if (!"INTEGER".equals(deleteFiledType) && !"BIGINT".equals(deleteFiledType)) {
            return;
        }

        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
        importedTypes.add(new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()));
        FullyQualifiedJavaType ibsreturnType = FullyQualifiedJavaType.getIntInstance();
        Method deleteMethod = new Method();
        // 1.设置方法可见性
        deleteMethod.setVisibility(JavaVisibility.PUBLIC);
        // 2.设置返回值类型 int类型
        deleteMethod.setReturnType(ibsreturnType);
        // 3.设置方法名
        deleteMethod.setName("deleteById");
        // 4.设置参数列表
        FullyQualifiedJavaType paramType = new FullyQualifiedJavaType("java.lang.Long");
        deleteMethod.addParameter(new Parameter(paramType, "id"));

        deleteMethod.addJavaDocLine("/** 删除单条记录(update is_deleted) */");
        interfaze.addImportedTypes(importedTypes);
        interfaze.addMethod(deleteMethod);
    }

    private void addBatchInsertMethod(Interface interfaze, IntrospectedTable introspectedTable) {
        // 设置需要import的类
        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
        importedTypes.add(FullyQualifiedJavaType.getNewListInstance());
        importedTypes.add(new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()));
        FullyQualifiedJavaType ibsreturnType = FullyQualifiedJavaType.getIntInstance();
        Method batchInsertMethod = new Method();
        // 1.设置方法可见性
        batchInsertMethod.setVisibility(JavaVisibility.PUBLIC);
        // 2.设置返回值类型 int类型
        batchInsertMethod.setReturnType(ibsreturnType);
        // 3.设置方法名
        batchInsertMethod.setName("batchInsert");
        batchInsertMethod.addJavaDocLine("/** 批量新增 */");
        // 4.设置参数列表
        FullyQualifiedJavaType paramType = FullyQualifiedJavaType.getNewListInstance();
        FullyQualifiedJavaType paramListType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        paramType.addTypeArgument(paramListType);
        batchInsertMethod.addParameter(new Parameter(paramType, "records"));
        interfaze.addImportedTypes(importedTypes);
        interfaze.addMethod(batchInsertMethod);
        Method batchInsertSelectiveMethod = new Method();
        // 1.设置方法可见性
        batchInsertSelectiveMethod.setVisibility(JavaVisibility.PUBLIC);
        // 2.设置返回值类型 int类型
        batchInsertSelectiveMethod.setReturnType(ibsreturnType);
        // 3.设置方法名
        batchInsertSelectiveMethod.setName("batchInsertSelective");
        batchInsertSelectiveMethod.addJavaDocLine("/** 批量新增,仅处理不为空的字段 */");
        // 4.设置参数列表
        FullyQualifiedJavaType paramTypeSelective = FullyQualifiedJavaType.getNewListInstance();
        FullyQualifiedJavaType paramListTypeSelective = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        paramTypeSelective.addTypeArgument(paramListTypeSelective);
        batchInsertSelectiveMethod.addParameter(new Parameter(paramTypeSelective, "records", "@Param(\"records\")"));
        batchInsertSelectiveMethod.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "columns", "@Param(\"columns\")", true));
        interfaze.addImportedTypes(importedTypes);
        interfaze.addMethod(batchInsertSelectiveMethod);
    }

    /**
     * 修改Mapper.xml
     */
    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
        addBatchInsertXml(document, introspectedTable);
        addBatchInsertSelectiveXml(document, introspectedTable);

        addDeleteXml(document, introspectedTable);
        return true;
    }

    private void addDeleteXml(Document document, IntrospectedTable introspectedTable) {
        boolean noGenerate = true;
        String columnName = "";
        String javaProperty = "";
        String jdbcTypeName = "";
        String deleteFiledType = "";
        String deletedValueLabel = "";

        for (IntrospectedColumn column : introspectedTable.getAllColumns()) {
            if ("is_deleted".equals(column.getActualColumnName())) {
                noGenerate = false;
                deleteFiledType = column.getJdbcTypeName();
            }
            if ("id".equals(column.getActualColumnName())) {
                columnName = column.getActualColumnName();
                javaProperty = column.getJavaProperty();
                jdbcTypeName = column.getJdbcTypeName();
            }
        }

        if (noGenerate) {
            return;
        }

        if (columnName == null || "".equals(columnName)) {
            return;
        }

        if ("INTEGER".equals(deleteFiledType)) {
            deletedValueLabel = " 1";
        } else if ("BIGINT".equals(deleteFiledType)) {
            deletedValueLabel = " #{" + javaProperty + ",jdbcType=" + jdbcTypeName + "}";
        } else {
            return;
        }

        XmlElement deleteElement = new XmlElement("update");
        deleteElement.addAttribute(new Attribute("id", "deleteById"));
        deleteElement.addAttribute(new Attribute("parameterType", "java.lang.Long"));

        deleteElement.addElement(new TextElement("update " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
        deleteElement.addElement(new TextElement("  set is_deleted = " +
                deletedValueLabel));
        deleteElement.addElement(new TextElement("where id = " + " #{" + javaProperty + ",jdbcType=" + jdbcTypeName + "}"));
        document.getRootElement().addElement(deleteElement);
    }

    private void addBatchInsertXml(Document document, IntrospectedTable introspectedTable) {
        // 
        XmlElement insertBatchElement = new XmlElement("insert");
        insertBatchElement.addAttribute(new Attribute("id", "batchInsert"));
        insertBatchElement.addAttribute(new Attribute("parameterType", "java.util.List"));
        XmlElement valueTrimElement = new XmlElement("trim");
        valueTrimElement.addAttribute(new Attribute("prefix", " ("));
        valueTrimElement.addAttribute(new Attribute("suffix", ")"));
        valueTrimElement.addAttribute(new Attribute("suffixOverrides", ","));
        XmlElement columnTrimElement = new XmlElement("trim");
        columnTrimElement.addAttribute(new Attribute("prefix", "("));
        columnTrimElement.addAttribute(new Attribute("suffix", ")"));
        columnTrimElement.addAttribute(new Attribute("suffixOverrides", ","));
        List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
        for (IntrospectedColumn introspectedColumn : columns) {
            String columnName = introspectedColumn.getActualColumnName();
            columnTrimElement.addElement(new TextElement(columnName+","));
            valueTrimElement.addElement(new TextElement("#{item." + introspectedColumn.getJavaProperty() + ",jdbcType=" + introspectedColumn.getJdbcTypeName() + "},"));
        }
        XmlElement foreachElement = new XmlElement("foreach");
        foreachElement.addAttribute(new Attribute("collection", "list"));
        foreachElement.addAttribute(new Attribute("index", "index"));
        foreachElement.addAttribute(new Attribute("item", "item"));
        foreachElement.addAttribute(new Attribute("separator", ","));
        insertBatchElement.addElement(new TextElement("insert into " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
        insertBatchElement.addElement(columnTrimElement);
        insertBatchElement.addElement(new TextElement(" values "));
        foreachElement.addElement(valueTrimElement);
        insertBatchElement.addElement(foreachElement);
        document.getRootElement().addElement(insertBatchElement);
    }
    private void addBatchInsertSelectiveXml(Document document, IntrospectedTable introspectedTable) {
        // 
        XmlElement insertBatchElement = new XmlElement("insert");
        insertBatchElement.addAttribute(new Attribute("id", "batchInsertSelective"));
        insertBatchElement.addAttribute(new Attribute("parameterType", "map"));
        XmlElement foreachColumn = new XmlElement("foreach");
        foreachColumn.addAttribute(new Attribute("collection", "columns"));
        foreachColumn.addAttribute(new Attribute("index", "index"));
        foreachColumn.addAttribute(new Attribute("item", "item"));
        foreachColumn.addAttribute(new Attribute("separator", ","));
        foreachColumn.addAttribute(new Attribute("open", "("));
        foreachColumn.addAttribute(new Attribute("close", ")"));
        foreachColumn.addElement(new TextElement("${item}"));
        insertBatchElement.addElement(new TextElement("insert into " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
        insertBatchElement.addElement(foreachColumn);
        insertBatchElement.addElement(new TextElement(" values "));
        XmlElement valueTrimElement = new XmlElement("trim");
        valueTrimElement.addAttribute(new Attribute("prefix", " ("));
        valueTrimElement.addAttribute(new Attribute("suffix", ")"));
        valueTrimElement.addAttribute(new Attribute("suffixOverrides", ","));
        XmlElement foreachColumnForValue = new XmlElement("foreach");
        foreachColumnForValue.addAttribute(new Attribute("collection", "columns"));
        foreachColumnForValue.addAttribute(new Attribute("index", "index"));
        foreachColumnForValue.addAttribute(new Attribute("item", "column"));
        List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
        for (IntrospectedColumn introspectedColumn : columns) {
            String columnName = introspectedColumn.getActualColumnName();
            XmlElement check = new XmlElement("if");
            check.addAttribute(new Attribute("test", "'" + columnName + "' == column"));
            check.addElement(new TextElement("#{record." + introspectedColumn.getJavaProperty() + ",jdbcType=" + introspectedColumn.getJdbcTypeName() + "},"));
            foreachColumnForValue.addElement(check);
        }
        valueTrimElement.addElement(foreachColumnForValue);
        XmlElement foreachElement = new XmlElement("foreach");
        foreachElement.addAttribute(new Attribute("collection", "records"));
        foreachElement.addAttribute(new Attribute("index", "index"));
        foreachElement.addAttribute(new Attribute("item", "record"));
        foreachElement.addAttribute(new Attribute("separator", ","));
        foreachElement.addElement(valueTrimElement);
        insertBatchElement.addElement(foreachElement);
        document.getRootElement().addElement(insertBatchElement);
    }
}

自动生成limit分页

import java.util.List;
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.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

public class MySQLLimitPlugin
        extends PluginAdapter
{
    public boolean validate(List<String> list)
    {
        return true;
    }

    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable)
    {
        PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper();

        Field limit = new Field();
        limit.setName("limit");
        limit.setVisibility(JavaVisibility.PRIVATE);
        limit.setType(integerWrapper);
        topLevelClass.addField(limit);

        Method setLimit = new Method();
        setLimit.setVisibility(JavaVisibility.PUBLIC);
        setLimit.setName("setLimit");
        setLimit.addParameter(new Parameter(integerWrapper, "limit"));
        setLimit.addBodyLine("this.limit = limit;");
        topLevelClass.addMethod(setLimit);

        Method getLimit = new Method();
        getLimit.setVisibility(JavaVisibility.PUBLIC);
        getLimit.setReturnType(integerWrapper);
        getLimit.setName("getLimit");
        getLimit.addBodyLine("return limit;");
        topLevelClass.addMethod(getLimit);

        Field offset = new Field();
        offset.setName("offset");
        offset.setVisibility(JavaVisibility.PRIVATE);
        offset.setType(integerWrapper);
        topLevelClass.addField(offset);

        Method setOffset = new Method();
        setOffset.setVisibility(JavaVisibility.PUBLIC);
        setOffset.setName("setOffset");
        setOffset.addParameter(new Parameter(integerWrapper, "offset"));
        setOffset.addBodyLine("this.offset = offset;");
        topLevelClass.addMethod(setOffset);

        Method getOffset = new Method();
        getOffset.setVisibility(JavaVisibility.PUBLIC);
        getOffset.setReturnType(integerWrapper);
        getOffset.setName("getOffset");
        getOffset.addBodyLine("return offset;");
        topLevelClass.addMethod(getOffset);

        return true;
    }

    @Override
    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable)
    {
        XmlElement ifLimitNotNullElement = new XmlElement("if");
        ifLimitNotNullElement.addAttribute(new Attribute("test", "limit != null"));

        XmlElement ifOffsetNotNullElement = new XmlElement("if");
        ifOffsetNotNullElement.addAttribute(new Attribute("test", "offset != null"));
        ifOffsetNotNullElement.addElement(new TextElement("limit ${offset}, ${limit}"));
        ifLimitNotNullElement.addElement(ifOffsetNotNullElement);

        XmlElement ifOffsetNullElement = new XmlElement("if");
        ifOffsetNullElement.addAttribute(new Attribute("test", "offset == null"));
        ifOffsetNullElement.addElement(new TextElement("limit ${limit}"));
        ifLimitNotNullElement.addElement(ifOffsetNullElement);

        element.addElement(ifLimitNotNullElement);

        return true;
    }
}

使用方法

<plugin type="com.***.***.plugin.MySQLLimitPlugin">plugin>
<plugin type="com.***.***.plugin.BatchInsertPlugin">plugin>
<commentGenerator type="com.***.***.plugin.MyCommentGenerator">
    
    

    
    <property name="suppressDate" value="true"/>
commentGenerator>

如下图
MyBatis自动生成插件 生成批量插入 生成PO注释 自动分页limit_第1张图片

仅供参考,多谢支持

你可能感兴趣的:(mybaits,自动生成)