Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mybatis-generator-plugin
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
mybatis-generator-plugin
Commits
22cca553
Commit
22cca553
authored
Nov 06, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加createCriteria的静态方法和测试用例
parent
fc6f9629
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
1 deletion
+61
-1
README.md
README.md
+14
-0
src/main/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPlugin.java
...tfsw/mybatis/generator/plugins/ExampleEnhancedPlugin.java
+26
-1
src/test/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPluginTest.java
.../mybatis/generator/plugins/ExampleEnhancedPluginTest.java
+21
-0
No files found.
README.md
View file @
22cca553
...
...
@@ -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
()
);
}
}
```
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPlugin.java
View file @
22cca553
...
...
@@ -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
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/ExampleEnhancedPluginTest.java
View file @
22cca553
...
...
@@ -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方法
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment