Skip to content

Commit 7412b3b

Browse files
author
hewei
committed
解决EnumTypeStatusPlugin和逻辑删除插件在某些情况下冲突的问题
1 parent d740425 commit 7412b3b

File tree

7 files changed

+118
-51
lines changed

7 files changed

+118
-51
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,9 @@ public class Test {
492492
- 查询构造工具中增加逻辑删除条件andLogicalDeleted(boolean);
493493
- 数据Model增加逻辑删除条件andLogicalDeleted(boolean);
494494
- 增加逻辑删除常量IS_DELETED(已删除 默认值)、NOT_DELETED(未删除 默认值)([[issues#11]](https://github.com/itfsw/mybatis-generator-plugin/issues/11));
495-
- 增加逻辑删除枚举,通过注解覆盖逻辑删除配置,具体使用参照[状态枚举生成插件(EnumTypeStatusPlugin)](#21-状态枚举生成插件)
495+
- 增加逻辑删除枚举
496496

497-
>warning: 使用注解生成逻辑删除枚举时,枚举数量必须大于等于2,且第一个代表未删除,第二个代表删除。同时不必配置enumColumns选项,逻辑删除插件使用logicalDeleteColumn覆盖该配置
497+
>warning: 注意在配合[状态枚举生成插件(EnumTypeStatusPlugin)](#21-状态枚举生成插件)使用时的注释格式,枚举数量必须大于等于2,且逻辑删除和未删除的值能在枚举中找到
498498
499499
插件:
500500
```xml

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

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

1919
import com.itfsw.mybatis.generator.plugins.utils.*;
20+
import com.itfsw.mybatis.generator.plugins.utils.hook.ILogicalDeletePluginHook;
2021
import org.mybatis.generator.api.CommentGenerator;
2122
import org.mybatis.generator.api.IntrospectedColumn;
2223
import org.mybatis.generator.api.IntrospectedTable;
2324
import org.mybatis.generator.api.dom.java.*;
25+
import org.mybatis.generator.api.dom.xml.Document;
26+
import org.mybatis.generator.api.dom.xml.XmlElement;
2427
import org.mybatis.generator.internal.util.StringUtility;
2528

2629
import java.math.BigDecimal;
@@ -36,7 +39,8 @@
3639
* @time:2018/11/27 20:36
3740
* ---------------------------------------------------------------------------
3841
*/
39-
public class EnumTypeStatusPlugin extends BasePlugin {
42+
public class EnumTypeStatusPlugin extends BasePlugin implements ILogicalDeletePluginHook {
43+
4044
/**
4145
* 自动扫描
4246
*/
@@ -107,19 +111,6 @@ public void initialized(IntrospectedTable introspectedTable) {
107111
}
108112
}
109113
}
110-
111-
// 逻辑删除插件依赖该插件生成对应枚举
112-
if (PluginTools.checkDependencyPlugin(this.getContext(), LogicalDeletePlugin.class)) {
113-
// 1. 获取配置的逻辑删除列
114-
String logicalDeleteColumnStr = this.getProperties().getProperty(LogicalDeletePlugin.PRO_LOGICAL_DELETE_COLUMN);
115-
if (introspectedTable.getTableConfigurationProperty(LogicalDeletePlugin.PRO_LOGICAL_DELETE_COLUMN) != null) {
116-
logicalDeleteColumnStr = introspectedTable.getTableConfigurationProperty(LogicalDeletePlugin.PRO_LOGICAL_DELETE_COLUMN);
117-
}
118-
IntrospectedColumn logicalDeleteColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, logicalDeleteColumnStr);
119-
if (logicalDeleteColumn != null) {
120-
this.enumColumns.remove(logicalDeleteColumn.getJavaProperty());
121-
}
122-
}
123114
}
124115

125116
/**
@@ -147,6 +138,34 @@ public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, Intros
147138
return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
148139
}
149140

141+
// ======================================= ILogicalDeletePluginHook ======================================
142+
143+
144+
@Override
145+
public boolean clientLogicalDeleteByExampleMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
146+
return true;
147+
}
148+
149+
@Override
150+
public boolean clientLogicalDeleteByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
151+
return true;
152+
}
153+
154+
@Override
155+
public boolean sqlMapLogicalDeleteByExampleElementGenerated(Document document, XmlElement element, IntrospectedColumn logicalDeleteColumn, String logicalDeleteValue, IntrospectedTable introspectedTable) {
156+
return true;
157+
}
158+
159+
@Override
160+
public boolean sqlMapLogicalDeleteByPrimaryKeyElementGenerated(Document document, XmlElement element, IntrospectedColumn logicalDeleteColumn, String logicalDeleteValue, IntrospectedTable introspectedTable) {
161+
return true;
162+
}
163+
164+
@Override
165+
public boolean logicalDeleteEnumGenerated(IntrospectedColumn logicalDeleteColumn) {
166+
return this.enumColumns.containsKey(logicalDeleteColumn.getJavaProperty());
167+
}
168+
150169
/**
151170
* 生成对应enum
152171
* @param topLevelClass

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

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ public class LogicalDeletePlugin extends BasePlugin {
106106
* 逻辑删除枚举
107107
*/
108108
private InnerEnum logicalDeleteEnum;
109+
private int logicalUnDeleteEnumIndex = 0;
110+
private int logicalDeleteEnumIndex = 1;
109111

110112
/**
111113
* 初始化阶段
@@ -116,6 +118,9 @@ public class LogicalDeletePlugin extends BasePlugin {
116118
@Override
117119
public void initialized(IntrospectedTable introspectedTable) {
118120
super.initialized(introspectedTable);
121+
this.logicalUnDeleteEnumIndex = 0;
122+
this.logicalDeleteEnumIndex = 1;
123+
119124
// 1. 获取配置的逻辑删除列
120125
Properties properties = getProperties();
121126
String logicalDeleteColumn = properties.getProperty(PRO_LOGICAL_DELETE_COLUMN);
@@ -139,7 +144,21 @@ public void initialized(IntrospectedTable introspectedTable) {
139144
}
140145
}
141146

142-
// 3. 优先借助 EnumTypeStatusPlugin 插件,去注解里面解析枚举
147+
// 3.判断逻辑删除值是否配置了
148+
this.logicalDeleteValue = properties.getProperty(PRO_LOGICAL_DELETE_VALUE);
149+
this.logicalUnDeleteValue = properties.getProperty(PRO_LOGICAL_UN_DELETE_VALUE);
150+
if (introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_VALUE) != null) {
151+
this.logicalDeleteValue = introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_VALUE);
152+
}
153+
if (introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_UN_DELETE_VALUE) != null) {
154+
this.logicalUnDeleteValue = introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_UN_DELETE_VALUE);
155+
}
156+
if (this.logicalDeleteValue == null || this.logicalUnDeleteValue == null) {
157+
this.logicalDeleteColumn = null;
158+
warnings.add("itfsw(逻辑删除插件):" + introspectedTable.getFullyQualifiedTable() + "没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值!");
159+
}
160+
161+
// 4. 优先借助 EnumTypeStatusPlugin 插件,去注解里面解析枚举
143162
if (this.logicalDeleteColumn != null) {
144163
EnumTypeStatusPlugin.EnumInfo enumInfo = null;
145164
try {
@@ -155,40 +174,45 @@ public void initialized(IntrospectedTable introspectedTable) {
155174

156175
if (enumInfo != null) {
157176
// 这个是注释里配置了枚举
158-
if (enumInfo.hasItems() && enumInfo.getItems().size() >= 2) {
159-
this.logicalUnDeleteValue = enumInfo.getItems().get(0).getOriginalValue();
160-
this.logicalUnDeleteConstName = enumInfo.getItems().get(0).getName();
161-
this.logicalDeleteValue = enumInfo.getItems().get(1).getOriginalValue();
162-
this.logicalDeleteConstName = enumInfo.getItems().get(1).getName();
163-
this.logicalDeleteEnum = enumInfo.generateEnum(commentGenerator, introspectedTable);
164-
} else {
165-
// 没有在注释里配置读取xml中配置的
166-
this.logicalDeleteValue = properties.getProperty(PRO_LOGICAL_DELETE_VALUE);
167-
this.logicalUnDeleteValue = properties.getProperty(PRO_LOGICAL_UN_DELETE_VALUE);
168-
if (introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_VALUE) != null) {
169-
this.logicalDeleteValue = introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_DELETE_VALUE);
170-
}
171-
if (introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_UN_DELETE_VALUE) != null) {
172-
this.logicalUnDeleteValue = introspectedTable.getTableConfigurationProperty(PRO_LOGICAL_UN_DELETE_VALUE);
173-
}
174-
// 4. 判断逻辑删除值是否配置了
175-
if (this.logicalDeleteValue == null || this.logicalUnDeleteValue == null) {
177+
if (enumInfo.hasItems()) {
178+
if (enumInfo.getItems().size() < 2) {
176179
this.logicalDeleteColumn = null;
177-
warnings.add("itfsw(逻辑删除插件):" + introspectedTable.getFullyQualifiedTable() + "没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值!");
180+
warnings.add("itfsw(逻辑删除插件):" + introspectedTable.getFullyQualifiedTable() + "在配合EnumTypeStatusPlugin插件使用时,枚举数量必须大于等于2!");
178181
} else {
179-
// 兼容处理以前一些老用户配置的Long 和 Float配置问题
180-
if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getFullyQualifiedName().equals(Long.class.getName())) {
181-
this.logicalUnDeleteValue = this.logicalUnDeleteValue.replaceAll("L|l", "");
182-
this.logicalDeleteValue = this.logicalDeleteValue.replaceAll("L|l", "");
183-
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getFullyQualifiedName().equals(Float.class.getName())) {
184-
this.logicalUnDeleteValue = this.logicalUnDeleteValue.replaceAll("F|f", "");
185-
this.logicalDeleteValue = this.logicalDeleteValue.replaceAll("F|f", "");
182+
this.logicalDeleteEnumIndex = -1;
183+
this.logicalUnDeleteEnumIndex = -1;
184+
for (int i = 0; i < enumInfo.getItems().size(); i++) {
185+
EnumTypeStatusPlugin.EnumInfo.EnumItemInfo enumItemInfo = enumInfo.getItems().get(i);
186+
if (this.logicalDeleteValue.equals(enumItemInfo.getOriginalValue())) {
187+
this.logicalDeleteEnumIndex = i;
188+
} else if (this.logicalUnDeleteValue.equals(enumItemInfo.getOriginalValue())) {
189+
this.logicalUnDeleteEnumIndex = i;
190+
}
191+
}
192+
if (this.logicalDeleteEnumIndex == -1) {
193+
this.logicalDeleteColumn = null;
194+
warnings.add("itfsw(逻辑删除插件):" + introspectedTable.getFullyQualifiedTable() + "在配合EnumTypeStatusPlugin插件使用时,逻辑删除值“" + this.logicalDeleteValue + "”未在枚举中找到!");
195+
} else if (this.logicalDeleteEnumIndex == -1) {
196+
this.logicalDeleteColumn = null;
197+
warnings.add("itfsw(逻辑删除插件):" + introspectedTable.getFullyQualifiedTable() + "在配合EnumTypeStatusPlugin插件使用时,逻辑未删除值“" + this.logicalUnDeleteValue + "”未在枚举中找到!");
198+
} else {
199+
this.logicalDeleteEnum = enumInfo.generateEnum(commentGenerator, introspectedTable);
186200
}
187201

188-
enumInfo.addItem(this.logicalUnDeleteConstName, "未删除", this.logicalUnDeleteValue);
189-
enumInfo.addItem(this.logicalDeleteConstName, "已删除", this.logicalDeleteValue);
190-
this.logicalDeleteEnum = enumInfo.generateEnum(commentGenerator, introspectedTable);
191202
}
203+
} else {
204+
// 兼容处理以前一些老用户配置的Long 和 Float配置问题
205+
if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getFullyQualifiedName().equals(Long.class.getName())) {
206+
this.logicalUnDeleteValue = this.logicalUnDeleteValue.replaceAll("L|l", "");
207+
this.logicalDeleteValue = this.logicalDeleteValue.replaceAll("L|l", "");
208+
} else if (this.logicalDeleteColumn.getFullyQualifiedJavaType().getFullyQualifiedName().equals(Float.class.getName())) {
209+
this.logicalUnDeleteValue = this.logicalUnDeleteValue.replaceAll("F|f", "");
210+
this.logicalDeleteValue = this.logicalDeleteValue.replaceAll("F|f", "");
211+
}
212+
213+
enumInfo.addItem(this.logicalUnDeleteConstName, "未删除", this.logicalUnDeleteValue);
214+
enumInfo.addItem(this.logicalDeleteConstName, "已删除", this.logicalDeleteValue);
215+
this.logicalDeleteEnum = enumInfo.generateEnum(commentGenerator, introspectedTable);
192216
}
193217
}
194218
}
@@ -455,7 +479,9 @@ public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, Int
455479
// 常量、枚举和逻辑删除方法跟随 逻辑删除列走
456480
if (this.logicalDeleteColumn.getJavaProperty().equals(field.getName())) {
457481
// 1. 添加枚举
458-
topLevelClass.addInnerEnum(this.logicalDeleteEnum);
482+
if (PluginTools.getHook(ILogicalDeletePluginHook.class).logicalDeleteEnumGenerated(this.logicalDeleteColumn) == false) {
483+
topLevelClass.addInnerEnum(this.logicalDeleteEnum);
484+
}
459485
// 2. andLogicalDeleted 方法
460486
Method mAndLogicalDeleted = JavaElementGeneratorTools.generateMethod(
461487
METHOD_LOGICAL_DELETED,
@@ -559,7 +585,7 @@ public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, Introspec
559585
*/
560586
private String getEnumConstantValue(boolean delete) {
561587
if (this.logicalDeleteEnum != null) {
562-
String enumConstant = this.logicalDeleteEnum.getEnumConstants().get(delete ? 1 : 0);
588+
String enumConstant = this.logicalDeleteEnum.getEnumConstants().get(delete ? this.logicalDeleteEnumIndex : this.logicalUnDeleteEnumIndex);
563589
enumConstant = enumConstant.split("\\(")[0];
564590
return this.logicalDeleteEnum.getType().getShortName() + "." + enumConstant + ".value()";
565591
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,11 @@ public boolean sqlMapLogicalDeleteByPrimaryKeyElementGenerated(Document document
499499
return true;
500500
}
501501

502+
@Override
503+
public boolean logicalDeleteEnumGenerated(IntrospectedColumn logicalDeleteColumn) {
504+
return false;
505+
}
506+
502507
// =================================================== 一些生成方法 ========================================
503508

504509
/**

src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,16 @@ public boolean sqlMapLogicalDeleteByPrimaryKeyElementGenerated(Document document
311311
return true;
312312
}
313313

314+
@Override
315+
public boolean logicalDeleteEnumGenerated(IntrospectedColumn logicalDeleteColumn) {
316+
for (ILogicalDeletePluginHook plugin : this.getPlugins(ILogicalDeletePluginHook.class)) {
317+
if (plugin.logicalDeleteEnumGenerated(logicalDeleteColumn)) {
318+
return true;
319+
}
320+
}
321+
return false;
322+
}
323+
314324
// ============================================= ILombokPluginHook ==============================================
315325

316326

src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/ILogicalDeletePluginHook.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,11 @@ public interface ILogicalDeletePluginHook {
7171
* @return
7272
*/
7373
boolean sqlMapLogicalDeleteByPrimaryKeyElementGenerated(Document document, XmlElement element, IntrospectedColumn logicalDeleteColumn, String logicalDeleteValue, IntrospectedTable introspectedTable);
74+
75+
/**
76+
* 逻辑删除枚举是否生成
77+
* @param logicalDeleteColumn
78+
* @return
79+
*/
80+
boolean logicalDeleteEnumGenerated(IntrospectedColumn logicalDeleteColumn);
7481
}

src/test/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePluginTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,17 @@ public void reloadProject(SqlSession sqlSession, ClassLoader loader, String pack
246246
// 验证andLogicalDeleted方法
247247
ObjectUtil tbRemark = new ObjectUtil(loader, packagz + ".TbRemark");
248248
tbRemark.invoke("andLogicalDeleted", true);
249-
Assert.assertEquals(tbRemark.get("delFlag"), (short)0);
250-
tbRemark.invoke("andLogicalDeleted", false);
251249
Assert.assertEquals(tbRemark.get("delFlag"), (short)1);
250+
tbRemark.invoke("andLogicalDeleted", false);
251+
Assert.assertEquals(tbRemark.get("delFlag"), (short)0);
252252

253253
// 验证sql执行
254254
ObjectUtil tbRemarkMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbRemarkMapper")));
255255
ObjectUtil tbRemarkExample = new ObjectUtil(loader, packagz + ".TbRemarkExample");
256256
ObjectUtil criteria = new ObjectUtil(tbRemarkExample.invoke("createCriteria"));
257257
criteria.invoke("andLogicalDeleted", true);
258258
String sql = SqlHelper.getFormatMapperSql(tbRemarkMapper.getObject(), "selectByExample", tbRemarkExample.getObject());
259-
Assert.assertEquals(sql, "select id, del_flag from tb_remark WHERE ( del_flag = '0' )");
259+
Assert.assertEquals(sql, "select id, del_flag from tb_remark WHERE ( del_flag = '1' )");
260260
Object result = tbRemarkMapper.invoke("selectByExample", tbRemarkExample.getObject());
261261
Assert.assertEquals(((List)result).size(), 1);
262262
}

0 commit comments

Comments
 (0)