计算机系统应用教程网站

网站首页 > 技术文章 正文

代码演示Mybatis-Generator 扩展自定义生成(上)

btikc 2024-09-29 09:59:05 技术文章 15 ℃ 0 评论

Mybatis-Generator 可自动生成Model、Dao、Mapper代码,但其自带生成的代码存在以下问题:

  • 生成的注释不是我们想要的,我们期望的是根据数据库表、字段生成不同的注释;
  • 分页代码生成缺失,每个公司的分页方式不同,尤其是老久项目或已发布API,不能随意变动,那么如何自适应分页代码生成;
  • Mapper.xml没有group by相关代码生成;
  • 重复生成代码时,Mapper.xml并不是覆盖原代码;而是对内容进行了追加;
  • 序列化,mybatis-generator内置了SerializablePlugin,但仅对Model,并没有对 Example序列化,在一些开发中是不够的;
  • 对Service Layer代码没有生成。

实际上,mybatis-generator提供了PluginAdapter供我们来继承,进行个性化的一些扩展(Plugin的相关内容是阅读本文的前置条件)如果不熟悉的同学请自行补充,本文不对其进行相关介绍。同时,本文不可能涵盖所有业务所需的扩展点,基本样板已有,可参考本文代码继续进行扩展。

一、注释的自定义生成


根据数据库表或字段的COMMENT生成注释。@Date 生成的时间可根据需要自己定义格式。

package run.override;
import java.util.Date;
import java.util.Properties;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.InnerEnum;
import org.mybatis.generator.api.dom.java.JavaElement;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility;
/**
 * Comment Generator
 * @ClassName CommentGenerator
 * @Description
 * @author Marvis
 */
public class CommentGenerator extends DefaultCommentGenerator {
    private Properties properties;
    private boolean suppressDate;
    private boolean suppressAllComments;

    public CommentGenerator() {
        this.properties = new Properties();
        this.suppressDate = false;
        this.suppressAllComments = false;
    }

    public void addJavaFileComment(CompilationUnit compilationUnit) {
        
        compilationUnit.addFileCommentLine("/*** copyright (c) 2019 Marvis  ***/");
    }
    /**
     * XML file Comment
     */
    public void addComment(XmlElement xmlElement) {
        if (this.suppressAllComments) {
            return;
        }

    }

    public void addRootComment(XmlElement rootElement) {
    }

    public void addConfigurationProperties(Properties properties) {
        this.properties.putAll(properties);

        this.suppressDate = StringUtility.isTrue(properties.getProperty("suppressDate"));

        this.suppressAllComments = StringUtility.isTrue(properties.getProperty("suppressAllComments"));
    }

    protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {
        StringBuilder sb = new StringBuilder();
        sb.append(" * ");
        sb.append("@date");
        String s = getDateString();
        if (s != null) {
            sb.append(' ');
            sb.append(s);
        }
        javaElement.addJavaDocLine(sb.toString());
    }

    protected String getDateString() {
        if (this.suppressDate) {
            return null;
        }
        return new Date().toString();
    }
    /**
     *  Comment of Example inner class(GeneratedCriteria ,Criterion)
     */
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
        if (this.suppressAllComments) {
            return;
        }

