修改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 '名字类型{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模板生成字段注释枚举常量的更多相关文章
- 使用T4模板为EF框架添加实体根据数据库自动生成字段注释的功能
转自http://jeffblog.sinaapp.com/archives/501 首先我们先下载一个文件GetSummery,这里我提供了,大家可以直接下载:下载 我们在数据库建立一个表,并给表中 ...
- 【Mybatis】MyBatis之Generator自动生成代码(九)
MyBatis Generator 简介 MyBatis Generator 连接数据库表并生成MyBatis或iBatis文件.这有助于最大限度地减少使用MyBatis时为数据库文件创建简单CRUD ...
- Mybatis的generator自动生成代码
mybatis-generator有三种用法:命令行.ide插件.maven插件.本次使用maven生成 环境:IDEA,mysql8,maven (1):新建项目,本次以SpringBoot项目为例 ...
- MyBatis使用Generator自动生成代码
MyBatis中,可以使用Generator自动生成代码,包括DAO层. MODEL层 .MAPPING SQL映射文件. 第一步: 配置好自动生成代码所需的XML配置文件,例如(generator. ...
- Mybatis使用generator自动生成映射配置文件信息
使用mybatis配置映射文件比较的麻烦,但是有自动生成jar工具,方便加速开发速度,下面主要是该工具的使用以及相关的配置. 1.下载相关的资源 我们需要下载mybatis-generator-co ...
- MyBatis 使用Generator自动生成Model , Dao, mapper
最近 我新建了一 个maven 项目,使用的是spring + springmvc + mybatis框架. 听说Mybatis可以自动生成model和mapper以及dao层,我就从网上查了查资 ...
- Mybatis使用generator自动生成的Example类使用OR条件查询
参考:https://blog.csdn.net/qq_36614559/article/details/80354511 public List<AssetsDevicetypeRefacto ...
- SpringBoot整合Mybatis 使用generator自动生成实体类代码、Mapper代码、dao层代码
1.新建一个SpringBoot项目,并引入Mybatis和mybatis-generator相关的依赖. <dependency> <groupId>org.springfr ...
- mybatis拦截器 修改mybatis返回结果集中的字段的值
项目中使用了shardingJDBC,业务库做了分库,公共库没在一起,所以导致做码值转换的时候,需要在实现类里面做转码,重复的代码量大,故考虑用mybatis拦截器,将码值转换后再做返回给实现类. ...
随机推荐
- 微信小程序开发 如何退出当前页面
默认是在首页 wx.navigateBack({ delta: -1 }); 详情参考. https://mp.weixin.qq.com/debug/wxadoc/dev/api/u ...
- 提取Word里的文本内容 C#
using DocumentFormat.OpenXml.Packaging; public static string TextFromWord(string path) { const strin ...
- Java+selenium之WebDriver的常用方法封装(八)
总结:WEB UI自动化测试一般采用 POP(面向页面编程),自动化测试框架分三层,有时如果页面如果太多,不好管理,可以面向控件编程,即把控件当作页面,毕竟控件是有限的,所以封装页面的代码量会少很多, ...
- meaven
一个项目管理工具.java语言编写的,所以可以跨平台 https://mvnrepository.com/
- Just oj 2018 C语言程序设计竞赛(高级组)H: CBT?
H: CBT? 时间限制: 1 s 内存限制: 128 MB 提交 我的状态 题目描述 对于二叉树,如果这棵树的节点排布是按行从上到下,每行从左到右挨个放置,中间不会有空闲的节点. ...
- 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)
在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...
- 【Leetcode | 5】求和问题
一.1两数之和 二.15三数之和 C++ Soution 1: class Solution { public: vector<vector<int>> threeSum(ve ...
- proc/net/dev实时网速统计实例
https://blog.csdn.net/dosthing/article/details/80384541 http://www.l99.com/EditText_view.action?text ...
- BZOJ 4282(慎二的随机数列
题解: 网上题解还没看 我的方法是用平衡树维护一个单调栈 由于N用了一定是赚的 所以它的作用是让f[i+1]=f[i]+1 这个是可以记录的 就跟noip蚯蚓那题一样 然后插入一个值的时候查询前面的最 ...
- 转:ubuntu-E:Encountered a section with no Package: header的解决办法
http://blog.csdn.net/hs794502825/article/details/7835902 blog.csdn.net/lixiang0522/article/details/7 ...