Skip to content

Commit 9b83bc1

Browse files
author
hewei
committed
逻辑删除插件增强
1 parent 91fcd50 commit 9b83bc1

File tree

2 files changed

+91
-14
lines changed

2 files changed

+91
-14
lines changed

src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.itfsw.mybatis.generator.plugins;
1818

1919
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
20+
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
2021
import com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools;
2122
import org.mybatis.generator.api.IntrospectedColumn;
2223
import org.mybatis.generator.api.IntrospectedTable;
@@ -50,13 +51,16 @@ public class LogicalDeletePlugin extends PluginAdapter {
5051

5152
public static final String LOGICAL_DELETE_COLUMN_KEY = "logicalDeleteColumn"; // 逻辑删除列-Key
5253
public static final String LOGICAL_DELETE_VALUE_KEY = "logicalDeleteValue"; // 逻辑删除值-Key
54+
public static final String LOGICAL_UN_DELETE_VALUE_KEY = "logicalUnDeleteValue"; // 逻辑删除未删除值-Key
5355

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"; // 逻辑删除标志位常量名称(未删除)
5558

5659
public static final String METHOD_LOGICAL_DELETE = "andDeleted"; // 逻辑删除查询方法
5760

5861
private IntrospectedColumn logicalDeleteColumn; // 逻辑删除列
5962
private String logicalDeleteValue; // 逻辑删除值
63+
private String logicalUnDeleteValue; // 逻辑删除值(未删除)
6064

6165
/**
6266
* {@inheritDoc}
@@ -84,13 +88,17 @@ public void initialized(IntrospectedTable introspectedTable) {
8488
Properties properties = getProperties();
8589
String logicalDeleteColumn = properties.getProperty(LOGICAL_DELETE_COLUMN_KEY);
8690
this.logicalDeleteValue = properties.getProperty(LOGICAL_DELETE_VALUE_KEY);
91+
this.logicalUnDeleteValue = properties.getProperty(LOGICAL_UN_DELETE_VALUE_KEY);
8792
// 2. 获取表单独配置,如果有则覆盖全局配置
8893
if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY) != null){
8994
logicalDeleteColumn = introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY);
9095
}
9196
if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_VALUE_KEY) != null){
9297
this.logicalDeleteValue = introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_VALUE_KEY);
9398
}
99+
if (introspectedTable.getTableConfigurationProperty(LOGICAL_UN_DELETE_VALUE_KEY) != null){
100+
this.logicalUnDeleteValue = introspectedTable.getTableConfigurationProperty(LOGICAL_UN_DELETE_VALUE_KEY);
101+
}
94102
// 3. 判断该表是否存在逻辑删除列
95103
this.logicalDeleteColumn = null;
96104
List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
@@ -124,6 +132,11 @@ public void initialized(IntrospectedTable introspectedTable) {
124132
if (introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY) != null && this.logicalDeleteColumn == null){
125133
logger.warn("itfsw(逻辑删除插件):"+introspectedTable.getFullyQualifiedTable()+"没有找到您配置的逻辑删除列("+introspectedTable.getTableConfigurationProperty(LOGICAL_DELETE_COLUMN_KEY)+")!");
126134
}
135+
136+
// 4. 判断逻辑删除值是否配置了
137+
if (this.logicalDeleteColumn != null && (this.logicalDeleteValue == null || this.logicalUnDeleteValue == null)){
138+
logger.warn("itfsw(逻辑删除插件):"+introspectedTable.getFullyQualifiedTable()+"没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值!");
139+
}
127140
}
128141

129142
/**
@@ -330,27 +343,47 @@ public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable intr
330343
@Override
331344
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
332345
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);
338346

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;
339358
if (this.logicalDeleteValue == null || "NULL".equalsIgnoreCase(this.logicalDeleteValue)){
340-
field.setInitializationString("null");
359+
delFlagOnValue = "null";
341360
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getShortNameWithoutTypeArguments().equalsIgnoreCase("String")){
342-
field.setInitializationString("\"" + this.logicalDeleteValue + "\"");
361+
delFlagOnValue = "\"" + this.logicalDeleteValue + "\"";
343362
} 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";
345364
} else {
346-
field.setInitializationString(this.logicalDeleteValue);
365+
delFlagOnValue = this.logicalDeleteValue;
347366
}
348-
CommentTools.addFieldComment(field, introspectedTable);
349-
367+
Field field = JavaElementGeneratorTools.generateStaticFinalField(DEL_FLAG_NAME, this.logicalDeleteColumn.getFullyQualifiedJavaType(), delFlagOnValue, introspectedTable);
350368
// 常量插入到第一位
351-
ArrayList<Field> fields = (ArrayList<Field>) topLevelClass.getFields();
352369
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的常量。");
354387
}
355388
return true;
356389
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2017.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.itfsw.mybatis.generator.plugins.utils;
18+
19+
import org.mybatis.generator.api.IntrospectedTable;
20+
import org.mybatis.generator.api.dom.java.Field;
21+
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
22+
import org.mybatis.generator.api.dom.java.JavaVisibility;
23+
24+
/**
25+
* ---------------------------------------------------------------------------
26+
* Java ele 生成工具
27+
* ---------------------------------------------------------------------------
28+
* @author: hewei
29+
* @time:2017/4/21 16:22
30+
* ---------------------------------------------------------------------------
31+
*/
32+
public class JavaElementGeneratorTools {
33+
public static Field generateStaticFinalField(String fieldName, FullyQualifiedJavaType javaType, String initString, IntrospectedTable introspectedTable){
34+
Field field = new Field(fieldName, javaType);
35+
CommentTools.addFieldComment(field, introspectedTable);
36+
field.setVisibility(JavaVisibility.PUBLIC);
37+
field.setStatic(true);
38+
field.setFinal(true);
39+
if (initString != null){
40+
field.setInitializationString(initString);
41+
}
42+
return field;
43+
}
44+
}

0 commit comments

Comments
 (0)