        innerClass.addJavaDocLine("/**");
        innerClass.addJavaDocLine(" * " + introspectedTable.getFullyQualifiedTable().getDomainObjectName()+ "<p/>");
        innerClass.addJavaDocLine(" * " + introspectedTable.getFullyQualifiedTable().toString());
        addJavadocTag(innerClass, false);
        innerClass.addJavaDocLine(" */");
    }

    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
        if (this.suppressAllComments) {
            return;
        }

        StringBuilder sb = new StringBuilder();

        innerEnum.addJavaDocLine("/**");
        innerEnum.addJavaDocLine(" * " + introspectedTable.getFullyQualifiedTable().getAlias()+ "<p/>");
        innerEnum.addJavaDocLine(" * " + introspectedTable.getFullyQualifiedTable());
        innerEnum.addJavaDocLine(sb.toString());

        addJavadocTag(innerEnum, false);

        innerEnum.addJavaDocLine(" */");
    }
    /**
     * entity filed Comment
     */
    public void addFieldComment(Field field, IntrospectedTable introspectedTable,
            IntrospectedColumn introspectedColumn) {
        if (this.suppressAllComments) {
            return;
        }

//      if(introspectedColumn.getRemarks() != null && !introspectedColumn.getRemarks().trim().equals(""))
        
        field.addJavaDocLine("/**");
        field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
        field.addJavaDocLine(" * @author " );
        field.addJavaDocLine(" * @date " + getDateString() );
        field.addJavaDocLine(" * @return");
        field.addJavaDocLine(" */");
    }
    /**
     *  Comment of EXample filed
     */
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
        if (this.suppressAllComments) {
            return;
        }
        field.addJavaDocLine("/**");
        addJavadocTag(field, false);
        field.addJavaDocLine(" */");
    }
    /**
     * Comment of Example method
     */
    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
        if (this.suppressAllComments) {
            return;
        }
    }
    /**
     *
     * entity Getter Comment
     */
    public void addGetterComment(Method method, IntrospectedTable introspectedTable,
            IntrospectedColumn introspectedColumn) {
        if (this.suppressAllComments) {
            return;
        }
        method.addJavaDocLine("/**");

        
        method.addJavaDocLine(" * @return " + introspectedTable.getFullyQualifiedTable().getAlias() + " : " + introspectedColumn.getRemarks());
        method.addJavaDocLine(" */");
    }

    public void addSetterComment(Method method, IntrospectedTable introspectedTable,
            IntrospectedColumn introspectedColumn) {
        if (this.suppressAllComments) {
            return;
        }

        StringBuilder sb = new StringBuilder();

        method.addJavaDocLine("/**");

        Parameter parm = (Parameter) method.getParameters().get(0);
        sb.append(" * @param ");
        sb.append(parm.getName());
        sb.append(" : ");
        sb.append(introspectedColumn.getRemarks());
        method.addJavaDocLine(sb.toString());
        method.addJavaDocLine(" */");
    }
    
    /**
     * Comment of Example inner class(Criteria)
     */
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
        if (this.suppressAllComments) {
            return;
        }

        innerClass.addJavaDocLine("/**");
        innerClass.addJavaDocLine(" * " + introspectedTable.getFullyQualifiedTable().getAlias()+ "<p/>");
        innerClass.addJavaDocLine(" * " + introspectedTable.getFullyQualifiedTable().toString());
        addJavadocTag(innerClass, markAsDoNotDelete);

        innerClass.addJavaDocLine(" */");
    }

Model 类注释(表的描述): MySQL。

1)EntityCommentPlugin

package run.override.model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;

import org.mybatis.generator.api.FullyQualifiedTable;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.JDBCConnectionFactory;
import org.mybatis.generator.internal.util.StringUtility;

/**
 * Comment of Entity,only support MySQL
 * @ClassName CommentPlugin
 * @Description
 * @author Marvis
 */
