【Mybatis】Mybatis-Generator-Plugins-Java&XmlMethod生成工具

系列

  1. 【Mybatis】Mybatis-Generator-Postgresql返回主键插件
  2. 【Mybatis】Mybatis-Generator-Oracle返回主键插件
  3. 【Mybatis】Mybatis-Generator-batchDelete(批量删除)方法插件
  4. 【Mybatis】Mybatis-Generator-Postgresql&Mysql(批量新增)方法插件
  5. 【Mybatis】Mybatis-Generator-Oracle(批量新增)方法插件
  6. 【Mybatis】Mybatis-Generator-Mysql(批量更新)方法插件
  7. 【Mybatis】Mybatis-Generator-Postgresql(批量更新)方法插件
  8. 【Mybatis】Mybatis-Generator-Oracle(批量更新)方法插件
  9. 【Mybatis】Mybatis-Generator-Tool 生成Java和Xml的Method工具类

Github

地址:https://github.com/ithuhui/hui-mybatis-generator-plugins
分支:master
位置:com.hui.mybatis.tools

Note

Mybatis-Generator-Tool 生成Java和Xml的Method工具类。

github上针对mybatis-generator还有很多不同类型的插件,我实在懒的看,而且太多复杂的功能,我简单点搞自己需要的。

Code

maven

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.6</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.7</version>
    </dependency>
</dependencies>

MethodGeneratorTool

/**
 * <b><code>MethodGeneratorTool</code></b>
 * <p/>
 * Description: 方法生成器
 * <p/>
 * <b>Creation Time:</b> 2018/12/14 0:50.
 *
 * @author HuWeihui
 */
public class MethodGeneratorTool {

    private final static String BATCH_INSERT = "batchInsert";

    private final static String PARAMETER_NAME = "recordList";

    private final static String DELETE_PARAMETER_NAME = "ids";

    private final static String BATCH_UPDATE = "batchUpdate";

    private final static String BATCH_DELETE = "batchDelete";

    public final static Integer INSERT = 0;

    public final static Integer UPDATE = 1;
    /**
     * java方法生成构造器.
     *
     * @param methodName     the method name
     * @param visibility     the visibility
     * @param returnJavaType the return java type
     * @param parameters     the parameters
     * @author HuWeihui
     * @since hui_project v1
     */
    public static Method methodGenerator(String methodName,
                                       JavaVisibility visibility,
                                       FullyQualifiedJavaType returnJavaType,
                                       Parameter... parameters) {
        Method method = new Method();
        method.setName(methodName);
        method.setVisibility(visibility);
        method.setReturnType(returnJavaType);
        for (Parameter parameter : parameters) {
            method.addParameter(parameter);
        }
        return method;
    }

    /**
     * 导入基础的java类型
     *
     * @param introspectedTable the introspected table
     * @return the set
     * @author HuWeihui
     * @since hui_project v1
     */
    public static Set<FullyQualifiedJavaType> importedBaseTypesGenerator(IntrospectedTable introspectedTable){
        //获取实体类类型
        FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();
        //@Param需要导入的类型
        FullyQualifiedJavaType paramType = new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param");
        //Integer类型
        FullyQualifiedJavaType intInstance = FullyQualifiedJavaType.getIntInstance();
        //List<Entity>
        FullyQualifiedJavaType listParameterType = FullyQualifiedJavaType.getNewListInstance();

        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
        importedTypes.add(parameterType);
        importedTypes.add(intInstance);
        importedTypes.add(paramType);
        importedTypes.add(listParameterType);
        return importedTypes;
    }

    /**
     * 默认的批量新增/更新方法构造器.
     *
     * @param interfaze         the interfaze
     * @param introspectedTable the introspected table
     * @param context           the context
     * @author HuWeihui
     * @since hui_project v1
     */
    public static void defaultBatchInsertOrUpdateMethodGen(Integer type ,Interface interfaze,IntrospectedTable introspectedTable, Context context){
        //JAVA导入基础包
        Set<FullyQualifiedJavaType> importedTypes = MethodGeneratorTool.importedBaseTypesGenerator(introspectedTable);

        //List<Entity>
        FullyQualifiedJavaType listParameterType = FullyQualifiedJavaType.getNewListInstance();
        listParameterType.addTypeArgument(introspectedTable.getRules().calculateAllFieldsClass());

        String methodName = BATCH_INSERT;
        //1.batchInsert
        if (type.equals(UPDATE)){
            methodName = BATCH_UPDATE;
        }
        Method insertMethod = MethodGeneratorTool.methodGenerator(methodName,
                JavaVisibility.DEFAULT,
                FullyQualifiedJavaType.getIntInstance(),
                new Parameter(listParameterType, PARAMETER_NAME, "@Param(\"" + PARAMETER_NAME + "\")"));

        CommentGenerator commentGenerator = context.getCommentGenerator();
        commentGenerator.addGeneralMethodComment(insertMethod, introspectedTable);

        interfaze.addImportedTypes(importedTypes);
        interfaze.addMethod(insertMethod);
    }

