1010import org .mybatis .generator .api .IntrospectedTable ;
1111import org .mybatis .generator .api .dom .java .*;
1212import org .mybatis .generator .codegen .mybatis3 .MyBatis3FormattingUtilities ;
13+ import org .mybatis .generator .internal .util .StringUtility ;
1314
1415import java .util .List ;
1516
2223 * ---------------------------------------------------------------------------
2324 */
2425public class ExampleEnhancedPlugin extends BasePlugin {
25- public static final String METHOD_NEW_AND_CREATE_CRITERIA = "newAndCreateCriteria" ; // newAndCreateCriteria 方法
26-
27- private boolean enableColumnOperate = false ; // 是否启用column的操作
26+ // newAndCreateCriteria 方法
27+ public static final String METHOD_NEW_AND_CREATE_CRITERIA = "newAndCreateCriteria" ;
28+ // 逻辑删除列-Key
29+ public static final String PRO_ENABLE_AND_IF = "enableAndIf" ;
30+ // 是否启用column的操作
31+ private boolean enableColumnOperate = false ;
32+ // 是否启用了过期的andIf
33+ private boolean enableAndIf ;
2834
2935 /**
3036 * {@inheritDoc}
@@ -34,6 +40,8 @@ public class ExampleEnhancedPlugin extends BasePlugin {
3440 public void initialized (IntrospectedTable introspectedTable ) {
3541 super .initialized (introspectedTable );
3642 this .enableColumnOperate = PluginTools .checkDependencyPlugin (context , ModelColumnPlugin .class );
43+ String enableAndIf = properties .getProperty (PRO_ENABLE_AND_IF );
44+ this .enableAndIf = enableAndIf == null ? true : StringUtility .isTrue (enableAndIf );
3745 }
3846
3947 /**
@@ -46,12 +54,17 @@ public void initialized(IntrospectedTable introspectedTable) {
4654 @ Override
4755 public boolean modelExampleClassGenerated (TopLevelClass topLevelClass , IntrospectedTable introspectedTable ) {
4856 List <InnerClass > innerClasses = topLevelClass .getInnerClasses ();
49- for (InnerClass innerClass : innerClasses ) {
57+ for (int i = 0 ; i < innerClasses .size (); i ++) {
58+ InnerClass innerClass = innerClasses .get (i );
5059 if ("Criteria" .equals (innerClass .getType ().getShortName ())) {
5160 // 工厂方法
5261 addFactoryMethodToCriteria (topLevelClass , innerClass , introspectedTable );
5362 // andIf
54- addAndIfMethodToCriteria (topLevelClass , innerClass , introspectedTable );
63+ if (this .enableAndIf ) {
64+ addAndIfMethodToCriteria (topLevelClass , innerClass , introspectedTable );
65+ }
66+ // when
67+ addWhenToCriteria (topLevelClass , innerClass , introspectedTable );
5568 } else if ("GeneratedCriteria" .equals (innerClass .getType ().getShortName ())) {
5669 // column 方法
5770 if (this .enableColumnOperate ) {
@@ -62,10 +75,12 @@ public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, Introspec
6275
6376 List <Method > methods = topLevelClass .getMethods ();
6477 for (Method method : methods ) {
65- if (!"createCriteriaInternal" .equals (method .getName ()))
78+ if (!"createCriteriaInternal" .equals (method .getName ())) {
6679 continue ;
67- method .getBodyLines ().set (0 , "Criteria criteria = new Criteria(this);" );
68- logger .debug ("itfsw(Example增强插件):" + topLevelClass .getType ().getShortName () + "修改createCriteriaInternal方法,修改构造Criteria时传入Example对象" );
80+ } else {
81+ method .getBodyLines ().set (0 , "Criteria criteria = new Criteria(this);" );
82+ logger .debug ("itfsw(Example增强插件):" + topLevelClass .getType ().getShortName () + "修改createCriteriaInternal方法,修改构造Criteria时传入Example对象" );
83+ }
6984 }
7085
7186 // orderBy方法
@@ -74,6 +89,9 @@ public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, Introspec
7489 // createCriteria 静态方法
7590 this .addStaticCreateCriteriaMethodToExample (topLevelClass , introspectedTable );
7691
92+ // when
93+ addWhenToExample (topLevelClass , introspectedTable );
94+
7795 return true ;
7896 }
7997
@@ -215,17 +233,101 @@ private void addFactoryMethodToCriteria(TopLevelClass topLevelClass, InnerClass
215233 logger .debug ("itfsw(Example增强插件):" + topLevelClass .getType ().getShortName () + "." + innerClass .getType ().getShortName () + "增加工厂方法example" );
216234 }
217235
236+ /**
237+ * 增强Criteria的链式调用(when)
238+ * @param topLevelClass
239+ * @param innerClass
240+ * @param introspectedTable
241+ */
242+ private void addWhenToCriteria (TopLevelClass topLevelClass , InnerClass innerClass , IntrospectedTable introspectedTable ) {
243+ this .addWhenToClass (topLevelClass , innerClass , introspectedTable , "criteria" );
244+ }
245+
246+ /**
247+ * 增强Example的链式调用(when)
248+ * @param topLevelClass
249+ * @param introspectedTable
250+ */
251+ private void addWhenToExample (TopLevelClass topLevelClass , IntrospectedTable introspectedTable ) {
252+ this .addWhenToClass (topLevelClass , topLevelClass , introspectedTable , "example" );
253+ }
254+
255+ /**
256+ * 增强链式调用(when)
257+ * @param topLevelClass
258+ * @param clazz
259+ * @param introspectedTable
260+ */
261+ private void addWhenToClass (TopLevelClass topLevelClass , InnerClass clazz , IntrospectedTable introspectedTable , String type ) {
262+ // 添加接口When
263+ InnerInterface whenInterface = new InnerInterface ("I" + FormatTools .upFirstChar (type ) + "When" );
264+ whenInterface .setVisibility (JavaVisibility .PUBLIC );
265+
266+ // ICriteriaAdd增加接口add
267+ Method addMethod = JavaElementGeneratorTools .generateMethod (
268+ type ,
269+ JavaVisibility .DEFAULT ,
270+ null ,
271+ new Parameter (clazz .getType (), type )
272+ );
273+ commentGenerator .addGeneralMethodComment (addMethod , introspectedTable );
274+ whenInterface .addMethod (addMethod );
275+
276+ InnerClass innerClassWrapper = new InnerInterfaceWrapperToInnerClass (whenInterface );
277+ // 添加注释
278+ commentGenerator .addClassComment (innerClassWrapper , introspectedTable );
279+ topLevelClass .addInnerClass (innerClassWrapper );
280+
281+ // 添加when方法
282+ Method whenMethod = JavaElementGeneratorTools .generateMethod (
283+ "when" ,
284+ JavaVisibility .PUBLIC ,
285+ clazz .getType (),
286+ new Parameter (FullyQualifiedJavaType .getBooleanPrimitiveInstance (), "condition" ),
287+ new Parameter (whenInterface .getType (), "then" )
288+ );
289+ commentGenerator .addGeneralMethodComment (whenMethod , introspectedTable );
290+ whenMethod = JavaElementGeneratorTools .generateMethodBody (
291+ whenMethod ,
292+ "if (condition) {" ,
293+ "then." + type + "(this);" ,
294+ "}" ,
295+ "return this;"
296+ );
297+ FormatTools .addMethodWithBestPosition (clazz , whenMethod );
298+ Method whenOtherwiseMethod = JavaElementGeneratorTools .generateMethod (
299+ "when" ,
300+ JavaVisibility .PUBLIC ,
301+ clazz .getType (),
302+ new Parameter (FullyQualifiedJavaType .getBooleanPrimitiveInstance (), "condition" ),
303+ new Parameter (whenInterface .getType (), "then" ),
304+ new Parameter (whenInterface .getType (), "otherwise" )
305+ );
306+ commentGenerator .addGeneralMethodComment (whenOtherwiseMethod , introspectedTable );
307+ whenOtherwiseMethod = JavaElementGeneratorTools .generateMethodBody (
308+ whenOtherwiseMethod ,
309+ "if (condition) {" ,
310+ "then." + type + "(this);" ,
311+ "} else {" ,
312+ "otherwise." + type + "(this);" ,
313+ "}" ,
314+ "return this;"
315+ );
316+ FormatTools .addMethodWithBestPosition (clazz , whenOtherwiseMethod );
317+ }
218318
219319 /**
220320 * 增强Criteria的链式调用,添加andIf(boolean addIf, CriteriaAdd add)方法,实现链式调用中按条件增加查询语句
221321 * @param topLevelClass
222322 * @param innerClass
223323 * @param introspectedTable
224324 */
325+ @ Deprecated
225326 private void addAndIfMethodToCriteria (TopLevelClass topLevelClass , InnerClass innerClass , IntrospectedTable introspectedTable ) {
226327 // 添加接口CriteriaAdd
227328 InnerInterface criteriaAddInterface = new InnerInterface ("ICriteriaAdd" );
228329 criteriaAddInterface .setVisibility (JavaVisibility .PUBLIC );
330+ criteriaAddInterface .addAnnotation ("@Deprecated" );
229331 logger .debug ("itfsw(Example增强插件):" + topLevelClass .getType ().getShortName () + "." + innerClass .getType ().getShortName () + "增加接口ICriteriaAdd" );
230332
231333 // ICriteriaAdd增加接口add
@@ -252,6 +354,7 @@ private void addAndIfMethodToCriteria(TopLevelClass topLevelClass, InnerClass in
252354 new Parameter (FullyQualifiedJavaType .getBooleanPrimitiveInstance (), "ifAdd" ),
253355 new Parameter (criteriaAddInterface .getType (), "add" )
254356 );
357+ andIfMethod .addAnnotation ("@Deprecated" );
255358 commentGenerator .addGeneralMethodComment (andIfMethod , introspectedTable );
256359 andIfMethod = JavaElementGeneratorTools .generateMethodBody (
257360 andIfMethod ,
0 commit comments