public class EntityCommentPlugin extends PluginAdapter {
        
    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        addModelClassComment(topLevelClass, introspectedTable);
        return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
    }

    @Override
    public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass,
            IntrospectedTable introspectedTable) {

        addModelClassComment(topLevelClass, introspectedTable);
        return super.modelRecordWithBLOBsClassGenerated(topLevelClass, introspectedTable);
    }

    protected void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

        FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
        String tableComment = getTableComment(table);

        topLevelClass.addJavaDocLine("/**");
        if(StringUtility.stringHasValue(tableComment))
            topLevelClass.addJavaDocLine(" * " + tableComment + "<p/>");
        topLevelClass.addJavaDocLine(" * " + table.toString() + "<p/>");
        topLevelClass.addJavaDocLine(" * @date " + new Date().toString());
        topLevelClass.addJavaDocLine(" *");
        topLevelClass.addJavaDocLine(" */");
    }

    /**
     * @author Marvis
     * @date Jul 13, 2017 4:39:52 PM
     * @param table
     */
    private String getTableComment(FullyQualifiedTable table) {
        String tableComment = "";
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            JDBCConnectionFactory jdbc = new JDBCConnectionFactory(context.getJdbcConnectionConfiguration());
            connection = jdbc.getConnection();
            statement = connection.createStatement();
            rs = statement.executeQuery("SHOW CREATE TABLE " + table.getIntrospectedTableName());

            if (rs != null && rs.next()) {
                String createDDL = rs.getString(2);
                int index = createDDL.indexOf("COMMENT='");
                if (index < 0) {
                    tableComment = "";
                } else {
                    tableComment = createDDL.substring(index + 9);
                    tableComment = tableComment.substring(0, tableComment.length() - 1);
                }
            }

        } catch (SQLException e) {

        } finally {
            closeConnection(connection, statement, rs);
        }
        return tableComment;
    }
    /**
     *
     * @author Marvis
     * @date Jul 13, 2017 4:45:26 PM
     * @param connection
     * @param statement
     * @param rs
     */
    private void closeConnection(Connection connection, Statement statement, ResultSet rs) {
        try {
            if (null != rs)
                rs.close();
        } catch (SQLException e) {

            e.printStackTrace();
        } finally {
            try {
                if (statement != null)
                    statement.close();
            } catch (Exception e) {
                e.printStackTrace();

            } finally {

                try {
                    if (connection != null)
                        connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * This plugin is always valid - no properties are required
     */
    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }
}


二、分页和分组代码生成


这里,我对Dao Model进行了通用方法的抽取,建立通用基类。同时,对其进行了一些扩展,增加分页和分组。

先对基类进行介绍。

1)BaseMapper

package cn.xxx.core.base.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

public interface BaseMapper<T, Example, ID> {
    
    long countByExample(Example example);

    int deleteByExample(Example example);

    int deleteByPrimaryKey(ID id);

    int insert(T record);

    int insertSelective(T record);

    List<T> selectByExample(Example example);

    T selectByPrimaryKey(ID id);

    int updateByExampleSelective(@Param("record") T record, @Param("example") Example example);

    int updateByExample(@Param("record") T record, @Param("example") Example example);

    int updateByPrimaryKeySelective(T record);

    int updateByPrimaryKey(T record);

}

2)BaseExample

package cn.xxx.core.base.model;
/**
 * BaseExample 基类
 * @ClassName BaseExample
 * @Description 增加分页参数
 * @author Marvis
 * @date Jul 31, 2017 11:26:53 AM
 */
public abstract class BaseExample {

    protected PageInfo pageInfo;
    protected String groupByClause;

    public PageInfo getPageInfo() {
        return pageInfo;
    }

    public void setPageInfo(PageInfo pageInfo) {
        this.pageInfo = pageInfo;
    }

    public String getGroupByClause() {
        return groupByClause;
    }

    public void setGroupByClause(String groupByClause) {
        this.groupByClause = groupByClause;
    }
    
}

3)PageInfo

package cn.xxx.core.base.model;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 分页查询参数类
 *
 * @author
 *
 */
public class PageInfo {

    public static final int Default_PageSize = 20;

    // 当前页码
    protected int currentPage = 1;

    // 总页数
    protected int totalPage;

    // 总记录数
    protected int totalCount;

    // 每页条数
    protected int pageSize = Default_PageSize;

    // 开始
    protected int pageBegin = 0;

    // 结束
    protected int pageEnd = 20;

    /**
     * bean起始坐标(不包含)
     */
    private Integer pageBeginId = null;

    public static final String PageQuery_classname = "pageInfo";

