Skip to content

Commit 91fcd50

Browse files
author
hewei
committed
增加SelectiveEnhancedPlugin插件;
1 parent b479fd3 commit 91fcd50

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Maven引用:
2323
<artifactId>mybatis-generator-plugin</artifactId>
2424
<!-- 稳定版本 -->
2525
<version>1.0.6</version>
26-
<!-- 快照版本(最新提交到中央库,增加了部分功能,未完成测试) -->
26+
<!-- 快照版本(最新提交到中央库,增加了部分功能,欢迎测试反馈) -->
2727
<version>1.0.7-SNAPSHOT</version>
2828
</dependency>
2929
```

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public boolean validate(List<String> warnings) {
6161
return false;
6262
}
6363

64+
// 插件配置位置最好是在末尾
65+
if (PluginTools.getConfigPlugins(getContext()).size() - 1 != PluginTools.getPluginIndex(SelectiveEnhancedPlugin.class, getContext())){
66+
logger.warn("itfsw:插件" + this.getClass().getTypeName() + "插件建议配置在所有插件末尾以便最后调用,否则某些Selective方法得不到增强!");
67+
}
68+
6469
return true;
6570
}
6671

src/main/java/com/itfsw/mybatis/generator/plugins/utils/PluginTools.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.slf4j.LoggerFactory;
2323

2424
import java.lang.reflect.Field;
25+
import java.util.ArrayList;
2526
import java.util.List;
2627

2728
/**
@@ -37,27 +38,47 @@ public class PluginTools {
3738

3839
/**
3940
* 检查插件依赖
41+
* @param plugin 插件
42+
* @param ctx 上下文
43+
* @return
44+
*/
45+
public static boolean checkDependencyPlugin(Class plugin, Context ctx) {
46+
return getPluginIndex(plugin, ctx) >= 0;
47+
}
48+
49+
/**
50+
* 获取插件所在位置
4051
*
4152
* @param plugin 插件
4253
* @param ctx 上下文
54+
* @return -1:未找到
55+
*/
56+
public static int getPluginIndex(Class plugin, Context ctx) {
57+
List<PluginConfiguration> list = getConfigPlugins(ctx);
58+
// 检查是否配置了ModelColumnPlugin插件
59+
for (int i = 0; i < list.size(); i++) {
60+
PluginConfiguration config = list.get(i);
61+
if (plugin.getName().equals(config.getConfigurationType())) {
62+
return i;
63+
}
64+
}
65+
return -1;
66+
}
67+
68+
/**
69+
* 获取插件列表
70+
* @param ctx 上下文
4371
* @return
4472
*/
45-
public static boolean checkDependencyPlugin(Class plugin, Context ctx){
73+
public static List<PluginConfiguration> getConfigPlugins(Context ctx) {
4674
try {
4775
// 利用反射获取pluginConfigurations属性
4876
Field field = Context.class.getDeclaredField("pluginConfigurations");
4977
field.setAccessible(true);
50-
List<PluginConfiguration> list = (List<PluginConfiguration>) field.get(ctx);
51-
// 检查是否配置了ModelColumnPlugin插件
52-
for (PluginConfiguration config: list) {
53-
if (plugin.getName().equals(config.getConfigurationType())){
54-
return true;
55-
}
56-
}
78+
return (List<PluginConfiguration>) field.get(ctx);
5779
} catch (Exception e) {
5880
logger.error("插件检查反射异常", e);
5981
}
60-
61-
return false;
82+
return new ArrayList<>();
6283
}
6384
}

0 commit comments

Comments
 (0)