entity.java.ftl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package ${package.Entity};
  2. <#list table.importPackages as pkg>
  3. import ${pkg};
  4. </#list>
  5. <#if swagger2>
  6. import io.swagger.annotations.ApiModel;
  7. import io.swagger.annotations.ApiModelProperty;
  8. </#if>
  9. import javax.validation.constraints.NotEmpty;
  10. import javax.validation.constraints.NotNull;
  11. import org.hibernate.validator.constraints.Length;
  12. import org.hibernate.validator.constraints.Range;
  13. import java.time.LocalDateTime;
  14. <#if entityLombokModel>
  15. import lombok.AllArgsConstructor;
  16. import lombok.Builder;
  17. import lombok.Data;
  18. import lombok.EqualsAndHashCode;
  19. import lombok.NoArgsConstructor;
  20. import lombok.ToString;
  21. import lombok.experimental.Accessors;
  22. </#if>
  23. <#if cfg.filedTypes??>
  24. <#list cfg.filedTypes as fieldType>
  25. <#list table.fields as field>
  26. <#if field.propertyName == fieldType.name && table.name==fieldType.table && field.propertyType=="String">
  27. import ${fieldType.packagePath};
  28. <#break>
  29. </#if>
  30. </#list>
  31. </#list>
  32. </#if>
  33. import static com.baomidou.mybatisplus.annotation.SqlCondition.LIKE;
  34. <#assign tableComment = "${table.comment!''}"/>
  35. <#if table.comment?? && table.comment!?contains('\n')>
  36. <#assign tableComment = "${table.comment!?substring(0,table.comment?index_of('\n'))?trim}"/>
  37. </#if>
  38. /**
  39. * <p>
  40. * 实体类
  41. * ${table.comment!?replace("\n","\n * ")}
  42. * </p>
  43. *
  44. * @since ${date}
  45. */
  46. <#if entityLombokModel>
  47. @Data
  48. @NoArgsConstructor
  49. @ToString(callSuper = true)
  50. </#if>
  51. <#if table.convert>
  52. @TableName("${table.name}")
  53. </#if>
  54. <#if swagger2>
  55. @ApiModel(value = "${entity}", description = "${tableComment}")
  56. </#if>
  57. <#if superEntityClass??>
  58. @AllArgsConstructor
  59. <#assign hasCustomAnno="0"/>
  60. <#if superEntityClass?? && superEntityClass=="TreeEntity">
  61. <#assign hasCustomAnno="1"/>
  62. </#if>
  63. public class ${entity} {
  64. <#elseif activeRecord>
  65. @AllArgsConstructor
  66. public class ${entity} extends Model<${entity}> {
  67. <#else>
  68. public class ${entity} implements Serializable {
  69. </#if>
  70. private static final long serialVersionUID = 1L;
  71. <#setting number_format="0">
  72. <#-- ---------- BEGIN 字段循环遍历 ---------->
  73. <#list table.fields as field>
  74. <#if field.keyFlag>
  75. <#assign keyPropertyName="${field.propertyName}"/>
  76. </#if>
  77. <#assign fieldComment="${field.comment!}"/>
  78. <#if field.comment!?length gt 0>
  79. /**
  80. * ${field.comment!?replace("\n","\n * ")}
  81. */
  82. <#if field.comment!?contains("\n") >
  83. <#assign fieldComment="${field.comment!?substring(0,field.comment?index_of('\n'))?replace('\r\n','')?replace('\r','')?replace('\n','')?trim}"/>
  84. </#if>
  85. </#if>
  86. <#if swagger2>
  87. @ApiModelProperty(value = "${fieldComment}")
  88. </#if>
  89. <#assign myPropertyType="${field.propertyType}"/>
  90. <#assign isEnumType="1"/>
  91. <#list cfg.filedTypes as fieldType>
  92. <#if fieldType.name == field.propertyName && table.name==fieldType.table && field.propertyType=="String">
  93. <#assign myPropertyType="${fieldType.type}"/>
  94. <#assign isEnumType="2"/>
  95. </#if>
  96. </#list>
  97. <#if field.customMap.Null == "NO" >
  98. <#if (field.columnType!"") == "STRING" && isEnumType == "1">
  99. @NotEmpty(message = "${fieldComment}不能为空")
  100. <#else>
  101. @NotNull(message = "${fieldComment}不能为空")
  102. </#if>
  103. </#if>
  104. <#if (field.columnType!"") == "STRING" && isEnumType == "1">
  105. <#assign max = 255/>
  106. <#if field.type?starts_with("varchar") || field.type?starts_with("char")>
  107. <#if field.type?contains("(")>
  108. <#assign max = field.type?substring(field.type?index_of("(") + 1, field.type?index_of(")"))/>
  109. </#if>
  110. @Length(max = ${max}, message = "${fieldComment}长度不能超过${max}")
  111. <#elseif field.type?starts_with("text")>
  112. <#assign max = 65535/>
  113. @Length(max = ${max}, message = "${fieldComment}长度不能超过${max}")
  114. <#elseif field.type?starts_with("mediumtext")>
  115. <#assign max = 16777215/>
  116. @Length(max = ${max}, message = "${fieldComment}长度不能超过${max}")
  117. <#elseif field.type?starts_with("longtext")>
  118. </#if>
  119. <#else>
  120. <#if field.propertyType?starts_with("Short")>
  121. @Range(min = Short.MIN_VALUE, max = Short.MAX_VALUE, message = "${fieldComment}长度不能超过"+Short.MAX_VALUE)
  122. </#if>
  123. <#if field.propertyType?starts_with("Byte")>
  124. @Range(min = Byte.MIN_VALUE, max = Byte.MAX_VALUE, message = "${fieldComment}长度不能超过"+Byte.MAX_VALUE)
  125. </#if>
  126. <#if field.propertyType?starts_with("Short")>
  127. @Range(min = Short.MIN_VALUE, max = Short.MAX_VALUE, message = "${fieldComment}长度不能超过"+Short.MAX_VALUE)
  128. </#if>
  129. </#if>
  130. <#if field.keyFlag>
  131. <#-- 主键 -->
  132. <#if field.keyIdentityFlag>
  133. @TableId(value = "${field.name}", type = IdType.AUTO)
  134. <#elseif idType??>
  135. @TableId(value = "${field.name}", type = IdType.${idType})
  136. <#elseif field.convert>
  137. @TableId("${field.name}")
  138. </#if>
  139. <#-- 普通字段 -->
  140. <#elseif field.fill??>
  141. <#-- ----- 存在字段填充设置 ----->
  142. <#if field.convert>
  143. @TableField(value = "${field.name}", fill = FieldFill.${field.fill})
  144. <#else>
  145. @TableField(fill = FieldFill.${field.fill})
  146. </#if>
  147. <#elseif field.convert>
  148. <#if (field.type?starts_with("varchar") || field.type?starts_with("char")) && myPropertyType == "String">
  149. @TableField(value = "${field.name}", condition = LIKE)
  150. <#else>
  151. @TableField("${field.name}")
  152. </#if>
  153. </#if>
  154. <#-- 乐观锁注解 -->
  155. <#if (versionFieldName!"") == field.name>
  156. @Version
  157. </#if>
  158. <#-- 逻辑删除注解 -->
  159. <#if (logicDeleteFieldName!"") == field.name>
  160. @TableLogic
  161. </#if>
  162. <#assign myPropertyName="${field.propertyName}"/>
  163. <#-- 自动注入注解 -->
  164. <#if field.customMap.annotation??>
  165. ${field.customMap.annotation}
  166. @ExcelEntity(name = "")
  167. <#assign myPropertyType="${field.customMap.type}"/>
  168. <#if field.propertyName?ends_with("Id")>
  169. <#assign myPropertyName="${field.propertyName!?substring(0,field.propertyName?index_of('Id'))}"/>
  170. </#if>
  171. </#if>
  172. private ${myPropertyType} ${myPropertyName};
  173. </#list>
  174. <#------------ END 字段循环遍历 ---------->
  175. <#if !entityLombokModel>
  176. <#list table.fields as field>
  177. <#if field.propertyType == "boolean">
  178. <#assign getprefix="is"/>
  179. <#else>
  180. <#assign getprefix="get"/>
  181. </#if>
  182. public ${field.propertyType} ${getprefix}${field.capitalName}() {
  183. return ${field.propertyName};
  184. }
  185. <#if entityBuilderModel>
  186. public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
  187. <#else>
  188. public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
  189. </#if>
  190. this.${field.propertyName} = ${field.propertyName};
  191. <#if entityBuilderModel>
  192. return this;
  193. </#if>
  194. }
  195. </#list>
  196. </#if>
  197. <#if activeRecord>
  198. @Override
  199. protected Serializable pkVal() {
  200. <#if keyPropertyName??>
  201. return this.${keyPropertyName};
  202. <#else>
  203. return null;
  204. </#if>
  205. }
  206. </#if>
  207. <#if !entityLombokModel>
  208. @Override
  209. public String toString() {
  210. return "${entity}{" +
  211. <#list table.fields as field>
  212. <#if field_index==0>
  213. "${field.propertyName}=" + ${field.propertyName} +
  214. <#else>
  215. ", ${field.propertyName}=" + ${field.propertyName} +
  216. </#if>
  217. </#list>
  218. "}";
  219. }
  220. </#if>
  221. }