    /**
     * 将分布参数传入处理,最终计算出当前页码PageQuery_currPage,开始坐标PageQuery_star,
     * 结束坐标PageQuery_end,总页数PageQuery_Psize
     * <p/>
     * 页数从1开始计数
     *
     * @param totalCount
     *            记录总数
     * @param pageSize
     *            每页显示个数
     * @param currentPage
     *            当前页码
     */
    public void setPageParams(int totalCount, int pageSize, int currentPage) {

        this.totalPage = pageSize == 0 ? 1 : (int) Math.ceil((double) totalCount / (double) pageSize);

        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.currentPage = currentPage;

        float Psize_l = totalCount / (float) (this.pageSize);
        if (currentPage < 2) {
            currentPage = 1;
            pageBegin = 0;
        } else if (currentPage > Psize_l) {
            if (Psize_l == 0) {
                currentPage = 1;
            } else {
                currentPage = (int) Math.ceil(Psize_l);
            }

            pageBegin = (currentPage - 1) * this.pageSize;
        } else {
            pageBegin = (currentPage - 1) * this.pageSize;
        }
        pageSize = (int) Math.ceil(Psize_l);
        this.pageEnd = currentPage * this.pageSize;

        if (this.currentPage <= 0 || this.currentPage > this.totalPage)
            this.pageSize = 0;
    }

    /**
     * 将分布参数传入处理,最终计算出当前页码PageQuery_currPage,开始坐标PageQuery_star,
     * 结束坐标PageQuery_end,总页数PageQuery_Psize
     *
     * @param infoCount
     *            记录总数
     */
    public void setPageParams(int totalCount) {
        this.setPageParams(totalCount, this.pageSize, this.currentPage);
    }