    /**
     * 默认的批量删除方法构造器.
     *
     * @param interfaze         the interfaze
     * @param introspectedTable the introspected table
     * @param context           the context
     * @author HuWeihui
     * @since hui_project v1
     */
    public static void defaultBatchDeleteMethodGen(Interface interfaze,IntrospectedTable introspectedTable, Context context){
        //JAVA导入基础包
        Set<FullyQualifiedJavaType> importedTypes = MethodGeneratorTool.importedBaseTypesGenerator(introspectedTable);
        FullyQualifiedJavaType paramType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType();
        Method batchDeleteMethod = MethodGeneratorTool.methodGenerator(BATCH_DELETE,
                JavaVisibility.DEFAULT,
                FullyQualifiedJavaType.getIntInstance(),
                new Parameter(new FullyQualifiedJavaType(paramType.getFullyQualifiedName()+"[]"), DELETE_PARAMETER_NAME, "@Param(\""+DELETE_PARAMETER_NAME+"\")"));

        context.getCommentGenerator().addGeneralMethodComment(batchDeleteMethod,introspectedTable);
        interfaze.addImportedTypes(importedTypes);
        interfaze.addMethod(batchDeleteMethod);
    }

}

SqlMapperGeneratorTool

/**
 * <b><code>SqlMapperGeneratorTool</code></b>
 * <p/>
 * Description: 一些基础XMLELEMENT的构造器
 * <p/>
 * <b>Creation Time:</b> 2018/12/15 18:40.
 *
 * @author HuWeihui
 */
public class SqlMapperGeneratorTool {

    /**
     * The constant INSERT.
     */
    public static final String INSERT = "insert";

    /**
     * The constant DELETE.
     */
    public static final String DELETE = "delete";

    /**
     * The constant UPDATE.
     */
    public static final String UPDATE = "update";

    /**
     * The constant SELECT.
     */
    public static final String SELECT = "select";


    /**
     * 基础XmlElement构造器.
     *
     * @param sqlElementType the sql element type
     * @param sqlMapperId    the sql mapper id
     * @param parameterType  the parameter type
     * @return the xml element
     * @author HuWeihui
     * @since hui_project v1
     */
    public static XmlElement baseElementGenerator(String sqlElementType, String sqlMapperId,FullyQualifiedJavaType parameterType){


        XmlElement baseElement = new XmlElement(sqlElementType);

        baseElement.addAttribute(new Attribute("id", sqlMapperId));

        baseElement.addAttribute(new Attribute("parameterType", parameterType.getFullyQualifiedName()));

        return baseElement;
    }


    /**
     *基础foreach Element构造器.
     *
     * @param collectionName the collection name
     * @param itemName       the item name
     * @param indexName      the index name
     * @param separatorName  the separator name
     * @return the xml element
     * @author HuWeihui
     * @since hui_project v1
     */
    public static XmlElement baseForeachElementGenerator(String collectionName,String itemName,String indexName ,String separatorName){
        XmlElement foreachElement = new XmlElement("foreach");
        if (null!=collectionName){
            foreachElement.addAttribute(new Attribute("collection", collectionName));
        }
        if (null!=itemName){
            foreachElement.addAttribute(new Attribute("item", itemName));
        }
        if (null!=indexName){
            foreachElement.addAttribute(new Attribute("index", indexName));
        }
        if (null!=separatorName){
            foreachElement.addAttribute(new Attribute("separator", separatorName));
        }
        return foreachElement;
    }


    /**
     * 基础IF Element构造器.
     *
     * @param columnJavaTypeName the column java type name
     * @param sql                the sql
     * @param judgeNull          the judge null
     * @return the xml element
     * @author HuWeihui
     * @since hui_project v1
     */
    public static XmlElement baseIfJudgeElementGen(String columnJavaTypeName,String sql ,boolean judgeNull){
        String colmunJudge = "";
        if (judgeNull){
            colmunJudge = columnJavaTypeName + " ==null ";
        }else {
            colmunJudge = columnJavaTypeName + " !=null ";
        }
        XmlElement ifElement = new XmlElement("if");
        ifElement.addAttribute(new Attribute("test", colmunJudge));
        ifElement.addElement(new TextElement(sql));
        return ifElement;
    }

    /**
     * 基础Trim Element构造器.
     *
     * @param prefix          the prefix
     * @param suffix          the suffix
     * @param suffixOverrides the suffix overrides
     * @return the xml element
     * @author HuWeihui
     * @since hui_project v1
     */
    public static XmlElement baseTrimElement(String prefix,String suffix,String suffixOverrides){
        XmlElement trimElement = new XmlElement("trim");
        if (null != prefix){
            trimElement.addAttribute(new Attribute("prefix", prefix));
        }
        if (null != suffix){
            trimElement.addAttribute(new Attribute("suffix", suffix));
        }
        if (null!=suffixOverrides){
            trimElement.addAttribute(new Attribute("suffixOverrides", suffixOverrides));
        }
        return trimElement;
    }
}

Author

 作者:HuHui
 转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值