修改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. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  2. gitlab的完全卸载

    一:先停止gitlab gitlab-ctl stop 二:卸载gitlab部分(之前我是rpm安装的,这里rpm卸载) rpm  -e  gitlab-ce 三:发现系统进程还有一个gitlab的进 ...

  3. 步步為營-98-MyAPI

    1 通过NuGet程序管理包添加  Microsoft Asp.Net webAPI 2.2 的引用 2 添加两个文件夹Controllers和Models 2.1 在本地模拟数据库,所以在Model ...

  4. Oracle 11g 安装过程中“检查网络配置要求 未执行”解决方法

    正在检查网络配置要求... 检查完成.此次检查的总体结果为: 未执行 网上查了一下,很多朋友都遇到这个问题而无从下手,其实解决起来很容易的. 只需要在 Windows XP 中安装 Microsoft ...

  5. kickstart-G

    感觉自己很蠢,large数据只能交一次,忘记这回事了 A题 O(n^2)解法,用vector<set> 缓存j后面的数据,减少一重循环 #include <string> #i ...

  6. alpha冲刺2/10

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:拿快递也不能耽搁了软工 团队部分 后敬甲(组长) 过去两天完成了哪些任务 文字描述 github代码管理规范 商家端订单 ...

  7. Sql与C#中日期格式转换总结

    SQL中的转换方法: 一.将string转换为datetime,主要是使用Convert方法, 方法,Convert(datetime [ ( length ) ] , expression, [st ...

  8. User模型扩展和自定义

    参考如下: django文档参考 django signal使用总结 django 信号注册 django信号问题1 django oneTooneFiled     1. django 自定义用户u ...

  9. 强大的图片展示插件,JQuery图片预览展示插件

    只需要引入JQuery.js , viewer.css 和 viewer.js <!DOCTYPE html> <html lang="en"> <h ...

  10. maven pom.xml(公司版)

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...