    @Override
    public String toString() {
        return "PageInfo [currentPage=" + currentPage + ", totalPage=" + totalPage + ", totalCount=" + totalCount
                + ", pageSize=" + pageSize + ", pageBegin=" + pageBegin + ", pageEnd=" + pageEnd + ", pageBeginId="
                + pageBeginId + "]";
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public int getTotalCount() {
        return totalCount;
    }

    /**
     * 每页显示个数
     */
    public int getPageSize() {
        return pageSize;
    }

    @JsonIgnore
    public int getPageBegin() {
        return pageBegin;
    }

    @JsonIgnore
    public int getPageEnd() {
        return pageEnd;
    }

    /**
     * bean起始id(不包含)
     */
    @JsonIgnore
    public Integer getPageBeginId() {
        return pageBeginId;
    }

    /**
     * 请求页
     */
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    /**
     * 每页显示个数
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
}

4)PaginationPlugin

分页扩展。并且Example继承BaseExample。

package run.override.pagination;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
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;

import run.override.mapper.SqlMapIsMergeablePlugin;
import run.override.proxyFactory.FullyQualifiedJavaTypeProxyFactory;

public class PaginationPlugin extends SqlMapIsMergeablePlugin {
    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

        FullyQualifiedJavaType baseExampleType = FullyQualifiedJavaTypeProxyFactory.getBaseExampleInstance();
        topLevelClass.setSuperClass(baseExampleType);
        
        topLevelClass.addImportedType(baseExampleType);
        return super.modelExampleClassGenerated(topLevelClass, introspectedTable);
    }
    
    @Override
    public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element,
            IntrospectedTable introspectedTable) {

        XmlElement isNotNullElement1 = new XmlElement("if");
        isNotNullElement1.addAttribute(new Attribute("test", "groupByClause != null"));
        isNotNullElement1.addElement(new TextElement("group by ${groupByClause}"));
        element.addElement(5, isNotNullElement1);
        XmlElement isNotNullElement = new XmlElement("if");
        isNotNullElement.addAttribute(new Attribute("test", "pageInfo != null"));
        isNotNullElement.addElement(new TextElement("limit #{pageInfo.pageBegin} , #{pageInfo.pageSize}"));
        element.addElement(isNotNullElement);

        return super.sqlMapUpdateByExampleWithBLOBsElementGenerated(element, introspectedTable);
    }

    @Override
    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
            IntrospectedTable introspectedTable) {

        XmlElement isNotNullElement1 = new XmlElement("if");
        isNotNullElement1.addAttribute(new Attribute("test", "groupByClause != null"));
        isNotNullElement1.addElement(new TextElement("group by ${groupByClause}"));
        element.addElement(5, isNotNullElement1);

        XmlElement isNotNullElement = new XmlElement("if");
        isNotNullElement.addAttribute(new Attribute("test", "pageInfo != null"));
        isNotNullElement.addElement(new TextElement("limit #{pageInfo.pageBegin} , #{pageInfo.pageSize}"));
        element.addElement(isNotNullElement);

        return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
    }

    @Override
    public boolean sqlMapCountByExampleElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {

        XmlElement answer = new XmlElement("select");

        String fqjt = introspectedTable.getExampleType();

        answer.addAttribute(new Attribute("id", introspectedTable.getCountByExampleStatementId()));
        answer.addAttribute(new Attribute("parameterType", fqjt));
        answer.addAttribute(new Attribute("resultType", "java.lang.Integer"));

        this.context.getCommentGenerator().addComment(answer);

        StringBuilder sb = new StringBuilder();
        sb.append("select count(1) from ");
        sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());

        XmlElement ifElement = new XmlElement("if");
        ifElement.addAttribute(new Attribute("test", "_parameter != null"));
        XmlElement includeElement = new XmlElement("include");
        includeElement.addAttribute(new Attribute("refid", introspectedTable.getExampleWhereClauseId()));
        ifElement.addElement(includeElement);

        element.getElements().clear();
        element.getElements().add(new TextElement(sb.toString()));
        element.getElements().add(ifElement);
        return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
    }
}

5)FullyQualifiedJavaTypeProxyFactory

package run.override.proxyFactory;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;

public class FullyQualifiedJavaTypeProxyFactory  extends FullyQualifiedJavaType{
    
    private static FullyQualifiedJavaType pageInfoInstance = new FullyQualifiedJavaType("cn.xxx.core.base.model.PageInfo");
    private static FullyQualifiedJavaType baseExampleInstance = new FullyQualifiedJavaType("cn.xxx.core.base.model.BaseExample");
    private static FullyQualifiedJavaType baseMapperInstance = new FullyQualifiedJavaType("cn.xxx.core.base.dao.BaseMapper");
    private static FullyQualifiedJavaType baseServiceInstance = new FullyQualifiedJavaType("cn.xxx.core.base.service.BaseService");
    private static FullyQualifiedJavaType baseServiceImplInstance = new FullyQualifiedJavaType("cn.xxx.core.base.service.impl.BaseServiceImpl");
    
    public FullyQualifiedJavaTypeProxyFactory(String fullTypeSpecification) {
        super(fullTypeSpecification);
    }
    
    public static final FullyQualifiedJavaType getPageInfoInstanceInstance() {

        return pageInfoInstance;
    }
    public static final FullyQualifiedJavaType getBaseExampleInstance() {
        
        return baseExampleInstance;
    }
    
    public static final FullyQualifiedJavaType getBaseMapperInstance() {
        
        return baseMapperInstance;
    }
    public static final FullyQualifiedJavaType getBaseServiceInstance() {
        
        return baseServiceInstance;
    }
    public static final FullyQualifiedJavaType getBaseServiceImplInstance() {
        
        return baseServiceImplInstance;
    }
}


三、Dao生成代码简化


1)ClientDaoPlugin

package run.override.dao;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.JavaTypeResolver;
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.types.JavaTypeResolverDefaultImpl;

import run.override.model.EntityCommentPlugin;
import run.override.proxyFactory.FullyQualifiedJavaTypeProxyFactory;

/**
 * javaClient("XMLMAPPER") extended
 *
 * @ClassName ClientDaoPlugin
 * @Description Mapper.java
 * @author Marvis
 */
public class ClientDaoPlugin extends EntityCommentPlugin {

    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass,
            IntrospectedTable introspectedTable) {

        JavaTypeResolver javaTypeResolver = new JavaTypeResolverDefaultImpl();
        FullyQualifiedJavaType calculateJavaType = javaTypeResolver
                .calculateJavaType(introspectedTable.getPrimaryKeyColumns().get(0));

        FullyQualifiedJavaType superInterfaceType = new FullyQualifiedJavaType(
                new StringBuilder("BaseMapper<")
                    .append(introspectedTable.getBaseRecordType())
                    .append(",")
                    .append(introspectedTable.getExampleType())
                    .append(",")
                    .append(calculateJavaType.getShortName())
                    .append(">")
                    .toString()
                );
        FullyQualifiedJavaType baseMapperInstance = FullyQualifiedJavaTypeProxyFactory.getBaseMapperInstance();

        interfaze.addSuperInterface(superInterfaceType);
        interfaze.addImportedType(baseMapperInstance);

        List<Method> changeMethods = interfaze.getMethods().stream()
                .filter(method -> method.getName().endsWith("WithBLOBs")
                        || method.getReturnType().toString().endsWith("WithBLOBs")
                        || Arrays.toString(method.getParameters().toArray()).contains("WithBLOBs"))
                .collect(Collectors.toList());

        interfaze.getMethods().retainAll(changeMethods);

        if (changeMethods.isEmpty())
            interfaze.getImportedTypes().removeIf(javaType -> javaType.getFullyQualifiedName().equals("java.util.List")
                    || javaType.getFullyQualifiedName().equals("org.apache.ibatis.annotations.Param"));

        return super.clientGenerated(interfaze, topLevelClass, introspectedTable);
    }

}


四、修正


重复生成时Mapper.xml不是覆盖原代码,而是对内容进行了追加。

1)SqlMapIsMergeablePlugin

