|
17 | 17 | package com.itfsw.mybatis.generator.plugins; |
18 | 18 |
|
19 | 19 | import com.itfsw.mybatis.generator.plugins.utils.CommentTools; |
| 20 | +import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools; |
20 | 21 | import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools; |
21 | 22 | import org.mybatis.generator.api.IntrospectedColumn; |
22 | 23 | import org.mybatis.generator.api.IntrospectedTable; |
@@ -50,13 +51,16 @@ public class LogicalDeletePlugin extends PluginAdapter { |
50 | 51 |
|
51 | 52 | public static final String LOGICAL_DELETE_COLUMN_KEY = "logicalDeleteColumn"; // 逻辑删除列-Key |
52 | 53 | public static final String LOGICAL_DELETE_VALUE_KEY = "logicalDeleteValue"; // 逻辑删除值-Key |
| 54 | + public static final String LOGICAL_UN_DELETE_VALUE_KEY = "logicalUnDeleteValue"; // 逻辑删除未删除值-Key |
53 | 55 |
|
54 | | - public static final String DEL_FLAG_NAME = "DEL_FLAG"; // 逻辑删除标志位常量名称 |
| 56 | + public static final String DEL_FLAG_NAME = "DEL_FLAG_OFF"; // 逻辑删除标志位常量名称 |
| 57 | + public static final String UN_DEL_FLAG_NAME = "DEL_FLAG_ON"; // 逻辑删除标志位常量名称(未删除) |
55 | 58 |
|
56 | 59 | public static final String METHOD_LOGICAL_DELETE = "andDeleted"; // 逻辑删除查询方法 |
57 | 60 |
|
58 | 61 | private IntrospectedColumn logicalDeleteColumn; // 逻辑删除列 |
59 | 62 | private String logicalDeleteValue; // 逻辑删除值 |
| 63 | + private String logicalUnDeleteValue; // 逻辑删除值(未删除) |
60 | 64 |
|
61 | 65 | /** |
62 | 66 | * {@inheritDoc} |
@@ -84,13 +88,17 @@ public void initialized(IntrospectedTable introspectedTable) { |
84 | 88 | Properties properties = getProperties(); |
85 | 89 | String logicalDeleteColumn = properties.getProperty(LOGICAL_DELETE_COLUMN_KEY); |
86 | 90 | this.logicalDeleteValue = properties.getProperty(LOGICAL_DELETE_VALUE_KEY); |
| 91 | + this.logicalUnDeleteValue = properties.getProperty(LOGICAL_UN_DELETE_VALUE_KEY); |
87 | 92 | // 2. 获取表单独配置,如果有则覆盖全局配置 |
88 | 93 | if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY) != null){ |
89 | 94 | logicalDeleteColumn = introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY); |
90 | 95 | } |
91 | 96 | if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_VALUE_KEY) != null){ |
92 | 97 | this.logicalDeleteValue = introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_VALUE_KEY); |
93 | 98 | } |
| 99 | + if (introspectedTable.getTableConfigurationProperty(LOGICAL_UN_DELETE_VALUE_KEY) != null){ |
| 100 | + this.logicalUnDeleteValue = introspectedTable.getTableConfigurationProperty(LOGICAL_UN_DELETE_VALUE_KEY); |
| 101 | + } |
94 | 102 | // 3. 判断该表是否存在逻辑删除列 |
95 | 103 | this.logicalDeleteColumn = null; |
96 | 104 | List<IntrospectedColumn> columns = introspectedTable.getAllColumns(); |
@@ -124,6 +132,11 @@ public void initialized(IntrospectedTable introspectedTable) { |
124 | 132 | if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY) != null && this.logicalDeleteColumn == null){ |
125 | 133 | logger.warn("itfsw(逻辑删除插件):"+introspectedTable.getFullyQualifiedTable()+"没有找到您配置的逻辑删除列("+introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY)+")!"); |
126 | 134 | } |
| 135 | + |
| 136 | + // 4. 判断逻辑删除值是否配置了 |
| 137 | + if (this.logicalDeleteColumn != null && (this.logicalDeleteValue == null || this.logicalUnDeleteValue == null)){ |
| 138 | + logger.warn("itfsw(逻辑删除插件):"+introspectedTable.getFullyQualifiedTable()+"没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值!"); |
| 139 | + } |
127 | 140 | } |
128 | 141 |
|
129 | 142 | /** |
@@ -330,27 +343,47 @@ public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable intr |
330 | 343 | @Override |
331 | 344 | public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { |
332 | 345 | if (this.logicalDeleteColumn != null){ |
333 | | - // 添加删除标志位常量 |
334 | | - Field field = new Field(DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType()); |
335 | | - field.setVisibility(JavaVisibility.PUBLIC); |
336 | | - field.setStatic(true); |
337 | | - field.setFinal(true); |
338 | 346 |
|
| 347 | + ArrayList<Field> fields = (ArrayList<Field>) topLevelClass.getFields(); |
| 348 | + |
| 349 | + // TODO 过期的 |
| 350 | + Field field2 = JavaElementGeneratorTools.generateStaticFinalField("DEL_FLAG", this.logicalDeleteColumn.getFullyQualifiedJavaType(), DEL_FLAG_NAME, introspectedTable); |
| 351 | + field2.addAnnotation("@Deprecated"); |
| 352 | + // 常量插入到第一位 |
| 353 | + fields.add(0, field2); |
| 354 | + logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"增加方法DEL_FLAG的常量。"); |
| 355 | + |
| 356 | + // 添加删除标志位常量 DEL_FLAG_OFF |
| 357 | + String delFlagOnValue; |
339 | 358 | if (this.logicalDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalDeleteValue)){ |
340 | | - field.setInitializationString("null"); |
| 359 | + delFlagOnValue = "null"; |
341 | 360 | } else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("String")){ |
342 | | - field.setInitializationString("\"" + this.logicalDeleteValue + "\""); |
| 361 | + delFlagOnValue = "\"" + this.logicalDeleteValue + "\""; |
343 | 362 | } else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("Boolean")){ |
344 | | - field.setInitializationString((this.logicalDeleteValue.equals("1") || this.logicalDeleteValue.equalsIgnoreCase("true")) ? "true" : "false"); |
| 363 | + delFlagOnValue = (this.logicalDeleteValue.equals("1") || this.logicalDeleteValue.equalsIgnoreCase("true")) ? "true" : "false"; |
345 | 364 | } else { |
346 | | - field.setInitializationString(this.logicalDeleteValue); |
| 365 | + delFlagOnValue = this.logicalDeleteValue; |
347 | 366 | } |
348 | | - CommentTools.addFieldComment(field, introspectedTable); |
349 | | - |
| 367 | + Field field = JavaElementGeneratorTools.generateStaticFinalField(DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType(), delFlagOnValue, introspectedTable); |
350 | 368 | // 常量插入到第一位 |
351 | | - ArrayList<Field> fields = (ArrayList<Field>) topLevelClass.getFields(); |
352 | 369 | fields.add(0, field); |
353 | | - logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"增加方法DEL_FLAG的常量。"); |
| 370 | + logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"增加方法DEL_FLAG_OFF的常量。"); |
| 371 | + |
| 372 | + // 添加删除标志位常量 DEL_FLAG_ON |
| 373 | + String unDelFlagOnValue; |
| 374 | + if (this.logicalUnDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalUnDeleteValue)){ |
| 375 | + unDelFlagOnValue = "null"; |
| 376 | + } else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("String")){ |
| 377 | + unDelFlagOnValue = "\"" + this.logicalUnDeleteValue + "\""; |
| 378 | + } else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("Boolean")){ |
| 379 | + unDelFlagOnValue = (this.logicalUnDeleteValue.equals("1") || this.logicalUnDeleteValue.equalsIgnoreCase("true")) ? "true" : "false"; |
| 380 | + } else { |
| 381 | + unDelFlagOnValue = this.logicalUnDeleteValue; |
| 382 | + } |
| 383 | + Field field1 = JavaElementGeneratorTools.generateStaticFinalField(UN_DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType(), unDelFlagOnValue, introspectedTable); |
| 384 | + // 常量插入到第一位 |
| 385 | + fields.add(0, field1); |
| 386 | + logger.debug("itfsw(逻辑删除插件):"+topLevelClass.getType().getShortName()+"增加方法DEL_FLAG_ON的常量。"); |
354 | 387 | } |
355 | 388 | return true; |
356 | 389 | } |
|
0 commit comments