Skip to content

Commit e49da96

Browse files
author
hewei
committed
增强distinct方法,实现链式调用
1 parent 8b70974 commit e49da96

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, Introspec
9292
// when
9393
addWhenToExample(topLevelClass, introspectedTable);
9494

95+
// 增强链式调用(distinct)
96+
addDistinctMethodToExample(topLevelClass, introspectedTable);
97+
9598
return true;
9699
}
97100

@@ -412,4 +415,27 @@ private void addOrderByMethodToExample(TopLevelClass topLevelClass, Introspected
412415
FormatTools.addMethodWithBestPosition(topLevelClass, orderByMethod1);
413416
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "增加方法orderBy(String ... orderByClauses)");
414417
}
418+
419+
/**
420+
* 增强链式调用(distinct)
421+
* @param topLevelClass
422+
* @param introspectedTable
423+
*/
424+
private void addDistinctMethodToExample(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
425+
// 添加orderBy
426+
Method distinctMethod = JavaElementGeneratorTools.generateMethod(
427+
"distinct",
428+
JavaVisibility.PUBLIC,
429+
topLevelClass.getType(),
430+
new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "distinct")
431+
);
432+
commentGenerator.addGeneralMethodComment(distinctMethod, introspectedTable);
433+
distinctMethod = JavaElementGeneratorTools.generateMethodBody(
434+
distinctMethod,
435+
"this.setDistinct(distinct);",
436+
"return this;"
437+
);
438+
FormatTools.addMethodWithBestPosition(topLevelClass, distinctMethod);
439+
logger.debug("itfsw(Example增强插件):" + topLevelClass.getType().getShortName() + "增加方法distinct");
440+
}
415441
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,28 @@ public void reloadProject(SqlSession sqlSession, ClassLoader loader, String pack
8888
});
8989
}
9090

91+
/**
92+
* 测试生成的distinct方法
93+
*/
94+
@Test
95+
public void testDistinct() throws Exception {
96+
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/ExampleEnhancedPlugin/mybatis-generator.xml");
97+
tool.generate(new AbstractShellCallback() {
98+
@Override
99+
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
100+
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
101+
102+
ObjectUtil tbExample = new ObjectUtil(loader, packagz + ".TbExample");
103+
Object rExample = tbExample.invoke("distinct", true);
104+
105+
Assert.assertEquals(rExample.getClass().getTypeName(), packagz + ".TbExample");
106+
107+
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExample", tbExample.getObject());
108+
Assert.assertEquals(sql, "select distinct id, field1, field2 from tb");
109+
}
110+
});
111+
}
112+
91113
/**
92114
* 测试静态 createCriteria
93115
* @throws Exception

0 commit comments

Comments
 (0)