package run.override.mapper;

import org.mybatis.generator.api.GeneratedXmlFile;
import org.mybatis.generator.api.IntrospectedTable;
import run.override.dao.ClientDaoPlugin;

public class SqlMapIsMergeablePlugin extends ClientDaoPlugin {

    @Override
    public boolean sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) {
        //重新生成代码,xml内容覆盖
        sqlMap.setMergeable(false);
        return super.sqlMapGenerated(sqlMap, introspectedTable);
    }
}


五、序列化自定义扩展


增加Example的序列化,并增加@SuppressWarnings("serial")注解。

1)SerializablePlugin

package run.override;

import java.util.List;
import java.util.Properties;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.TopLevelClass;

public class SerializablePlugin extends PluginAdapter {
    private FullyQualifiedJavaType serializable;
    private FullyQualifiedJavaType gwtSerializable;
    private boolean addGWTInterface;
    private boolean suppressJavaInterface;

    public SerializablePlugin() {
        this.serializable = new FullyQualifiedJavaType("java.io.Serializable");
        this.gwtSerializable = new FullyQualifiedJavaType("com.google.gwt.user.client.rpc.IsSerializable");
    }

    @Override
    public void setProperties(Properties properties) {
        super.setProperties(properties);
        this.addGWTInterface = Boolean.valueOf(properties.getProperty("addGWTInterface")).booleanValue();
        this.suppressJavaInterface = Boolean.valueOf(properties.getProperty("suppressJavaInterface")).booleanValue();
    }
    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        makeSerializable(topLevelClass, introspectedTable);
        return true;
    }
    @Override
    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        makeSerializable(topLevelClass, introspectedTable);
        return true;
    }
    @Override
    public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass,
            IntrospectedTable introspectedTable) {
        makeSerializable(topLevelClass, introspectedTable);
        return true;
    }
    
    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,IntrospectedTable introspectedTable){
        makeSerializable(topLevelClass, introspectedTable);
        return true;
    }

    protected void makeSerializable(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        if (this.addGWTInterface) {
            topLevelClass.addImportedType(this.gwtSerializable);
            topLevelClass.addSuperInterface(this.gwtSerializable);
        }

        if (!(this.suppressJavaInterface)) {
            topLevelClass.addImportedType(this.serializable);
            topLevelClass.addSuperInterface(this.serializable);
            topLevelClass.addAnnotation("@SuppressWarnings(\"serial\")");
            
        }
    }
    
    /**
     * This plugin is always valid - no properties are required
     */
    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }
}

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表