diff --git a/README.md b/README.md index d92605d..15f7ea8 100644 --- a/README.md +++ b/README.md @@ -282,6 +282,7 @@ public class Test { * Example增强了setOrderByClause方法,新增orderBy(String orderByClause)方法直接返回example,增强链式调用,可以一路.下去了。 * 继续增强orderBy(String orderByClause)方法,增加orderBy(String ... orderByClauses)方法,配合数据Model属性对应Column获取插件(ModelColumnPlugin)使用效果更佳。 * 增加基于column的操作,当配置了[数据Model属性对应Column获取插件(ModelColumnPlugin)](#8-数据model属性对应column获取插件)插件时,提供column之间的比对操作。 +* 增加createCriteria静态方法newAndCreateCriteria简写example的创建。 插件: ```xml @@ -369,6 +370,19 @@ public class Test { .andField1LessThanOrEqualToColumn(Tb.Column.field2) // where field1 <= field2 .example() ); + + // ---------------------------- static createCriteria ----------------------- + // simple + this.tbMapper.selectByExample( + new TbExample() + .createCriteria() + .example() + ); + // new + this.tbMapper.selectByExample( + TbExample.newAndCreateCriteria() + .example() + ); } } ``` diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPlugin.java index 8692d44..a591f73 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPlugin.java @@ -22,6 +22,8 @@ import java.util.List; * --------------------------------------------------------------------------- */ public class ExampleEnhancedPlugin extends BasePlugin { + public static final String METHOD_NEW_AND_CREATE_CRITERIA = "newAndCreateCriteria"; // newAndCreateCriteria 方法 + private boolean enableColumnOperate = false; // 是否启用column的操作 /** @@ -67,11 +69,34 @@ public class ExampleEnhancedPlugin extends BasePlugin { } // orderBy方法 - addOrderByMethodToExample(topLevelClass, introspectedTable); + this.addOrderByMethodToExample(topLevelClass, introspectedTable); + + // createCriteria 静态方法 + this.addStaticCreateCriteriaMethodToExample(topLevelClass, introspectedTable); return true; } + /** + * 添加 createCriteria 静态方法 + * @param topLevelClass + * @param introspectedTable + */ + private void addStaticCreateCriteriaMethodToExample(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { + Method createCriteriaMethod = JavaElementGeneratorTools.generateMethod( + METHOD_NEW_AND_CREATE_CRITERIA, + JavaVisibility.PUBLIC, + FullyQualifiedJavaType.getCriteriaInstance() + ); + commentGenerator.addGeneralMethodComment(createCriteriaMethod, introspectedTable); + + createCriteriaMethod.setStatic(true); + createCriteriaMethod.addBodyLine(topLevelClass.getType().getShortName() + " example = new " + topLevelClass.getType().getShortName() + "();"); + createCriteriaMethod.addBodyLine("return example.createCriteria();"); + + FormatTools.addMethodWithBestPosition(topLevelClass, createCriteriaMethod); + } + /** * 添加列操作方法 * @param topLevelClass diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPluginTest.java index b0a74f1..6176224 100644 --- a/src/test/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPluginTest.java +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPluginTest.java @@ -88,6 +88,27 @@ public class ExampleEnhancedPluginTest { }); } + /** + * 测试静态 createCriteria + * @throws Exception + */ + @Test + public void testStaticCreateCriteria() throws Exception{ + MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ExampleEnhancedPlugin/mybatis-generator.xml"); + tool.generate(new AbstractShellCallback() { + @Override + public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { + ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); + + ObjectUtil tbExampleCriteria = new ObjectUtil(loader.loadClass(packagz + ".TbExample").getMethod(ExampleEnhancedPlugin.METHOD_NEW_AND_CREATE_CRITERIA).invoke(null)); + + // 调用example方法能正常返回 + String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExample", tbExampleCriteria.invoke("example")); + Assert.assertEquals(sql, "select id, field1, field2 from tb"); + } + }); + } + /** * 测试andIf方法 */