修改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 '名字类型{Girl:0,女孩;boy:1,男孩;other:2,其它}' -->
<#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 '名字类型{Girl:0,女孩;boy:1,男孩;other:2,其它}' -->
<#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>
}

修改mybatis plus Generator模板生成字段注释枚举常量的更多相关文章

  1. 使用T4模板为EF框架添加实体根据数据库自动生成字段注释的功能

    转自http://jeffblog.sinaapp.com/archives/501 首先我们先下载一个文件GetSummery,这里我提供了,大家可以直接下载:下载 我们在数据库建立一个表,并给表中 ...

  2. 【Mybatis】MyBatis之Generator自动生成代码(九)

    MyBatis Generator 简介 MyBatis Generator 连接数据库表并生成MyBatis或iBatis文件.这有助于最大限度地减少使用MyBatis时为数据库文件创建简单CRUD ...

  3. Mybatis的generator自动生成代码

    mybatis-generator有三种用法:命令行.ide插件.maven插件.本次使用maven生成 环境:IDEA,mysql8,maven (1):新建项目,本次以SpringBoot项目为例 ...

  4. MyBatis使用Generator自动生成代码

    MyBatis中,可以使用Generator自动生成代码,包括DAO层. MODEL层 .MAPPING SQL映射文件. 第一步: 配置好自动生成代码所需的XML配置文件,例如(generator. ...

  5. Mybatis使用generator自动生成映射配置文件信息

     使用mybatis配置映射文件比较的麻烦,但是有自动生成jar工具,方便加速开发速度,下面主要是该工具的使用以及相关的配置. 1.下载相关的资源 我们需要下载mybatis-generator-co ...

  6. MyBatis 使用Generator自动生成Model , Dao, mapper

    最近   我新建了一 个maven 项目,使用的是spring + springmvc + mybatis框架. 听说Mybatis可以自动生成model和mapper以及dao层,我就从网上查了查资 ...

  7. Mybatis使用generator自动生成的Example类使用OR条件查询

    参考:https://blog.csdn.net/qq_36614559/article/details/80354511 public List<AssetsDevicetypeRefacto ...

  8. SpringBoot整合Mybatis 使用generator自动生成实体类代码、Mapper代码、dao层代码

    1.新建一个SpringBoot项目,并引入Mybatis和mybatis-generator相关的依赖. <dependency> <groupId>org.springfr ...

  9. mybatis拦截器 修改mybatis返回结果集中的字段的值

    项目中使用了shardingJDBC,业务库做了分库,公共库没在一起,所以导致做码值转换的时候,需要在实现类里面做转码,重复的代码量大,故考虑用mybatis拦截器,将码值转换后再做返回给实现类.   ...

随机推荐

  1. HTML CSS JavaScript 工作笔记

    1. onclick方法如何传递多个参数 "<a href='#' onclick=\"applied_status('" + ids + "', '&q ...

  2. 繁简字转换(C#)

    1.首先引入: using Microsoft.VisualBasic; 2.转换方法: //繁体转简体 public static string Traditional2Simplified(str ...

  3. 012-Python-paramiko和IO多路复用

    1.IO 多路复用 1.监听多个socket变化 2.socket服务端 IO多路复用+socket 来实现web服务器: a.服务端优先运行 b.浏览器:http://.......com 浏览器连 ...

  4. 【C++ Primer | 15】访问控制与继承、继承中的类作用域

    1. 只有D继承B的方式是public时,用户代码才能使用派生类向基类的转换:如果D继承B的方式是受保护的或者私有的,则用户代码不能使用该转换. 2. 不论D以什么方式继承B,D的成员函数和友员函数都 ...

  5. OpenSSL + Windows 下载安装

    Download Win32 OpenSSLhttps://slproweb.com/products/Win32OpenSSL.htmlhttps://wiki.openssl.org/index. ...

  6. CDOJ 1962 天才钱vs学霸周2【最大流】

    以s=0,t=n+m+1分别为超级源点和超级汇点.网络流中的流量以0为开始,题目要求从1到20,我们先把每个点都减去1,即ai - m,bi - n.然后源点s与n个顶点连容量为ai的路,汇点t与m个 ...

  7. 【bzoj3747】[POI2015]Kinoman

    题解: 水题 从左向右维护以每一个作为右端点的最大值 线段树维护 代码: #include <bits/stdc++.h> using namespace std; #define rin ...

  8. .net core 使用NPOI填充Word模板导出Word

    最近工作用到在Word模板插入数据库数据,导出一个带数据的Word文件,想起来之前操作Word都是用微软提供的Microsoft.Office.Interop.Word,而在最新的..NET CORE ...

  9. Python自用笔记

    函数:raw_input()和input() 注意:在python3.x中,已经删除raw_input(),取而代之的是input(),当然这仅仅是重命名,用法还是一样.因此在这里介绍的是python ...

  10. Excel ——多表关联查询-vlookup

    一.分组 需求: 在B列的右侧添加一列[消费分组]对B列的[月分组水平]进行分组,原始数据如下: 公式:在 C2 输入:“=VLOOKUP(B2,$E$1:$G$4,2,1)”,下拉填充. 提示:VL ...