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
a8a788f1
Commit
a8a788f1
authored
Apr 24, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重新整合 IncrementsPlugin 插件
parent
a8c0a38a
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
430 additions
and
103 deletions
+430
-103
src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
...com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
+2
-2
src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
...sw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
+51
-16
src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
...ava/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
+102
-73
src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java
...ybatis/generator/plugins/utils/IncrementsPluginTools.java
+111
-6
src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
...itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
+97
-3
src/test/java/com/itfsw/mybatis/generator/plugins/OldSelectiveEnhancedPluginTest.java
...tis/generator/plugins/OldSelectiveEnhancedPluginTest.java
+1
-1
src/test/java/com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java
...com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java
+2
-2
src/test/resources/scripts/IncrementsPlugin/mybatis-generator-with-selective-enhanced-plugin.xml
...ugin/mybatis-generator-with-selective-enhanced-plugin.xml
+64
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
View file @
a8a788f1
...
@@ -195,7 +195,7 @@ public class IncrementsPlugin extends BasePlugin {
...
@@ -195,7 +195,7 @@ public class IncrementsPlugin extends BasePlugin {
// 查找是否需要进行增量操作
// 查找是否需要进行增量操作
if
(
incTools
.
supportColumn
(
introspectedColumn
))
{
if
(
incTools
.
supportColumn
(
introspectedColumn
))
{
xmlElement
.
getElements
().
clear
();
xmlElement
.
getElements
().
clear
();
xmlElement
.
getElements
().
addAll
(
incTools
.
generatedIncrementsElement
(
introspectedColumn
,
hasPrefix
,
true
));
xmlElement
.
getElements
().
addAll
(
incTools
.
generatedIncrementsElement
(
introspectedColumn
,
hasPrefix
?
"record."
:
null
,
true
));
}
}
}
}
}
}
...
@@ -223,7 +223,7 @@ public class IncrementsPlugin extends BasePlugin {
...
@@ -223,7 +223,7 @@ public class IncrementsPlugin extends BasePlugin {
IntrospectedColumn
introspectedColumn
=
IntrospectedTableTools
.
safeGetColumn
(
introspectedTable
,
columnName
);
IntrospectedColumn
introspectedColumn
=
IntrospectedTableTools
.
safeGetColumn
(
introspectedTable
,
columnName
);
// 查找判断是否需要进行节点替换
// 查找判断是否需要进行节点替换
if
(
incTools
.
supportColumn
(
introspectedColumn
))
{
if
(
incTools
.
supportColumn
(
introspectedColumn
))
{
newEles
.
addAll
(
incTools
.
generatedIncrementsElement
(
introspectedColumn
,
hasPrefix
,
text
.
endsWith
(
","
)));
newEles
.
addAll
(
incTools
.
generatedIncrementsElement
(
introspectedColumn
,
hasPrefix
?
"record."
:
null
,
text
.
endsWith
(
","
)));
continue
;
continue
;
}
}
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
View file @
a8a788f1
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.IncrementsPluginTools
;
import
com.itfsw.mybatis.generator.plugins.utils.PluginTools
;
import
com.itfsw.mybatis.generator.plugins.utils.PluginTools
;
import
com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools
;
import
com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedColumn
;
...
@@ -200,7 +201,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
...
@@ -200,7 +201,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
answer
.
addElement
(
insertChooseEle
);
answer
.
addElement
(
insertChooseEle
);
XmlElement
insertWhenEle
=
new
XmlElement
(
"when"
);
XmlElement
insertWhenEle
=
new
XmlElement
(
"when"
);
insertWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective.length > 0"
));
insertWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective
!= null and selective
.length > 0"
));
insertChooseEle
.
addElement
(
insertWhenEle
);
insertChooseEle
.
addElement
(
insertWhenEle
);
XmlElement
insertForeachEle
=
new
XmlElement
(
"foreach"
);
XmlElement
insertForeachEle
=
new
XmlElement
(
"foreach"
);
...
@@ -230,7 +231,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
...
@@ -230,7 +231,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
answer
.
addElement
(
valuesChooseEle
);
answer
.
addElement
(
valuesChooseEle
);
XmlElement
valuesWhenEle
=
new
XmlElement
(
"when"
);
XmlElement
valuesWhenEle
=
new
XmlElement
(
"when"
);
valuesWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective.length > 0"
));
valuesWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective
!= null and selective
.length > 0"
));
valuesChooseEle
.
addElement
(
valuesWhenEle
);
valuesChooseEle
.
addElement
(
valuesWhenEle
);
XmlElement
valuesForeachEle
=
new
XmlElement
(
"foreach"
);
XmlElement
valuesForeachEle
=
new
XmlElement
(
"foreach"
);
...
@@ -267,6 +268,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
...
@@ -267,6 +268,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
*/
*/
@Override
@Override
public
boolean
sqlMapUpdateByExampleSelectiveElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
public
boolean
sqlMapUpdateByExampleSelectiveElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
IncrementsPluginTools
incTools
=
IncrementsPluginTools
.
getTools
(
context
,
introspectedTable
,
warnings
);
// 清空
// 清空
XmlElement
answer
=
element
;
XmlElement
answer
=
element
;
answer
.
getElements
().
clear
();
answer
.
getElements
().
clear
();
...
@@ -283,26 +285,43 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
...
@@ -283,26 +285,43 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
answer
.
addElement
(
new
TextElement
(
sb
.
toString
()));
answer
.
addElement
(
new
TextElement
(
sb
.
toString
()));
// selective
// selective
answer
.
addElement
(
new
TextElement
(
"SET"
));
XmlElement
setChooseEle
=
new
XmlElement
(
"choose"
);
XmlElement
setChooseEle
=
new
XmlElement
(
"choose"
);
answer
.
addElement
(
setChooseEle
);
answer
.
addElement
(
setChooseEle
);
XmlElement
setWhenEle
=
new
XmlElement
(
"when"
);
XmlElement
setWhenEle
=
new
XmlElement
(
"when"
);
setWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective.length > 0"
));
setWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective
!= null and selective
.length > 0"
));
setChooseEle
.
addElement
(
setWhenEle
);
setChooseEle
.
addElement
(
setWhenEle
);
XmlElement
setForeachEle
=
new
XmlElement
(
"foreach"
);
XmlElement
setForeachEle
=
new
XmlElement
(
"foreach"
);
setForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"open"
,
"SET"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
setForeachEle
.
addElement
(
new
TextElement
(
"${column.value} = #{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
// 自增插件支持
if
(
incTools
.
support
())
{
incTools
.
generateSetsSelectiveWithSelectiveEnhancedPlugin
(
setForeachEle
);
}
else
{
setForeachEle
.
addElement
(
new
TextElement
(
"${column.value} = #{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
}
setWhenEle
.
addElement
(
setForeachEle
);
setWhenEle
.
addElement
(
setForeachEle
);
XmlElement
setOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
XmlElement
setOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
setOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateSetsSelective
(
ListUtilities
.
removeGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
()),
// 自增插件支持
"record."
if
(
incTools
.
support
())
{
));
setOtherwiseEle
.
addElement
(
incTools
.
generateSetsSelective
(
ListUtilities
.
removeGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
()),
"record."
,
false
));
}
else
{
setOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateSetsSelective
(
ListUtilities
.
removeGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
()),
"record."
));
}
setChooseEle
.
addElement
(
setOtherwiseEle
);
setChooseEle
.
addElement
(
setOtherwiseEle
);
answer
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
answer
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
...
@@ -319,6 +338,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
...
@@ -319,6 +338,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
*/
*/
@Override
@Override
public
boolean
sqlMapUpdateByPrimaryKeySelectiveElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
public
boolean
sqlMapUpdateByPrimaryKeySelectiveElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
IncrementsPluginTools
incTools
=
IncrementsPluginTools
.
getTools
(
context
,
introspectedTable
,
warnings
);
// 清空
// 清空
XmlElement
answer
=
element
;
XmlElement
answer
=
element
;
answer
.
getElements
().
clear
();
answer
.
getElements
().
clear
();
...
@@ -336,27 +356,42 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
...
@@ -336,27 +356,42 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
answer
.
addElement
(
new
TextElement
(
sb
.
toString
()));
answer
.
addElement
(
new
TextElement
(
sb
.
toString
()));
// selective
// selective
answer
.
addElement
(
new
TextElement
(
"SET"
));
XmlElement
setChooseEle
=
new
XmlElement
(
"choose"
);
XmlElement
setChooseEle
=
new
XmlElement
(
"choose"
);
answer
.
addElement
(
setChooseEle
);
answer
.
addElement
(
setChooseEle
);
XmlElement
setWhenEle
=
new
XmlElement
(
"when"
);
XmlElement
setWhenEle
=
new
XmlElement
(
"when"
);
setWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective.length > 0"
));
setWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective
!= null and selective
.length > 0"
));
setChooseEle
.
addElement
(
setWhenEle
);
setChooseEle
.
addElement
(
setWhenEle
);
XmlElement
setForeachEle
=
new
XmlElement
(
"foreach"
);
XmlElement
setForeachEle
=
new
XmlElement
(
"foreach"
);
setForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"open"
,
"SET"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
setForeachEle
.
addElement
(
new
TextElement
(
"${column.value} = #{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
// 自增插件支持
if
(
incTools
.
support
())
{
incTools
.
generateSetsSelectiveWithSelectiveEnhancedPlugin
(
setForeachEle
);
}
else
{
setForeachEle
.
addElement
(
new
TextElement
(
"${column.value} = #{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
}
setWhenEle
.
addElement
(
setForeachEle
);
setWhenEle
.
addElement
(
setForeachEle
);
XmlElement
setOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
XmlElement
setOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
setOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateSetsSelective
(
ListUtilities
.
removeGeneratedAlwaysColumns
(
introspectedTable
.
getNonPrimaryKeyColumns
()),
"record."
));
setChooseEle
.
addElement
(
setOtherwiseEle
);
setChooseEle
.
addElement
(
setOtherwiseEle
);
// 自增插件支持
if
(
incTools
.
support
())
{
setOtherwiseEle
.
addElement
(
incTools
.
generateSetsSelective
(
ListUtilities
.
removeGeneratedAlwaysColumns
(
introspectedTable
.
getNonPrimaryKeyColumns
()),
"record."
,
false
));
}
else
{
setOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateSetsSelective
(
ListUtilities
.
removeGeneratedAlwaysColumns
(
introspectedTable
.
getNonPrimaryKeyColumns
()),
"record."
));
}
XmlElementGeneratorTools
.
generateWhereByPrimaryKeyTo
(
answer
,
introspectedTable
.
getPrimaryKeyColumns
(),
"record."
);
XmlElementGeneratorTools
.
generateWhereByPrimaryKeyTo
(
answer
,
introspectedTable
.
getPrimaryKeyColumns
(),
"record."
);
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
View file @
a8a788f1
This diff is collapsed.
Click to expand it.
src/main/java/com/itfsw/mybatis/generator/plugins/utils/IncrementsPluginTools.java
View file @
a8a788f1
...
@@ -32,6 +32,7 @@ import org.slf4j.Logger;
...
@@ -32,6 +32,7 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.List
;
/**
/**
...
@@ -125,13 +126,85 @@ public class IncrementsPluginTools {
...
@@ -125,13 +126,85 @@ public class IncrementsPluginTools {
return
false
;
return
false
;
}
}
/**
* 生成sets Selective Ele
* @param columns
* @param prefix
* @param bracket
* @return
*/
public
XmlElement
generateSetsSelective
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
,
boolean
bracket
)
{
XmlElement
eleTrim
=
new
XmlElement
(
"trim"
);
if
(
bracket
)
{
eleTrim
.
addAttribute
(
new
Attribute
(
"prefix"
,
"("
));
eleTrim
.
addAttribute
(
new
Attribute
(
"suffix"
,
")"
));
eleTrim
.
addAttribute
(
new
Attribute
(
"suffixOverrides"
,
","
));
}
else
{
eleTrim
.
addAttribute
(
new
Attribute
(
"suffixOverrides"
,
","
));
}
for
(
IntrospectedColumn
introspectedColumn
:
columns
)
{
XmlElement
eleIf
=
new
XmlElement
(
"if"
);
eleIf
.
addAttribute
(
new
Attribute
(
"test"
,
introspectedColumn
.
getJavaProperty
(
prefix
)
+
" != null"
));
if
(
this
.
supportColumn
(
introspectedColumn
))
{
for
(
Element
ele
:
this
.
generatedIncrementsElement
(
introspectedColumn
,
prefix
,
true
))
{
eleIf
.
addElement
(
ele
);
}
}
else
{
eleIf
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
" = "
+
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
)
+
","
));
}
eleTrim
.
addElement
(
eleIf
);
}
return
eleTrim
;
}
/**
* 生成sets Ele
* @param columns
* @param prefix
* @param bracket
* @return
*/
public
List
<
Element
>
generateSets
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
,
boolean
bracket
)
{
List
<
Element
>
list
=
new
ArrayList
<>();
if
(
bracket
)
{
list
.
add
(
new
TextElement
(
"("
));
}
Iterator
<
IntrospectedColumn
>
columnIterator
=
columns
.
iterator
();
while
(
columnIterator
.
hasNext
())
{
IntrospectedColumn
introspectedColumn
=
columnIterator
.
next
();
if
(
this
.
supportColumn
(
introspectedColumn
))
{
list
.
add
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
" = "
));
for
(
Element
ele
:
this
.
generatedIncrementsElement
(
introspectedColumn
,
prefix
,
columnIterator
.
hasNext
()))
{
list
.
add
(
ele
);
}
}
else
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
));
sb
.
append
(
" = "
);
sb
.
append
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
));
if
(
columnIterator
.
hasNext
())
{
sb
.
append
(
", "
);
}
list
.
add
(
new
TextElement
(
sb
.
toString
()));
}
}
if
(
bracket
)
{
list
.
add
(
new
TextElement
(
")"
));
}
return
list
;
}
/**
/**
* 生成增量操作节点
* 生成增量操作节点
* @param introspectedColumn
* @param introspectedColumn
* @param
hasP
refix
* @param
p
refix
* @param hasComma
* @param hasComma
*/
*/
public
List
<
Element
>
generatedIncrementsElement
(
IntrospectedColumn
introspectedColumn
,
boolean
hasP
refix
,
boolean
hasComma
)
{
public
List
<
Element
>
generatedIncrementsElement
(
IntrospectedColumn
introspectedColumn
,
String
p
refix
,
boolean
hasComma
)
{
List
<
Element
>
list
=
new
ArrayList
<>();
List
<
Element
>
list
=
new
ArrayList
<>();
// 1. column = 节点
// 1. column = 节点
...
@@ -145,20 +218,20 @@ public class IncrementsPluginTools {
...
@@ -145,20 +218,20 @@ public class IncrementsPluginTools {
XmlElement
when
=
new
XmlElement
(
"when"
);
XmlElement
when
=
new
XmlElement
(
"when"
);
when
.
addAttribute
(
new
Attribute
(
when
.
addAttribute
(
new
Attribute
(
"test"
,
"test"
,
(
hasPrefix
?
"record."
:
"_parameter."
)
+
IncrementsPlugin
.
METHOD_INC_CHECK
(
prefix
!=
null
?
prefix
:
"_parameter."
)
+
IncrementsPlugin
.
METHOD_INC_CHECK
+
"('"
+
MyBatis3FormattingUtilities
.
escapeStringForMyBatis3
(
introspectedColumn
.
getActualColumnName
())
+
"')"
+
"('"
+
MyBatis3FormattingUtilities
.
escapeStringForMyBatis3
(
introspectedColumn
.
getActualColumnName
())
+
"')"
));
));
TextElement
spec
=
new
TextElement
(
TextElement
spec
=
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
" ${"
+
(
hasPrefix
?
"record."
:
""
)
+
" ${"
+
(
prefix
!=
null
?
prefix
:
""
)
+
IncrementsPlugin
.
FIELD_INC_MAP
+
"."
+
MyBatis3FormattingUtilities
.
escapeStringForMyBatis3
(
introspectedColumn
.
getActualColumnName
())
+
".value} "
+
IncrementsPlugin
.
FIELD_INC_MAP
+
"."
+
MyBatis3FormattingUtilities
.
escapeStringForMyBatis3
(
introspectedColumn
.
getActualColumnName
())
+
".value} "
+
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
hasPrefix
?
"record."
:
null
));
+
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
));
when
.
addElement
(
spec
);
when
.
addElement
(
spec
);
choose
.
addElement
(
when
);
choose
.
addElement
(
when
);
// 启用了增量操作
// 启用了增量操作
XmlElement
otherwise
=
new
XmlElement
(
"otherwise"
);
XmlElement
otherwise
=
new
XmlElement
(
"otherwise"
);
TextElement
normal
=
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
hasPrefix
?
"record."
:
null
));
TextElement
normal
=
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
));
otherwise
.
addElement
(
normal
);
otherwise
.
addElement
(
normal
);
choose
.
addElement
(
otherwise
);
choose
.
addElement
(
otherwise
);
...
@@ -171,4 +244,36 @@ public class IncrementsPluginTools {
...
@@ -171,4 +244,36 @@ public class IncrementsPluginTools {
return
list
;
return
list
;
}
}
/**
* 创建 sets (SelectiveEnhancedPlugin)
* @param setForeachEle
*/
public
void
generateSetsSelectiveWithSelectiveEnhancedPlugin
(
XmlElement
setForeachEle
)
{
XmlElement
choose
=
new
XmlElement
(
"choose"
);
for
(
IntrospectedColumn
introspectedColumn
:
columns
)
{
XmlElement
when
=
new
XmlElement
(
"when"
);
// 需要 inc 的列
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"'"
);
sb
.
append
(
introspectedColumn
.
getActualColumnName
());
sb
.
append
(
"'.toString()"
);
sb
.
append
(
" == "
);
sb
.
append
(
"column.value"
);
when
.
addAttribute
(
new
Attribute
(
"test"
,
sb
.
toString
()));
when
.
addElement
(
new
TextElement
(
"${column.value} = ${column.value} ${record.incrementsColumnsInfoMap."
+
introspectedColumn
.
getActualColumnName
()
+
".value} #{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
choose
.
addElement
(
when
);
}
XmlElement
otherwise
=
new
XmlElement
(
"otherwise"
);
otherwise
.
addElement
(
new
TextElement
(
"${column.value} = #{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
choose
.
addElement
(
otherwise
);
setForeachEle
.
addElement
(
choose
);
}
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
View file @
a8a788f1
...
@@ -19,12 +19,13 @@ package com.itfsw.mybatis.generator.plugins;
...
@@ -19,12 +19,13 @@ package com.itfsw.mybatis.generator.plugins;
import
com.itfsw.mybatis.generator.plugins.tools.*
;
import
com.itfsw.mybatis.generator.plugins.tools.*
;
import
org.apache.ibatis.session.SqlSession
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.junit.Before
Class
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.mybatis.generator.exception.InvalidConfigurationException
;
import
org.mybatis.generator.exception.InvalidConfigurationException
;
import
org.mybatis.generator.exception.XMLParserException
;
import
org.mybatis.generator.exception.XMLParserException
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.lang.reflect.Array
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Method
;
import
java.sql.ResultSet
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
...
@@ -44,8 +45,8 @@ public class IncrementsPluginTest {
...
@@ -44,8 +45,8 @@ public class IncrementsPluginTest {
/**
/**
* 初始化数据库
* 初始化数据库
*/
*/
@Before
Class
@Before
public
static
void
init
()
throws
Exception
{
public
void
init
()
throws
Exception
{
DBHelper
.
createDB
(
"scripts/IncrementsPlugin/init.sql"
);
DBHelper
.
createDB
(
"scripts/IncrementsPlugin/init.sql"
);
}
}
...
@@ -211,6 +212,99 @@ public class IncrementsPluginTest {
...
@@ -211,6 +212,99 @@ public class IncrementsPluginTest {
});
});
}
}
/**
* 测试整合 SelectiveEnhancedPlugin 插件
*/
@Test
public
void
testWithSelectiveEnhancedPlugin
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
InterruptedException
,
SQLException
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/IncrementsPlugin/mybatis-generator-with-selective-enhanced-plugin.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
// 1. 测试updateByExampleSelective
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
packagz
+
".TbExample"
);
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andIdEqualTo"
,
3
l
);
ObjectUtil
tbBuilder
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder"
);
ObjectUtil
tbBuilderInc
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder$Inc#INC"
);
tbBuilder
.
invoke
(
"incF1"
,
100
l
,
tbBuilderInc
.
getObject
());
tbBuilder
.
invoke
(
"incF2"
,
200
l
);
// selective
ObjectUtil
TbColumnField1
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#field1"
);
ObjectUtil
TbColumnIncF1
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#incF1"
);
ObjectUtil
TbColumnIncF2
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#incF2"
);
Object
columns
=
Array
.
newInstance
(
TbColumnField1
.
getCls
(),
3
);
Array
.
set
(
columns
,
0
,
TbColumnField1
.
getObject
());
Array
.
set
(
columns
,
1
,
TbColumnIncF1
.
getObject
());
Array
.
set
(
columns
,
2
,
TbColumnIncF2
.
getObject
());
// sql
// 非空判断
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"updateByExampleSelective"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb SET inc_f1 = inc_f1 + 100 , inc_f2 = 200 WHERE ( id = '3' )"
);
// selective 指定
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"updateByExampleSelective"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
(),
columns
);
Assert
.
assertEquals
(
sql
,
"update tb SET field1 = 'null' , inc_f1 = inc_f1 + 100 , inc_f2 = 200 WHERE ( id = '3' )"
);
// 执行
// inc_f1 增加100
Object
result
=
tbMapper
.
invoke
(
"updateByExampleSelective"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
(),
columns
);
Assert
.
assertEquals
(
result
,
1
);
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select inc_f1 from tb where id = 3"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
103
);
// inc_f1 再减去50
ObjectUtil
tbBuilderDec
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder$Inc#DEC"
);
tbBuilder
.
invoke
(
"incF1"
,
50
l
,
tbBuilderDec
.
getObject
());
result
=
tbMapper
.
invoke
(
"updateByExampleSelective"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
(),
Array
.
newInstance
(
TbColumnField1
.
getCls
(),
0
));
Assert
.
assertEquals
(
result
,
1
);
// 验证执行结果
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select inc_f1 from tb where id = 3"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
53
);
// 2. 测试updateByPrimaryKeySelective
ObjectUtil
tbKeysMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbKeysMapper"
)));
ObjectUtil
tbKeysBuilderInc
=
new
ObjectUtil
(
loader
,
packagz
+
".TbKeys$Builder$Inc#INC"
);
ObjectUtil
tbKeysBuilder
=
new
ObjectUtil
(
loader
,
packagz
+
".TbKeys$Builder"
);
tbKeysBuilder
.
invoke
(
"key1"
,
1
l
);
tbKeysBuilder
.
invoke
(
"key2"
,
"k1"
);
tbKeysBuilder
.
invoke
(
"incF1"
,
10
l
,
tbKeysBuilderInc
.
getObject
());
tbKeysBuilder
.
invoke
(
"incF3"
,
30
l
,
tbKeysBuilderInc
.
getObject
());
// selective
ObjectUtil
TbColumnKey1
=
new
ObjectUtil
(
loader
,
packagz
+
".TbKeys$Column#key1"
);
TbColumnIncF1
=
new
ObjectUtil
(
loader
,
packagz
+
".TbKeys$Column#incF1"
);
columns
=
Array
.
newInstance
(
TbColumnKey1
.
getCls
(),
2
);
Array
.
set
(
columns
,
0
,
TbColumnKey1
.
getObject
());
Array
.
set
(
columns
,
1
,
TbColumnIncF1
.
getObject
());
// sql
// 非空判断
sql
=
SqlHelper
.
getFormatMapperSql
(
tbKeysMapper
.
getObject
(),
"updateByPrimaryKeySelective"
,
tbKeysBuilder
.
invoke
(
"build"
));
Assert
.
assertEquals
(
sql
,
"update tb_keys SET inc_f1 = inc_f1 + 10 , inc_f3 = inc_f3 + 30 where key1 = 1 and key2 = 'k1'"
);
// selective 指定
sql
=
SqlHelper
.
getFormatMapperSql
(
tbKeysMapper
.
getObject
(),
"updateByPrimaryKeySelective"
,
tbKeysBuilder
.
invoke
(
"build"
),
columns
);
Assert
.
assertEquals
(
sql
,
"update tb_keys SET key1 = 1 , inc_f1 = inc_f1 + 10 where key1 = 1 and key2 = 'k1'"
);
// 执行
result
=
tbKeysMapper
.
invoke
(
"updateByPrimaryKeySelective"
,
tbKeysBuilder
.
invoke
(
"build"
),
columns
);
Assert
.
assertEquals
(
result
,
1
);
// 验证执行结果
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select inc_f1, inc_f3 from tb_keys where key1 = 1 and key2 = 'k1'"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
11
);
}
});
}
/**
/**
* 测试 autoDelimitKeywords
* 测试 autoDelimitKeywords
*/
*/
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/OldSelectiveEnhancedPluginTest.java
View file @
a8a788f1
...
@@ -212,7 +212,7 @@ public class OldSelectiveEnhancedPluginTest {
...
@@ -212,7 +212,7 @@ public class OldSelectiveEnhancedPluginTest {
// sql
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"upsertSelective"
,
tb
.
getObject
());
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"upsertSelective"
,
tb
.
getObject
());
Assert
.
assertEquals
(
sql
,
"insert into tb ( id, field_1, inc_f2 ) values ( 10, 'null', 5 ) on duplicate key update
id = 10,
field_1 = 'null', inc_f2 = 5"
);
Assert
.
assertEquals
(
sql
,
"insert into tb ( id, field_1, inc_f2 ) values ( 10, 'null', 5 ) on duplicate key update field_1 = 'null', inc_f2 = 5"
);
Object
result
=
tbMapper
.
invoke
(
"upsertSelective"
,
tb
.
getObject
());
Object
result
=
tbMapper
.
invoke
(
"upsertSelective"
,
tb
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
Assert
.
assertEquals
(
result
,
1
);
}
}
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java
View file @
a8a788f1
...
@@ -165,7 +165,7 @@ public class UpsertPluginTest {
...
@@ -165,7 +165,7 @@ public class UpsertPluginTest {
// sql
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"upsertSelective"
,
tb
.
getObject
());
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"upsertSelective"
,
tb
.
getObject
());
Assert
.
assertEquals
(
sql
,
"insert into tb ( id, field1 ) values ( 20, 'ts1' ) on duplicate key update
id = 20,
field1 = 'ts1'"
);
Assert
.
assertEquals
(
sql
,
"insert into tb ( id, field1 ) values ( 20, 'ts1' ) on duplicate key update field1 = 'ts1'"
);
Object
result
=
tbMapper
.
invoke
(
"upsertSelective"
,
tb
.
getObject
());
Object
result
=
tbMapper
.
invoke
(
"upsertSelective"
,
tb
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
Assert
.
assertEquals
(
result
,
1
);
...
@@ -179,7 +179,7 @@ public class UpsertPluginTest {
...
@@ -179,7 +179,7 @@ public class UpsertPluginTest {
// sql
// sql
sql
=
SqlHelper
.
getFormatMapperSql
(
TbBlobsMapper
.
getObject
(),
"upsertSelective"
,
TbBlobsWithBLOBs
.
getObject
());
sql
=
SqlHelper
.
getFormatMapperSql
(
TbBlobsMapper
.
getObject
(),
"upsertSelective"
,
TbBlobsWithBLOBs
.
getObject
());
Assert
.
assertEquals
(
sql
,
"insert into tb_blobs ( id, field1, field2 ) values ( 500, 'ts1', 'ts2' ) on duplicate key update
id = 500,
field1 = 'ts1', field2 = 'ts2'"
);
Assert
.
assertEquals
(
sql
,
"insert into tb_blobs ( id, field1, field2 ) values ( 500, 'ts1', 'ts2' ) on duplicate key update field1 = 'ts1', field2 = 'ts2'"
);
result
=
TbBlobsMapper
.
invoke
(
"upsertSelective"
,
TbBlobsWithBLOBs
.
getObject
());
result
=
TbBlobsMapper
.
invoke
(
"upsertSelective"
,
TbBlobsWithBLOBs
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
Assert
.
assertEquals
(
result
,
1
);
}
}
...
...
src/test/resources/scripts/IncrementsPlugin/mybatis-generator-with-selective-enhanced-plugin.xml
0 → 100644
View file @
a8a788f1
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2018.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties
resource=
"db.properties"
/>
<!--导入属性配置 -->
<context
id=
"default"
targetRuntime=
"MyBatis3"
>
<!-- 插件 -->
<plugin
type=
"com.itfsw.mybatis.generator.plugins.IncrementsPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.SelectiveEnhancedPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"
/>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${driver}"
connectionURL=
"${url}"
userId=
"${username}"
password=
"${password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
""
targetProject=
""
>
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator
targetPackage=
""
targetProject=
""
/>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator
targetPackage=
""
targetProject=
""
type=
"XMLMAPPER"
/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table
tableName=
"tb"
>
<property
name=
"incrementsColumns"
value=
"inc_f1"
/>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
<table
tableName=
"tb_keys"
>
<property
name=
"incrementsColumns"
value=
" inc_f1, inc_f2, inc_f3 "
/>
</table>
<table
tableName=
"tb_blobs"
>
<property
name=
"incrementsColumns"
value=
"inc_f1,inc_f3"
/>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
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