zoukankan      html  css  js  c++  java
  • 修改mybatis plus Generator模板生成字段注释枚举常量

    修改mybatis plus Generator模板生成字段注释枚举常量

    本文基于最新的mybatis-plus 3.0.1版本源码修改,如果使用其它版本,处理方式也类似,主要是生成Entity的FreekMarker模板文件的修改。
    源码下载:https://gitee.com/baomidou/mybatis-plus


    目标:

    表的字段定义如下:
    `name_type` int(1) DEFAULT NULL COMMENT '名字类型{Girl:0,女孩;boy:1,男孩;other:2,其它}'

    生成的实体中有如下内容:
    /*
    * name_type : 女孩
    */
    private final static INTEGER NAME_TYPE_GIRL = 0;
    
    /*
    * name_type : 男孩
    */
    private final static INTEGER NAME_TYPE_BOY = 1;
    
    /*
    * name_type : 其它
     */
    private final static INTEGER NAME_TYPE_OTHER= 2;

    主要的模板代码如下:

    <#-- 注释的枚举常量生成,例如字段内容为:`name_type` int(1) DEFAULT NULL COMMENT '名字类型{Girl0,女孩;boy1,男孩;other2,其它}' -->
        <#if field.comment!?length gt 0>
            <#if field.comment?contains("{") && field.comment?contains("}") && field.comment?contains(";") && field.comment?contains(",") >
                <#assign object_str = field.comment?substring(field.comment?index_of("{")+1, field.comment?index_of("}"))/>
                <#list object_str?split(";") as comment_element>
                    <#assign firstName = comment_element?substring(0, comment_element?index_of(":"))/>
                    <#assign lastName = comment_element?substring(comment_element?index_of(":")+1, comment_element?index_of(","))/>
                    <#assign meanName = comment_element?keep_after(",")/>
                    <#assign fname = field.propertyName/>
        /**
        * ${field.propertyName}:${meanName}
        */
        public final static ${field.propertyType} ${(field.name)?upper_case}_${firstName?upper_case} = ${lastName};
                </#list>
            </#if>
        </#if>

    完整的entity.java.ftl的内容如下:

    package ${package.Entity};
    
    <#list table.importPackages as pkg>
    import ${pkg};
    </#list>
    <#if swagger2>
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    </#if>
    <#if entityLombokModel>
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.experimental.Accessors;
    </#if>
    
    /**
     * <p>
     * ${table.comment!}
     * </p>
     *
     * @author ${author}
     * @since ${date}
     */
    <#if entityLombokModel>
    @Data
        <#if superEntityClass??>
    @EqualsAndHashCode(callSuper = true)
        <#else>
    @EqualsAndHashCode(callSuper = false)
        </#if>
    @Accessors(chain = true)
    </#if>
    <#if table.convert>
    @TableName("${table.name}")
    </#if>
    <#if swagger2>
    @ApiModel(value="${entity}对象", description="${table.comment!}")
    </#if>
    <#if superEntityClass??>
    public class ${entity} extends ${superEntityClass}<#if activeRecord><${entity}></#if> {
    <#elseif activeRecord>
    public class ${entity} extends Model<${entity}> {
    <#else>
    public class ${entity} implements Serializable {
    </#if>
    
        private static final long serialVersionUID = 1L;
    <#-- ----------  BEGIN 字段循环遍历  ---------->
    <#list table.fields as field>
        <#if field.keyFlag>
            <#assign keyPropertyName="${field.propertyName}"/>
        </#if>
    
        <#if field.comment!?length gt 0>
        <#if swagger2>
        @ApiModelProperty(value = "${field.comment}")
        <#else>
        /**
         * ${field.comment}
         */
        </#if>
        </#if>
        <#if field.keyFlag>
        <#-- 主键 -->
            <#if field.keyIdentityFlag>
        @TableId(value = "${field.name}", type = IdType.AUTO)
            <#elseif idType??>
        @TableId(value = "${field.name}", type = IdType.${idType})
            <#elseif field.convert>
        @TableId("${field.name}")
            </#if>
        <#-- 普通字段 -->
        <#elseif field.fill??>
        <#-- -----   存在字段填充设置   ----->
            <#if field.convert>
        @TableField(value = "${field.name}", fill = FieldFill.${field.fill})
            <#else>
        @TableField(fill = FieldFill.${field.fill})
            </#if>
        <#elseif field.convert>
        @TableField("${field.name}")
        </#if>
    <#-- 乐观锁注解 -->
        <#if (versionFieldName!"") == field.name>
        @Version
        </#if>
    <#-- 逻辑删除注解 -->
        <#if (logicDeleteFieldName!"") == field.name>
        @TableLogic
        </#if>
        private ${field.propertyType} ${field.propertyName};
    
    <#-- 注释的枚举常量生成,例如字段内容为:`name_type` int(1) DEFAULT NULL COMMENT '名字类型{Girl0,女孩;boy1,男孩;other2,其它}' -->
        <#if field.comment!?length gt 0>
            <#if field.comment?contains("{") && field.comment?contains("}") && field.comment?contains(";") && field.comment?contains(",") >
                <#assign object_str = field.comment?substring(field.comment?index_of("{")+1, field.comment?index_of("}"))/>
                <#list object_str?split(";") as comment_element>
                    <#assign firstName = comment_element?substring(0, comment_element?index_of(":"))/>
                    <#assign lastName = comment_element?substring(comment_element?index_of(":")+1, comment_element?index_of(","))/>
                    <#assign meanName = comment_element?keep_after(",")/>
                    <#assign fname = field.propertyName/>
        /**
        * ${field.propertyName}:${meanName}
        */
        public final static ${field.propertyType} ${(field.name)?upper_case}_${firstName?upper_case} = ${lastName};
                </#list>
            </#if>
        </#if>
    </#list>
    <#------------  END 字段循环遍历  ---------->
    
    <#if !entityLombokModel>
        <#list table.fields as field>
            <#if field.propertyType == "boolean">
                <#assign getprefix="is"/>
            <#else>
                <#assign getprefix="get"/>
            </#if>
        public ${field.propertyType} ${getprefix}${field.capitalName}() {
            return ${field.propertyName};
        }
    
            <#if entityBuilderModel>
        public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
            <#else>
        public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
            </#if>
            this.${field.propertyName} = ${field.propertyName};
            <#if entityBuilderModel>
            return this;
            </#if>
        }
        </#list>
    </#if>
    
    <#if entityColumnConstant>
        <#list table.fields as field>
        public static final String ${field.name?upper_case} = "${field.name}";
    
        </#list>
    </#if>
    <#if activeRecord>
        @Override
        protected Serializable pkVal() {
        <#if keyPropertyName??>
            return this.${keyPropertyName};
        <#else>
            return null;
        </#if>
        }
    
    </#if>
    <#if !entityLombokModel>
        @Override
        public String toString() {
            return "${entity}{" +
        <#list table.fields as field>
            <#if field_index==0>
            "${field.propertyName}=" + ${field.propertyName} +
            <#else>
            ", ${field.propertyName}=" + ${field.propertyName} +
            </#if>
        </#list>
            "}";
        }
    </#if>
    }
    
  • 相关阅读:
    “家亡血史,原应叹息”
    SQLite初体验
    两张表数据同步用触发器
    openstack 后期维护(四)--- 删除僵尸卷
    Python3 装逼神器---词云(wordcloud)
    (三)FastDFS 高可用集群架构学习---Client 接口开发
    (四)FastDFS 高可用集群架构学习---后期运维--基础知识及常用命令
    (二)FastDFS 高可用集群架构学习---搭建
    (一)FastDFS 高可用集群架构学习---简介
    Python3使用Print输出彩色字体
  • 原文地址:https://www.cnblogs.com/xiaocy66/p/10589291.html
Copyright © 2011-2022 走看看