entity.java.ftl 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 javax.validation.constraints.Size;
  12. import java.time.LocalDateTime;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. <#if entityLombokModel>
  16. import lombok.AllArgsConstructor;
  17. import lombok.Builder;
  18. import lombok.Data;
  19. import lombok.EqualsAndHashCode;
  20. import lombok.NoArgsConstructor;
  21. import lombok.ToString;
  22. import lombok.experimental.Accessors;
  23. </#if>
  24. <#if cfg.filedTypes??>
  25. <#list cfg.filedTypes as fieldType>
  26. <#list table.fields as field>
  27. <#if field.propertyName == fieldType.name && table.name==fieldType.table && field.propertyType=="String">
  28. import ${fieldType.packagePath};
  29. <#break>
  30. </#if>
  31. </#list>
  32. </#list>
  33. </#if>
  34. import static com.baomidou.mybatisplus.annotation.SqlCondition.LIKE;
  35. <#assign tableComment = "${table.comment!''}"/>
  36. <#if table.comment?? && table.comment!?contains('\n')>
  37. <#assign tableComment = "${table.comment!?substring(0,table.comment?index_of('\n'))?trim}"/>
  38. </#if>
  39. /**
  40. * <p>
  41. * 实体类
  42. * ${table.comment!?replace("\n","\n * ")}
  43. * </p>
  44. *
  45. * @author ${author}
  46. * @since ${date}
  47. */
  48. <#if entityLombokModel>
  49. @Data
  50. @NoArgsConstructor
  51. @ToString(callSuper = true)
  52. <#if superEntityClass??>
  53. @EqualsAndHashCode(callSuper = true)
  54. <#else>
  55. @EqualsAndHashCode(callSuper = false)
  56. </#if>
  57. @Accessors(chain = true)
  58. </#if>
  59. <#if table.convert>
  60. @TableName("${table.name}")
  61. </#if>
  62. <#if swagger2>
  63. @ApiModel(value = "${entity}", description = "${tableComment}")
  64. </#if>
  65. <#if superEntityClass??>
  66. @AllArgsConstructor
  67. <#assign hasCustomAnno="0"/>
  68. <#if superEntityClass?? && superEntityClass=="TreeEntity">
  69. <#assign hasCustomAnno="1"/>
  70. </#if>
  71. public class ${entity} extends Entity<Long> {
  72. <#elseif activeRecord>
  73. @AllArgsConstructor
  74. public class ${entity} extends Entity<Long> {
  75. <#else>
  76. public class ${entity} extends Entity<Long> {
  77. </#if>
  78. private static final long serialVersionUID = 1L;
  79. @TableField(exist = false)
  80. private Map<String, Object> echoMap = new HashMap<>();
  81. <#setting number_format="0">
  82. <#-- ---------- BEGIN 字段循环遍历 ---------->
  83. <#list table.fields as field>
  84. <#-- 如果有父类,排除公共字段 -->
  85. <#if (superEntityClass?? && cfg.superExtend?? && field.propertyName !="id") || (superEntityClass?? && field.propertyName !="id" && field.propertyName !="createTime" && field.propertyName != "updateTime" && field.propertyName !="createUser" && field.propertyName !="updateUser") || !superEntityClass??>
  86. <#if field.keyFlag>
  87. <#assign keyPropertyName="${field.propertyName}"/>
  88. </#if>
  89. <#assign fieldComment="${field.comment!}"/>
  90. <#if field.comment!?length gt 0>
  91. /**
  92. * ${field.comment!?replace("\n","\n * ")}
  93. */
  94. <#if field.comment!?contains("\n") >
  95. <#assign fieldComment="${field.comment!?substring(0,field.comment?index_of('\n'))?replace('\r\n','')?replace('\r','')?replace('\n','')?trim}"/>
  96. </#if>
  97. </#if>
  98. <#if swagger2>
  99. @ApiModelProperty(value = "${fieldComment}")
  100. </#if>
  101. <#assign myPropertyType="${field.propertyType}"/>
  102. <#assign isEnumType="1"/>
  103. <#list cfg.filedTypes as fieldType>
  104. <#if fieldType.name == field.propertyName && table.name==fieldType.table && field.propertyType=="String">
  105. <#assign myPropertyType="${fieldType.type}"/>
  106. <#assign isEnumType="2"/>
  107. </#if>
  108. </#list>
  109. <#if field.customMap.Null == "NO" >
  110. <#if (field.columnType!"") == "STRING" && isEnumType == "1">
  111. @NotEmpty(message = "请填写${fieldComment}")
  112. <#else>
  113. @NotNull(message = "请填写${fieldComment}")
  114. </#if>
  115. </#if>
  116. <#if (field.columnType!"") == "STRING" && isEnumType == "1">
  117. <#assign max = 255/>
  118. <#if field.type?starts_with("varchar") || field.type?starts_with("char")>
  119. <#if field.type?contains("(")>
  120. <#assign max = field.type?substring(field.type?index_of("(") + 1, field.type?index_of(")"))/>
  121. </#if>
  122. @Size(max = ${max}, message = "${fieldComment}长度不能超过${max}")
  123. <#elseif field.type?starts_with("text")>
  124. <#assign max = 65535/>
  125. @Size(max = ${max}, message = "${fieldComment}长度不能超过${max}")
  126. <#elseif field.type?starts_with("mediumtext")>
  127. <#assign max = 16777215/>
  128. @Size(max = ${max}, message = "${fieldComment}长度不能超过${max}")
  129. <#elseif field.type?starts_with("longtext")>
  130. </#if>
  131. <#else>
  132. <#if field.propertyType?starts_with("Short")>
  133. @Size(min = Short.MIN_VALUE, max = Short.MAX_VALUE, message = "${fieldComment}长度不能超过"+Short.MAX_VALUE)
  134. </#if>
  135. <#if field.propertyType?starts_with("Byte")>
  136. @Size(min = Byte.MIN_VALUE, max = Byte.MAX_VALUE, message = "${fieldComment}长度不能超过"+Byte.MAX_VALUE)
  137. </#if>
  138. <#if field.propertyType?starts_with("Short")>
  139. @Size(min = Short.MIN_VALUE, max = Short.MAX_VALUE, message = "${fieldComment}长度不能超过"+Short.MAX_VALUE)
  140. </#if>
  141. </#if>
  142. <#if field.keyFlag>
  143. <#-- 主键 -->
  144. @TableId(value = "${field.name}"<#if field.keyIdentityFlag>, type = IdType.AUTO<#elseif idType??>, type = IdType.${idType}</#if>)
  145. <#else>
  146. @TableField(value = "${field.name}"<#if field.fill??>, fill = FieldFill.${field.fill}</#if><#if myPropertyType == "String">, condition = LIKE<#else></#if>)
  147. </#if>
  148. <#-- 乐观锁注解 -->
  149. <#if (versionFieldName!"") == field.name>
  150. @Version
  151. </#if>
  152. <#-- 逻辑删除注解 -->
  153. <#if (logicDeleteFieldName!"") == field.name>
  154. @TableLogic
  155. </#if>
  156. <#assign myPropertyName="${field.propertyName}"/>
  157. <#-- 自动注入注解 -->
  158. <#if field.customMap.annotation??>
  159. ${field.customMap.annotation}
  160. <#-- <#assign myPropertyType="${field.customMap.type}"/>-->
  161. <#-- <#if field.propertyName?ends_with("Id")>-->
  162. <#-- <#assign myPropertyName="${field.propertyName!?substring(0,field.propertyName?index_of('Id'))}"/>-->
  163. <#-- </#if>-->
  164. </#if>
  165. @Excel(name = "${fieldComment}"<#if myPropertyType!?contains("Local")>, format = "yyyy-MM-dd", width = 20</#if><#if myPropertyType!?contains("Boolean")>, replace = {"是_true", "否_false", "_null"}</#if><#if isEnumType=="2">, replace = {<#list field.customMap.enumCustom.list?keys as key>"${field.customMap.enumCustom.list[key][0]}_${key?upper_case}", </#list> "_null"}</#if>)
  166. private ${myPropertyType} ${myPropertyName};
  167. </#if>
  168. </#list>
  169. <#------------ END 字段循环遍历 ---------->
  170. <#if !entityLombokModel>
  171. <#list table.fields as field>
  172. <#if field.propertyType == "boolean">
  173. <#assign getprefix="is"/>
  174. <#else>
  175. <#assign getprefix="get"/>
  176. </#if>
  177. public ${field.propertyType} ${getprefix}${field.capitalName}() {
  178. return ${field.propertyName};
  179. }
  180. <#if entityBuilderModel>
  181. public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
  182. <#else>
  183. public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
  184. </#if>
  185. this.${field.propertyName} = ${field.propertyName};
  186. <#if entityBuilderModel>
  187. return this;
  188. </#if>
  189. }
  190. </#list>
  191. </#if>
  192. <#-- 如果有父类,自定义无全参构造方法 -->
  193. @Builder
  194. public ${entity}(<#list table.commonFields as cf><#if cf.propertyName!="tenantCode">${cf.propertyType} ${cf.propertyName}, </#if></#list>
  195. <#list table.fields as field><#assign myPropertyType="${field.propertyType}"/><#list cfg.filedTypes as fieldType><#if fieldType.name == field.propertyName && table.name==fieldType.table && field.propertyType=="String"><#assign myPropertyType="${fieldType.type}"/></#if></#list><#if field_has_next>${((field_index + 1) % 6 ==0)?string('\r\n ', '')}${myPropertyType} ${field.propertyName}, <#else>${myPropertyType} ${field.propertyName}</#if></#list>) {
  196. <#list table.commonFields as cf>
  197. <#if cf.propertyName!="tenantCode">
  198. this.${cf.propertyName} = ${cf.propertyName};
  199. </#if>
  200. </#list>
  201. <#list table.fields as field>
  202. <#assign myPropertyName="${field.propertyName}"/>
  203. <#-- 自动注入注解 -->
  204. <#-- <#if field.customMap.annotation?? && field.propertyName?ends_with("Id")>-->
  205. <#-- <#assign myPropertyName="${field.propertyName!?substring(0,field.propertyName?index_of('Id'))}"/>-->
  206. <#-- </#if>-->
  207. this.${myPropertyName} = ${field.propertyName};
  208. </#list>
  209. }
  210. <#if activeRecord>
  211. @Override
  212. protected Serializable pkVal() {
  213. <#if keyPropertyName??>
  214. return this.${keyPropertyName};
  215. <#else>
  216. return null;
  217. </#if>
  218. }
  219. </#if>
  220. <#if !entityLombokModel>
  221. @Override
  222. public String toString() {
  223. return "${entity}{" +
  224. <#list table.fields as field>
  225. <#if field_index==0>
  226. "${field.propertyName}=" + ${field.propertyName} +
  227. <#else>
  228. ", ${field.propertyName}=" + ${field.propertyName} +
  229. </#if>
  230. </#list>
  231. "}";
  232. }
  233. </#if>
  234. }