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
c33616c4
Commit
c33616c4
authored
Apr 28, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重构代码插件依赖关系
parent
cbfe10c9
Changes
18
Show whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
630 additions
and
1523 deletions
+630
-1523
src/main/java/com/itfsw/mybatis/generator/plugins/OldSelectiveEnhancedPlugin.java
...mybatis/generator/plugins/OldSelectiveEnhancedPlugin.java
+0
-296
src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
...fsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
+38
-0
src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
...sw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
+180
-146
src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
...ava/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
+91
-521
src/main/java/com/itfsw/mybatis/generator/plugins/utils/BasePlugin.java
...com/itfsw/mybatis/generator/plugins/utils/BasePlugin.java
+8
-6
src/main/java/com/itfsw/mybatis/generator/plugins/utils/PluginTools.java
...om/itfsw/mybatis/generator/plugins/utils/PluginTools.java
+11
-0
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java
...tis/generator/plugins/utils/XmlElementGeneratorTools.java
+56
-4
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java
.../mybatis/generator/plugins/utils/hook/HookAggregator.java
+144
-0
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/IUpsertPluginHook.java
...batis/generator/plugins/utils/hook/IUpsertPluginHook.java
+77
-0
src/test/java/com/itfsw/mybatis/generator/plugins/OldSelectiveEnhancedPluginTest.java
...tis/generator/plugins/OldSelectiveEnhancedPluginTest.java
+0
-264
src/test/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPluginTest.java
...ybatis/generator/plugins/SelectiveEnhancedPluginTest.java
+1
-6
src/test/java/com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java
...com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java
+7
-15
src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java
...a/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java
+17
-22
src/test/resources/scripts/OldSelectiveEnhancedPlugin/init.sql
...est/resources/scripts/OldSelectiveEnhancedPlugin/init.sql
+0
-37
src/test/resources/scripts/OldSelectiveEnhancedPlugin/mybatis-generator-with-UpsertPlugin-with-wrong-place.xml
.../mybatis-generator-with-UpsertPlugin-with-wrong-place.xml
+0
-52
src/test/resources/scripts/OldSelectiveEnhancedPlugin/mybatis-generator-without-ModelColumnPlugin.xml
...cedPlugin/mybatis-generator-without-ModelColumnPlugin.xml
+0
-50
src/test/resources/scripts/OldSelectiveEnhancedPlugin/mybatis-generator.xml
.../scripts/OldSelectiveEnhancedPlugin/mybatis-generator.xml
+0
-52
src/test/resources/scripts/SelectiveEnhancedPlugin/mybatis-generator-with-OldSelectiveEnhancedPlugin.xml
...gin/mybatis-generator-with-OldSelectiveEnhancedPlugin.xml
+0
-52
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/OldSelectiveEnhancedPlugin.java
deleted
100644 → 0
View file @
cbfe10c9
/*
* Copyright (c) 2017.
*
* 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.
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.IntrospectedTableTools
;
import
com.itfsw.mybatis.generator.plugins.utils.PluginTools
;
import
com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.*
;
import
org.mybatis.generator.api.dom.xml.*
;
import
org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities
;
import
java.util.List
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
/**
* ---------------------------------------------------------------------------
* Selective 增强插件
* ---------------------------------------------------------------------------
*
* @author: hewei
* @time:2017/4/20 15:39
* ---------------------------------------------------------------------------
*/
@Deprecated
public
class
OldSelectiveEnhancedPlugin
extends
BasePlugin
{
public
static
final
String
METHOD_HAS_SELECTIVE
=
"hasSelective"
;
// 方法名
/**
* {@inheritDoc}
*/
@Override
public
boolean
validate
(
List
<
String
>
warnings
)
{
// 插件使用前提是使用了ModelColumnPlugin插件
if
(!
PluginTools
.
checkDependencyPlugin
(
getContext
(),
ModelColumnPlugin
.
class
))
{
warnings
.
add
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!"
);
return
false
;
}
// 插件位置
PluginTools
.
shouldAfterPlugins
(
getContext
(),
this
.
getClass
(),
warnings
,
UpsertPlugin
.
class
);
return
super
.
validate
(
warnings
);
}
/**
* Model Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param topLevelClass
* @param introspectedTable
* @return
*/
@Override
public
boolean
modelBaseRecordClassGenerated
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
// import
topLevelClass
.
addImportedType
(
FullyQualifiedJavaType
.
getNewMapInstance
());
topLevelClass
.
addImportedType
(
FullyQualifiedJavaType
.
getNewHashMapInstance
());
// field
Field
selectiveColumnsField
=
new
Field
(
"selectiveColumns"
,
new
FullyQualifiedJavaType
(
"Map<String, Boolean>"
));
commentGenerator
.
addFieldComment
(
selectiveColumnsField
,
introspectedTable
);
selectiveColumnsField
.
setVisibility
(
JavaVisibility
.
PRIVATE
);
selectiveColumnsField
.
setInitializationString
(
"new HashMap<String, Boolean>()"
);
topLevelClass
.
addField
(
selectiveColumnsField
);
// Method hasSelective
Method
mHasSelective
=
new
Method
(
METHOD_HAS_SELECTIVE
);
commentGenerator
.
addGeneralMethodComment
(
mHasSelective
,
introspectedTable
);
mHasSelective
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
mHasSelective
.
setReturnType
(
FullyQualifiedJavaType
.
getBooleanPrimitiveInstance
());
mHasSelective
.
addBodyLine
(
"return this.selectiveColumns.size() > 0;"
);
topLevelClass
.
addMethod
(
mHasSelective
);
// Method hasSelective
Method
mHasSelective1
=
new
Method
(
METHOD_HAS_SELECTIVE
);
commentGenerator
.
addGeneralMethodComment
(
mHasSelective1
,
introspectedTable
);
mHasSelective1
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
mHasSelective1
.
setReturnType
(
FullyQualifiedJavaType
.
getBooleanPrimitiveInstance
());
mHasSelective1
.
addParameter
(
new
Parameter
(
FullyQualifiedJavaType
.
getStringInstance
(),
"column"
));
mHasSelective1
.
addBodyLine
(
"return this.selectiveColumns.get(column) != null;"
);
topLevelClass
.
addMethod
(
mHasSelective1
);
// Method selective
Method
mSelective
=
new
Method
(
"selective"
);
commentGenerator
.
addGeneralMethodComment
(
mSelective
,
introspectedTable
);
mSelective
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
mSelective
.
setReturnType
(
topLevelClass
.
getType
());
mSelective
.
addParameter
(
new
Parameter
(
new
FullyQualifiedJavaType
(
ModelColumnPlugin
.
ENUM_NAME
),
"columns"
,
true
));
mSelective
.
addBodyLine
(
"this.selectiveColumns.clear();"
);
mSelective
.
addBodyLine
(
"if (columns != null) {"
);
mSelective
.
addBodyLine
(
"for ("
+
ModelColumnPlugin
.
ENUM_NAME
+
" column : columns) {"
);
mSelective
.
addBodyLine
(
"this.selectiveColumns.put(column.value(), true);"
);
mSelective
.
addBodyLine
(
"}"
);
mSelective
.
addBodyLine
(
"}"
);
mSelective
.
addBodyLine
(
"return this;"
);
topLevelClass
.
addMethod
(
mSelective
);
return
true
;
}
/**
* SQL Map Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param document
* @param introspectedTable
* @return
*/
@Override
public
boolean
sqlMapDocumentGenerated
(
Document
document
,
IntrospectedTable
introspectedTable
)
{
List
<
Element
>
rootElements
=
document
.
getRootElement
().
getElements
();
for
(
Element
rootElement
:
rootElements
)
{
if
(
rootElement
instanceof
XmlElement
)
{
XmlElement
xmlElement
=
(
XmlElement
)
rootElement
;
List
<
Attribute
>
attributes
=
xmlElement
.
getAttributes
();
// 查找ID
String
id
=
""
;
for
(
Attribute
attribute
:
attributes
)
{
if
(
attribute
.
getName
().
equals
(
"id"
))
{
id
=
attribute
.
getValue
();
}
}
// ====================================== 1. insertSelective ======================================
if
(
"insertSelective"
.
equals
(
id
))
{
List
<
XmlElement
>
eles
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"trim"
);
for
(
XmlElement
ele
:
eles
)
{
this
.
replaceEle
(
ele
,
"_parameter."
,
introspectedTable
);
}
}
// ====================================== 2. updateByExampleSelective ======================================
if
(
"updateByExampleSelective"
.
equals
(
id
))
{
List
<
XmlElement
>
eles
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"set"
);
for
(
XmlElement
ele
:
eles
)
{
this
.
replaceEle
(
ele
,
"record."
,
introspectedTable
);
}
}
// ====================================== 3. updateByPrimaryKeySelective ======================================
if
(
"updateByPrimaryKeySelective"
.
equals
(
id
))
{
List
<
XmlElement
>
eles
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"set"
);
for
(
XmlElement
ele
:
eles
)
{
this
.
replaceEle
(
ele
,
"_parameter."
,
introspectedTable
);
}
}
// ====================================== 4. upsertSelective ======================================
if
(
"upsertSelective"
.
equals
(
id
))
{
List
<
XmlElement
>
eles
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"trim"
);
for
(
XmlElement
ele
:
eles
)
{
this
.
replaceEle
(
ele
,
"_parameter."
,
introspectedTable
);
}
}
// ====================================== 5. upsertByExampleSelective ======================================
if
(
"upsertByExampleSelective"
.
equals
(
id
))
{
List
<
XmlElement
>
eles
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"trim"
);
this
.
replaceEle
(
eles
.
get
(
1
),
"record."
,
introspectedTable
);
// upsertByExampleSelective的第二个trim比较特殊,需另行处理
this
.
replaceEleForUpsertByExampleSelective
(
eles
.
get
(
2
),
"record."
,
introspectedTable
,
!
introspectedTable
.
getRules
().
generateRecordWithBLOBsClass
());
List
<
XmlElement
>
eles1
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"set"
);
for
(
XmlElement
ele
:
eles1
)
{
this
.
replaceEle
(
ele
,
"record."
,
introspectedTable
);
}
}
}
}
return
true
;
}
/**
* 替换节点if信息
*
* @param element
* @param prefix
* @param introspectedTable
*/
private
void
replaceEle
(
XmlElement
element
,
String
prefix
,
IntrospectedTable
introspectedTable
)
{
// choose
XmlElement
chooseEle
=
new
XmlElement
(
"choose"
);
// when
XmlElement
whenEle
=
new
XmlElement
(
"when"
);
whenEle
.
addAttribute
(
new
Attribute
(
"test"
,
prefix
+
METHOD_HAS_SELECTIVE
+
"()"
));
for
(
Element
ele
:
element
.
getElements
())
{
// 对于字符串主键,是没有if判断节点的
if
(
ele
instanceof
XmlElement
)
{
// if的text节点
XmlElement
xmlElement
=
(
XmlElement
)
ele
;
// 找出field 名称
String
text
=
((
TextElement
)
xmlElement
.
getElements
().
get
(
0
)).
getContent
();
String
columnName
=
""
;
if
(
text
.
matches
(
"#\\{.*\\},?"
))
{
Pattern
pattern
=
Pattern
.
compile
(
"#\\{(.*?),.*\\},?"
);
Matcher
matcher
=
pattern
.
matcher
(
text
);
if
(
matcher
.
find
())
{
String
field
=
matcher
.
group
(
1
);
// 查找对应column
for
(
IntrospectedColumn
column
:
introspectedTable
.
getAllColumns
())
{
if
(
column
.
getJavaProperty
().
equals
(
field
))
{
columnName
=
column
.
getActualColumnName
();
}
}
}
}
else
{
if
(
text
.
matches
(
".*=.*"
))
{
columnName
=
text
.
split
(
"="
)[
0
];
}
else
{
columnName
=
text
.
replaceAll
(
","
,
""
);
}
// bug fixed: 修正使用autoDelimitKeywords过滤关键词造成的field前后加了特殊字符的问题
// columnName = columnName.trim().replaceAll("`", "").replaceAll("\"", "").replaceAll("'", "");
}
IntrospectedColumn
column
=
IntrospectedTableTools
.
safeGetColumn
(
introspectedTable
,
columnName
);
XmlElement
ifEle
=
new
XmlElement
(
"if"
);
ifEle
.
addAttribute
(
new
Attribute
(
"test"
,
prefix
+
METHOD_HAS_SELECTIVE
+
"(\'"
+
column
.
getActualColumnName
()
+
"\')"
));
for
(
Element
ifChild
:
xmlElement
.
getElements
())
{
ifEle
.
addElement
(
ifChild
);
}
whenEle
.
addElement
(
ifEle
);
}
else
{
whenEle
.
addElement
(
ele
);
}
}
// otherwise
XmlElement
otherwiseEle
=
new
XmlElement
(
"otherwise"
);
for
(
Element
ele
:
element
.
getElements
())
{
otherwiseEle
.
addElement
(
ele
);
}
chooseEle
.
addElement
(
whenEle
);
chooseEle
.
addElement
(
otherwiseEle
);
// 清空原始节点,新增choose节点
element
.
getElements
().
clear
();
element
.
addElement
(
chooseEle
);
}
/**
* 替换节点upsertByExampleSelective if信息
*
* @param element
* @param prefix
* @param introspectedTable
* @param allColumns
*/
private
void
replaceEleForUpsertByExampleSelective
(
XmlElement
element
,
String
prefix
,
IntrospectedTable
introspectedTable
,
boolean
allColumns
)
{
// choose
XmlElement
chooseEle
=
new
XmlElement
(
"choose"
);
// when
XmlElement
whenEle
=
new
XmlElement
(
"when"
);
whenEle
.
addAttribute
(
new
Attribute
(
"test"
,
prefix
+
METHOD_HAS_SELECTIVE
+
"()"
));
for
(
IntrospectedColumn
introspectedColumn
:
(
allColumns
?
introspectedTable
.
getAllColumns
()
:
introspectedTable
.
getNonBLOBColumns
()))
{
XmlElement
eleIf
=
new
XmlElement
(
"if"
);
eleIf
.
addAttribute
(
new
Attribute
(
"test"
,
prefix
+
METHOD_HAS_SELECTIVE
+
"(\'"
+
introspectedColumn
.
getActualColumnName
()
+
"\')"
));
eleIf
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
)
+
","
));
whenEle
.
addElement
(
eleIf
);
}
// otherwise
XmlElement
otherwiseEle
=
new
XmlElement
(
"otherwise"
);
for
(
Element
ele
:
element
.
getElements
())
{
otherwiseEle
.
addElement
(
ele
);
}
chooseEle
.
addElement
(
whenEle
);
chooseEle
.
addElement
(
otherwiseEle
);
// 清空原始节点,新增choose节点
element
.
getElements
().
clear
();
element
.
addElement
(
chooseEle
);
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
0 → 100644
View file @
c33616c4
/*
* 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.
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
org.mybatis.generator.api.GeneratedJavaFile
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
java.util.List
;
/**
* ---------------------------------------------------------------------------
* 乐观锁插件
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2018/4/26 10:24
* ---------------------------------------------------------------------------
*/
public
class
OptimisticLockerPlugin
extends
BasePlugin
{
@Override
public
List
<
GeneratedJavaFile
>
contextGenerateAdditionalJavaFiles
(
IntrospectedTable
introspectedTable
)
{
return
super
.
contextGenerateAdditionalJavaFiles
(
introspectedTable
);
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
View file @
c33616c4
...
...
@@ -17,9 +17,9 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
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.XmlElementGeneratorTools
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.IUpsertPluginHook
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.FullyQualifiedJavaType
;
...
...
@@ -42,7 +42,7 @@ import java.util.List;
* @time:2018/4/20 15:39
* ---------------------------------------------------------------------------
*/
public
class
SelectiveEnhancedPlugin
extends
BasePlugin
{
public
class
SelectiveEnhancedPlugin
extends
BasePlugin
implements
IUpsertPluginHook
{
/**
* {@inheritDoc}
...
...
@@ -56,12 +56,6 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
return
false
;
}
// 和 OldSelectiveEnhancedPlugin 不能同时使用
if
(
PluginTools
.
checkDependencyPlugin
(
getContext
(),
OldSelectiveEnhancedPlugin
.
class
))
{
warnings
.
add
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"不能和"
+
OldSelectiveEnhancedPlugin
.
class
.
getTypeName
()
+
"插件同时使用!"
);
return
false
;
}
return
super
.
validate
(
warnings
);
}
...
...
@@ -164,12 +158,8 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
@Override
public
boolean
sqlMapInsertSelectiveElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
// 清空
XmlElement
answer
=
element
;
answer
.
getElements
().
clear
();
answer
.
getAttributes
().
clear
();
XmlElement
answer
=
new
XmlElement
(
"insert"
);
answer
.
addAttribute
(
new
Attribute
(
"id"
,
introspectedTable
.
getInsertSelectiveStatementId
()));
answer
.
addAttribute
(
new
Attribute
(
"parameterType"
,
"map"
));
commentGenerator
.
addComment
(
answer
);
...
...
@@ -196,65 +186,13 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
sb
.
append
(
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
());
answer
.
addElement
(
new
TextElement
(
sb
.
toString
()));
// selective
XmlElement
insertChooseEle
=
new
XmlElement
(
"choose"
);
answer
.
addElement
(
insertChooseEle
);
// columns
answer
.
addElement
(
this
.
generateInsertColumnSelective
(
ListUtilities
.
removeIdentityAndGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
())));
// values
answer
.
addElement
(
new
TextElement
(
"values"
));
answer
.
addElement
(
this
.
generateInsertValuesSelective
(
ListUtilities
.
removeIdentityAndGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
())));
XmlElement
insertWhenEle
=
new
XmlElement
(
"when"
);
insertWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective != null and selective.length > 0"
));
insertChooseEle
.
addElement
(
insertWhenEle
);
XmlElement
insertForeachEle
=
new
XmlElement
(
"foreach"
);
insertForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"open"
,
"("
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"close"
,
")"
));
insertForeachEle
.
addElement
(
new
TextElement
(
"${column.value}"
));
insertWhenEle
.
addElement
(
insertForeachEle
);
XmlElement
insertOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
insertOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateKeysSelective
(
ListUtilities
.
removeIdentityAndGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
()),
"record."
));
insertChooseEle
.
addElement
(
insertOtherwiseEle
);
XmlElement
insertTrimElement
=
new
XmlElement
(
"trim"
);
insertTrimElement
.
addAttribute
(
new
Attribute
(
"prefix"
,
"("
));
insertTrimElement
.
addAttribute
(
new
Attribute
(
"suffix"
,
")"
));
insertTrimElement
.
addAttribute
(
new
Attribute
(
"suffixOverrides"
,
","
));
insertOtherwiseEle
.
addElement
(
insertTrimElement
);
XmlElement
valuesChooseEle
=
new
XmlElement
(
"choose"
);
answer
.
addElement
(
valuesChooseEle
);
XmlElement
valuesWhenEle
=
new
XmlElement
(
"when"
);
valuesWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective != null and selective.length > 0"
));
valuesChooseEle
.
addElement
(
valuesWhenEle
);
XmlElement
valuesForeachEle
=
new
XmlElement
(
"foreach"
);
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"open"
,
"values ("
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"close"
,
")"
));
valuesForeachEle
.
addElement
(
new
TextElement
(
"#{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
valuesWhenEle
.
addElement
(
valuesForeachEle
);
XmlElement
valuesOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
valuesOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
ListUtilities
.
removeIdentityAndGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
()),
"record."
));
valuesChooseEle
.
addElement
(
valuesOtherwiseEle
);
XmlElement
valuesTrimElement
=
new
XmlElement
(
"trim"
);
valuesTrimElement
.
addAttribute
(
new
Attribute
(
"prefix"
,
"values ("
));
valuesTrimElement
.
addAttribute
(
new
Attribute
(
"suffix"
,
")"
));
valuesTrimElement
.
addAttribute
(
new
Attribute
(
"suffixOverrides"
,
","
));
valuesOtherwiseEle
.
addElement
(
valuesTrimElement
);
XmlElementGeneratorTools
.
replaceXmlElement
(
element
,
answer
);
return
super
.
sqlMapInsertSelectiveElementGenerated
(
element
,
introspectedTable
);
}
...
...
@@ -268,12 +206,8 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
*/
@Override
public
boolean
sqlMapUpdateByExampleSelectiveElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
IncrementsPluginTools
incTools
=
IncrementsPluginTools
.
getTools
(
context
,
introspectedTable
,
warnings
);
// 清空
XmlElement
answer
=
element
;
answer
.
getElements
().
clear
();
answer
.
getAttributes
().
clear
();
XmlElement
answer
=
new
XmlElement
(
"update"
);
answer
.
addAttribute
(
new
Attribute
(
"id"
,
introspectedTable
.
getUpdateByExampleSelectiveStatementId
()));
answer
.
addAttribute
(
new
Attribute
(
"parameterType"
,
"map"
));
...
...
@@ -286,46 +220,12 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
// selective
answer
.
addElement
(
new
TextElement
(
"SET"
));
XmlElement
setChooseEle
=
new
XmlElement
(
"choose"
);
answer
.
addElement
(
setChooseEle
);
XmlElement
setWhenEle
=
new
XmlElement
(
"when"
);
setWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective != null and selective.length > 0"
));
setChooseEle
.
addElement
(
setWhenEle
);
XmlElement
setForeachEle
=
new
XmlElement
(
"foreach"
);
setForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
// 自增插件支持
if
(
incTools
.
support
())
{
incTools
.
generateSetsSelectiveWithSelectiveEnhancedPlugin
(
setForeachEle
);
}
else
{
setForeachEle
.
addElement
(
new
TextElement
(
"${column.value} = #{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
}
setWhenEle
.
addElement
(
setForeachEle
);
XmlElement
setOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
// 自增插件支持
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
);
answer
.
addElement
(
this
.
generateSetsSelective
(
ListUtilities
.
removeGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
())));
answer
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
XmlElementGeneratorTools
.
replaceXmlElement
(
element
,
answer
);
return
super
.
sqlMapUpdateByExampleSelectiveElementGenerated
(
element
,
introspectedTable
);
}
...
...
@@ -338,12 +238,8 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
*/
@Override
public
boolean
sqlMapUpdateByPrimaryKeySelectiveElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
IncrementsPluginTools
incTools
=
IncrementsPluginTools
.
getTools
(
context
,
introspectedTable
,
warnings
);
// 清空
XmlElement
answer
=
element
;
answer
.
getElements
().
clear
();
answer
.
getAttributes
().
clear
();
XmlElement
answer
=
new
XmlElement
(
"update"
);
answer
.
addAttribute
(
new
Attribute
(
"id"
,
introspectedTable
.
getUpdateByPrimaryKeySelectiveStatementId
()));
answer
.
addAttribute
(
new
Attribute
(
"parameterType"
,
"map"
));
...
...
@@ -357,44 +253,182 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
// selective
answer
.
addElement
(
new
TextElement
(
"SET"
));
XmlElement
setChooseEle
=
new
XmlElement
(
"choose"
);
answer
.
addElement
(
setChooseEle
);
answer
.
addElement
(
this
.
generateSetsSelective
(
ListUtilities
.
removeGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
())));
XmlElementGeneratorTools
.
generateWhereByPrimaryKeyTo
(
answer
,
introspectedTable
.
getPrimaryKeyColumns
(),
"record."
);
XmlElementGeneratorTools
.
replaceXmlElement
(
element
,
answer
);
return
super
.
sqlMapUpdateByPrimaryKeySelectiveElementGenerated
(
element
,
introspectedTable
);
}
// =============================================== IUpsertPluginHook ===================================================
/**
* upsertSelective 方法
* @param method
* @param interfaze
* @param introspectedTable
* @return
*/
@Override
public
boolean
clientUpsertSelectiveMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
// @Param("record")
method
.
getParameters
().
get
(
0
).
addAnnotation
(
"@Param(\"record\")"
);
// column枚举
// 找出全字段对应的Model
FullyQualifiedJavaType
fullFieldModel
=
introspectedTable
.
getRules
().
calculateAllFieldsClass
();
FullyQualifiedJavaType
selectiveType
=
new
FullyQualifiedJavaType
(
fullFieldModel
.
getShortName
()
+
"."
+
ModelColumnPlugin
.
ENUM_NAME
);
method
.
addParameter
(
new
Parameter
(
selectiveType
,
"selective"
,
"@Param(\"selective\")"
,
true
));
return
true
;
}
/**
* upsertByExampleSelective 方法
* @param method
* @param interfaze
* @param introspectedTable
* @return
*/
@Override
public
boolean
clientUpsertByExampleSelectiveMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
// column枚举
// 找出全字段对应的Model
FullyQualifiedJavaType
fullFieldModel
=
introspectedTable
.
getRules
().
calculateAllFieldsClass
();
FullyQualifiedJavaType
selectiveType
=
new
FullyQualifiedJavaType
(
fullFieldModel
.
getShortName
()
+
"."
+
ModelColumnPlugin
.
ENUM_NAME
);
method
.
addParameter
(
new
Parameter
(
selectiveType
,
"selective"
,
"@Param(\"selective\")"
,
true
));
return
true
;
}
/**
* upsertSelective xml
* @param element
* @param columns
* @param insertColumnsEle
* @param insertValuesEle
* @param setsEle
* @param introspectedTable
* @return
*/
@Override
public
boolean
sqlMapUpsertSelectiveElementGenerated
(
XmlElement
element
,
List
<
IntrospectedColumn
>
columns
,
XmlElement
insertColumnsEle
,
XmlElement
insertValuesEle
,
XmlElement
setsEle
,
IntrospectedTable
introspectedTable
)
{
// parameterType
XmlElementGeneratorTools
.
replaceAttribute
(
element
,
new
Attribute
(
"parameterType"
,
"map"
));
// 替换insert column
XmlElementGeneratorTools
.
replaceXmlElement
(
insertColumnsEle
,
this
.
generateInsertColumnSelective
(
columns
));
// 替换insert values
XmlElementGeneratorTools
.
replaceXmlElement
(
insertValuesEle
,
this
.
generateInsertValuesSelective
(
columns
));
// 替换update set
XmlElementGeneratorTools
.
replaceXmlElement
(
setsEle
,
this
.
generateSetsSelective
(
columns
));
return
true
;
}
/**
* upsertByExampleSelective xml
* @param element
* @param columns
* @param insertColumnsEle
* @param insertValuesEle
* @param setsEle
* @param introspectedTable
* @return
*/
@Override
public
boolean
sqlMapUpsertByExampleSelectiveElementGenerated
(
XmlElement
element
,
List
<
IntrospectedColumn
>
columns
,
XmlElement
insertColumnsEle
,
XmlElement
insertValuesEle
,
XmlElement
setsEle
,
IntrospectedTable
introspectedTable
)
{
return
false
;
}
// ====================================================== 一些私有节点生成方法 =========================================================
/**
* insert column selective
* @param columns
* @return
*/
private
XmlElement
generateInsertColumnSelective
(
List
<
IntrospectedColumn
>
columns
)
{
XmlElement
insertColumnsChooseEle
=
new
XmlElement
(
"choose"
);
XmlElement
insertWhenEle
=
new
XmlElement
(
"when"
);
insertWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective != null and selective.length > 0"
));
insertColumnsChooseEle
.
addElement
(
insertWhenEle
);
XmlElement
insertForeachEle
=
new
XmlElement
(
"foreach"
);
insertForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"open"
,
"("
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"close"
,
")"
));
insertForeachEle
.
addElement
(
new
TextElement
(
"${column.value}"
));
insertWhenEle
.
addElement
(
insertForeachEle
);
XmlElement
insertOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
insertOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateKeysSelective
(
columns
,
"record."
));
insertColumnsChooseEle
.
addElement
(
insertOtherwiseEle
);
XmlElement
insertTrimElement
=
new
XmlElement
(
"trim"
);
insertTrimElement
.
addAttribute
(
new
Attribute
(
"prefix"
,
"("
));
insertTrimElement
.
addAttribute
(
new
Attribute
(
"suffix"
,
")"
));
insertTrimElement
.
addAttribute
(
new
Attribute
(
"suffixOverrides"
,
","
));
insertOtherwiseEle
.
addElement
(
insertTrimElement
);
return
insertColumnsChooseEle
;
}
/**
* insert column selective
* @param columns
* @return
*/
private
XmlElement
generateInsertValuesSelective
(
List
<
IntrospectedColumn
>
columns
)
{
XmlElement
insertValuesChooseEle
=
new
XmlElement
(
"choose"
);
XmlElement
valuesWhenEle
=
new
XmlElement
(
"when"
);
valuesWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective != null and selective.length > 0"
));
insertValuesChooseEle
.
addElement
(
valuesWhenEle
);
XmlElement
valuesForeachEle
=
new
XmlElement
(
"foreach"
);
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"open"
,
"("
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"close"
,
")"
));
valuesForeachEle
.
addElement
(
new
TextElement
(
"#{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
valuesWhenEle
.
addElement
(
valuesForeachEle
);
XmlElement
valuesOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
insertValuesChooseEle
.
addElement
(
valuesOtherwiseEle
);
valuesOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
columns
,
"record."
));
return
insertValuesChooseEle
;
}
/**
* sets selective
* @param columns
* @return
*/
private
XmlElement
generateSetsSelective
(
List
<
IntrospectedColumn
>
columns
)
{
XmlElement
setsChooseEle
=
new
XmlElement
(
"choose"
);
XmlElement
setWhenEle
=
new
XmlElement
(
"when"
);
setWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective != null and selective.length > 0"
));
setChooseEle
.
addElement
(
setWhenEle
);
set
s
ChooseEle
.
addElement
(
setWhenEle
);
XmlElement
setForeachEle
=
new
XmlElement
(
"foreach"
);
setWhenEle
.
addElement
(
setForeachEle
);
setForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
// 自增插件支持
if
(
incTools
.
support
())
{
incTools
.
generateSetsSelectiveWithSelectiveEnhancedPlugin
(
setForeachEle
);
}
else
{
setForeachEle
.
addElement
(
new
TextElement
(
"${column.value} = #{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
}
setWhenEle
.
addElement
(
setForeachEle
);
XmlElement
setOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
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."
));
}
setOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateSetsSelective
(
columns
,
"record."
));
setsChooseEle
.
addElement
(
setOtherwiseEle
);
XmlElementGeneratorTools
.
generateWhereByPrimaryKeyTo
(
answer
,
introspectedTable
.
getPrimaryKeyColumns
(),
"record."
);
return
super
.
sqlMapUpdateByPrimaryKeySelectiveElementGenerated
(
element
,
introspectedTable
);
return
setsChooseEle
;
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
View file @
c33616c4
...
...
@@ -16,14 +16,16 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.*
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools
;
import
com.itfsw.mybatis.generator.plugins.utils.PluginTools
;
import
com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.IUpsertPluginHook
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.OutputUtilities
;
import
org.mybatis.generator.api.dom.java.*
;
import
org.mybatis.generator.api.dom.xml.*
;
import
org.mybatis.generator.codegen.mybatis3.ListUtilities
;
import
org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities
;
import
org.mybatis.generator.internal.util.StringUtility
;
import
java.util.Arrays
;
...
...
@@ -50,7 +52,6 @@ public class UpsertPlugin extends BasePlugin {
public
static
final
String
PRO_ALLOW_MULTI_QUERIES
=
"allowMultiQueries"
;
// property allowMultiQueries
private
boolean
allowMultiQueries
=
false
;
// 是否允许多sql提交
private
boolean
withSelectiveEnhancedPlugin
=
false
;
// 是否启用了Selective增强插件
/**
* {@inheritDoc}
...
...
@@ -73,9 +74,6 @@ public class UpsertPlugin extends BasePlugin {
warnings
.
add
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"插件您开启了allowMultiQueries支持,注意在jdbc url 配置中增加“allowMultiQueries=true”支持(不怎么建议使用该功能,开启多sql提交会增加sql注入的风险,请确保你所有sql都使用MyBatis书写,请不要使用statement进行sql提交)!"
);
}
// 是否启用了Selective增强插件
this
.
withSelectiveEnhancedPlugin
=
PluginTools
.
checkDependencyPlugin
(
context
,
SelectiveEnhancedPlugin
.
class
);
return
super
.
validate
(
warnings
);
}
...
...
@@ -122,20 +120,16 @@ public class UpsertPlugin extends BasePlugin {
Method
mUpsertSelective
=
JavaElementGeneratorTools
.
generateMethod
(
METHOD_UPSERT_SELECTIVE
,
JavaVisibility
.
DEFAULT
,
FullyQualifiedJavaType
.
getIntInstance
()
FullyQualifiedJavaType
.
getIntInstance
(),
new
Parameter
(
fullFieldModel
,
"record"
)
);
if
(
withSelectiveEnhancedPlugin
)
{
mUpsertSelective
.
addParameter
(
new
Parameter
(
fullFieldModel
,
"record"
,
"@Param(\"record\")"
));
// column枚举
FullyQualifiedJavaType
selectiveType
=
new
FullyQualifiedJavaType
(
fullFieldModel
.
getShortName
()
+
"."
+
ModelColumnPlugin
.
ENUM_NAME
);
mUpsertSelective
.
addParameter
(
new
Parameter
(
selectiveType
,
"selective"
,
"@Param(\"selective\")"
,
true
));
}
else
{
mUpsertSelective
.
addParameter
(
new
Parameter
(
fullFieldModel
,
"record"
));
}
commentGenerator
.
addGeneralMethodComment
(
mUpsertSelective
,
introspectedTable
);
// hook
if
(
PluginTools
.
getHook
(
IUpsertPluginHook
.
class
).
clientUpsertSelectiveMethodGenerated
(
mUpsertSelective
,
interfaze
,
introspectedTable
))
{
// interface 增加方法
interfaze
.
addMethod
(
mUpsertSelective
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加upsertSelective方法。"
);
}
if
(
this
.
allowMultiQueries
)
{
// ====================================== upsertByExample ======================================
...
...
@@ -175,16 +169,14 @@ public class UpsertPlugin extends BasePlugin {
new
Parameter
(
introspectedTable
.
getRules
().
calculateAllFieldsClass
(),
"record"
,
"@Param(\"record\")"
),
new
Parameter
(
new
FullyQualifiedJavaType
(
introspectedTable
.
getExampleType
()),
"example"
,
"@Param(\"example\")"
)
);
if
(
withSelectiveEnhancedPlugin
)
{
// column枚举
FullyQualifiedJavaType
selectiveType
=
new
FullyQualifiedJavaType
(
fullFieldModel
.
getShortName
()
+
"."
+
ModelColumnPlugin
.
ENUM_NAME
);
mUpsertByExampleSelective
.
addParameter
(
new
Parameter
(
selectiveType
,
"selective"
,
"@Param(\"selective\")"
,
true
));
}
commentGenerator
.
addGeneralMethodComment
(
mUpsertByExampleSelective
,
introspectedTable
);
// hook
if
(
PluginTools
.
getHook
(
IUpsertPluginHook
.
class
).
clientUpsertByExampleSelectiveMethodGenerated
(
mUpsertByExampleSelective
,
interfaze
,
introspectedTable
))
{
// interface 增加方法
interfaze
.
addMethod
(
mUpsertByExampleSelective
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加upsertByExampleSelective方法。"
);
}
}
return
super
.
clientGenerated
(
interfaze
,
topLevelClass
,
introspectedTable
);
}
...
...
@@ -215,209 +207,36 @@ public class UpsertPlugin extends BasePlugin {
private
void
generateXmlElementWithSelective
(
Document
document
,
IntrospectedTable
introspectedTable
)
{
List
<
IntrospectedColumn
>
columns
=
ListUtilities
.
removeGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
());
if
(
withSelectiveEnhancedPlugin
)
{
// ====================================== upsertSelective ======================================
XmlElement
eleUpsertSelective
=
new
XmlElement
(
"insert"
);
eleUpsertSelective
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_UPSERT_SELECTIVE
));
eleUpsertSelective
.
addAttribute
(
new
Attribute
(
"parameterType"
,
"map"
));
XmlElement
insertEle
=
new
XmlElement
(
"insert"
);
insertEle
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_UPSERT_SELECTIVE
));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
commentGenerator
.
addComment
(
eleUpsertSelective
);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools
.
useGeneratedKeys
(
eleUpsertSelective
,
introspectedTable
,
"record."
);
// insert
eleUpsertSelective
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
// selective
XmlElement
insertChooseEle
=
new
XmlElement
(
"choose"
);
eleUpsertSelective
.
addElement
(
insertChooseEle
);
XmlElement
insertWhenEle
=
new
XmlElement
(
"when"
);
insertWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective != null and selective.length > 0"
));
insertChooseEle
.
addElement
(
insertWhenEle
);
XmlElement
insertForeachEle
=
new
XmlElement
(
"foreach"
);
insertForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"open"
,
"("
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"close"
,
")"
));
insertForeachEle
.
addElement
(
new
TextElement
(
"${column.value}"
));
insertWhenEle
.
addElement
(
insertForeachEle
);
XmlElement
insertOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
insertOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateKeysSelective
(
columns
,
"record."
));
insertChooseEle
.
addElement
(
insertOtherwiseEle
);
XmlElement
insertTrimElement
=
new
XmlElement
(
"trim"
);
insertTrimElement
.
addAttribute
(
new
Attribute
(
"prefix"
,
"("
));
insertTrimElement
.
addAttribute
(
new
Attribute
(
"suffix"
,
")"
));
insertTrimElement
.
addAttribute
(
new
Attribute
(
"suffixOverrides"
,
","
));
insertOtherwiseEle
.
addElement
(
insertTrimElement
);
eleUpsertSelective
.
addElement
(
new
TextElement
(
"values"
));
XmlElement
valuesChooseEle
=
new
XmlElement
(
"choose"
);
eleUpsertSelective
.
addElement
(
valuesChooseEle
);
XmlElement
valuesWhenEle
=
new
XmlElement
(
"when"
);
valuesWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective != null and selective.length > 0"
));
valuesChooseEle
.
addElement
(
valuesWhenEle
);
XmlElement
valuesForeachEle
=
new
XmlElement
(
"foreach"
);
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"open"
,
"("
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"close"
,
")"
));
valuesForeachEle
.
addElement
(
new
TextElement
(
"#{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
valuesWhenEle
.
addElement
(
valuesForeachEle
);
XmlElement
valuesOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
valuesChooseEle
.
addElement
(
valuesOtherwiseEle
);
valuesOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
columns
,
"record."
));
eleUpsertSelective
.
addElement
(
new
TextElement
(
"on duplicate key update "
));
// update selective
XmlElement
setChooseEle
=
new
XmlElement
(
"choose"
);
eleUpsertSelective
.
addElement
(
setChooseEle
);
XmlElement
setWhenEle
=
new
XmlElement
(
"when"
);
setWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective != null and selective.length > 0"
));
setChooseEle
.
addElement
(
setWhenEle
);
XmlElement
setForeachEle
=
new
XmlElement
(
"foreach"
);
setWhenEle
.
addElement
(
setForeachEle
);
setForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
// set 操作增加增量插件支持
IncrementsPluginTools
incTools
=
IncrementsPluginTools
.
getTools
(
context
,
introspectedTable
,
warnings
);
if
(
incTools
.
support
())
{
incTools
.
generateSetsSelectiveWithSelectiveEnhancedPlugin
(
setForeachEle
);
}
else
{
setForeachEle
.
addElement
(
new
TextElement
(
"${column.value} = #{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
}
XmlElement
setOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
// set 操作增加增量插件支持
this
.
setsSupportIncrementsPlugin
(
setOtherwiseEle
,
"record."
,
true
,
columns
,
introspectedTable
);
setChooseEle
.
addElement
(
setOtherwiseEle
);
commentGenerator
.
addComment
(
insertEle
);
document
.
getRootElement
().
addElement
(
eleUpsertSelective
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加upsertSelective实现方法。"
);
if
(
this
.
allowMultiQueries
)
{
// ====================================== upsertByExampleSelective ======================================
XmlElement
updateEle
=
new
XmlElement
(
"update"
);
updateEle
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_UPSERT_BY_EXAMPLE_SELECTIVE
));
// 参数类型
updateEle
.
addAttribute
(
new
Attribute
(
"parameterType"
,
"map"
));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
commentGenerator
.
addComment
(
updateEle
);
insertEle
.
addAttribute
(
new
Attribute
(
"parameterType"
,
introspectedTable
.
getRules
().
calculateAllFieldsClass
().
getFullyQualifiedName
()));
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
// XmlElementGeneratorTools.useGeneratedKeys(updateEle, introspectedTable, "record.");
// update
updateEle
.
addElement
(
new
TextElement
(
"update "
+
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
()));
updateEle
.
addElement
(
new
TextElement
(
"set"
));
// selective
setChooseEle
=
new
XmlElement
(
"choose"
);
updateEle
.
addElement
(
setChooseEle
);
setWhenEle
=
new
XmlElement
(
"when"
);
setWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective != null and selective.length > 0"
));
setChooseEle
.
addElement
(
setWhenEle
);
setForeachEle
=
new
XmlElement
(
"foreach"
);
setWhenEle
.
addElement
(
setForeachEle
);
setForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
// set 操作增加增量插件支持
if
(
incTools
.
support
())
{
incTools
.
generateSetsSelectiveWithSelectiveEnhancedPlugin
(
setForeachEle
);
}
else
{
setForeachEle
.
addElement
(
new
TextElement
(
"${column.value} = #{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
}
setOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
// set 操作增加增量插件支持
this
.
setsSupportIncrementsPlugin
(
setOtherwiseEle
,
"record."
,
true
,
columns
,
introspectedTable
);
setChooseEle
.
addElement
(
setOtherwiseEle
);
// update where
updateEle
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
// multiQueries
updateEle
.
addElement
(
new
TextElement
(
";"
));
XmlElementGeneratorTools
.
useGeneratedKeys
(
insertEle
,
introspectedTable
);
// insert
updateEle
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
// selective
insertChooseEle
=
new
XmlElement
(
"choose"
);
updateEle
.
addElement
(
insertChooseEle
);
insertWhenEle
=
new
XmlElement
(
"when"
);
insertWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective != null and selective.length > 0"
));
insertChooseEle
.
addElement
(
insertWhenEle
);
insertForeachEle
=
new
XmlElement
(
"foreach"
);
insertForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"open"
,
"("
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
insertForeachEle
.
addAttribute
(
new
Attribute
(
"close"
,
")"
));
insertForeachEle
.
addElement
(
new
TextElement
(
"${column.value}"
));
insertWhenEle
.
addElement
(
insertForeachEle
);
insertOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
insertOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateKeysSelective
(
columns
,
"record."
));
insertChooseEle
.
addElement
(
insertOtherwiseEle
);
insertTrimElement
=
new
XmlElement
(
"trim"
);
insertTrimElement
.
addAttribute
(
new
Attribute
(
"prefix"
,
"("
));
insertTrimElement
.
addAttribute
(
new
Attribute
(
"suffix"
,
")"
));
insertTrimElement
.
addAttribute
(
new
Attribute
(
"suffixOverrides"
,
","
));
insertOtherwiseEle
.
addElement
(
insertTrimElement
);
this
.
generateExistsClause
(
introspectedTable
,
updateEle
,
true
,
columns
);
insertEle
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
XmlElement
insertColumnsEle
=
XmlElementGeneratorTools
.
generateKeysSelective
(
columns
);
insertEle
.
addElement
(
insertColumnsEle
);
insertEle
.
addElement
(
new
TextElement
(
"values"
));
XmlElement
insertValuesEle
=
XmlElementGeneratorTools
.
generateValuesSelective
(
columns
);
insertEle
.
addElement
(
insertValuesEle
);
insertEle
.
addElement
(
new
TextElement
(
"on duplicate key update "
));
// set
XmlElement
setsEle
=
XmlElementGeneratorTools
.
generateSetsSelective
(
columns
);
insertEle
.
addElement
(
setsEle
);
document
.
getRootElement
().
addElement
(
updateEle
);
// hook
if
(
PluginTools
.
getHook
(
IUpsertPluginHook
.
class
).
sqlMapUpsertSelectiveElementGenerated
(
insertEle
,
columns
,
insertColumnsEle
,
insertValuesEle
,
setsEle
,
introspectedTable
))
{
document
.
getRootElement
().
addElement
(
insertEle
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加upsertSelective实现方法。"
);
}
}
else
{
// ====================================== upsertSelective ======================================
XmlElement
eleUpsertSelective
=
new
XmlElement
(
"insert"
);
eleUpsertSelective
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_UPSERT_SELECTIVE
));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
commentGenerator
.
addComment
(
eleUpsertSelective
);
// 参数类型
eleUpsertSelective
.
addAttribute
(
new
Attribute
(
"parameterType"
,
introspectedTable
.
getRules
().
calculateAllFieldsClass
().
getFullyQualifiedName
()));
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools
.
useGeneratedKeys
(
eleUpsertSelective
,
introspectedTable
);
// insert
eleUpsertSelective
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
eleUpsertSelective
.
addElement
(
XmlElementGeneratorTools
.
generateKeysSelective
(
columns
));
eleUpsertSelective
.
addElement
(
new
TextElement
(
"values"
));
eleUpsertSelective
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
columns
));
eleUpsertSelective
.
addElement
(
new
TextElement
(
"on duplicate key update "
));
// set 操作增加增量插件支持
this
.
setsSupportIncrementsPlugin
(
eleUpsertSelective
,
null
,
true
,
columns
,
introspectedTable
);
document
.
getRootElement
().
addElement
(
eleUpsertSelective
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加upsertSelective实现方法。"
);
if
(
this
.
allowMultiQueries
)
{
// ====================================== upsertByExampleSelective ======================================
XmlElement
updateEle
=
new
XmlElement
(
"update"
);
...
...
@@ -435,7 +254,8 @@ public class UpsertPlugin extends BasePlugin {
updateEle
.
addElement
(
new
TextElement
(
"update "
+
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
()));
updateEle
.
addElement
(
new
TextElement
(
"set"
));
// set 操作增加增量插件支持
this
.
setsSupportIncrementsPlugin
(
updateEle
,
"record."
,
true
,
columns
,
introspectedTable
);
setsEle
=
XmlElementGeneratorTools
.
generateSetsSelective
(
columns
,
"record."
);
updateEle
.
addElement
(
setsEle
);
// update where
updateEle
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
...
...
@@ -445,9 +265,15 @@ public class UpsertPlugin extends BasePlugin {
// insert
updateEle
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
updateEle
.
addElement
(
XmlElementGeneratorTools
.
generateKeysSelective
(
columns
,
"record."
));
this
.
generateExistsClause
(
introspectedTable
,
updateEle
,
true
,
columns
);
insertColumnsEle
=
XmlElementGeneratorTools
.
generateKeysSelective
(
columns
,
"record."
);
updateEle
.
addElement
(
insertColumnsEle
);
insertValuesEle
=
XmlElementGeneratorTools
.
generateValuesSelective
(
columns
,
"record."
,
false
);
// 查询值
this
.
generateExistsClause
(
introspectedTable
,
updateEle
,
Arrays
.
asList
(
insertValuesEle
));
// hook
if
(
PluginTools
.
getHook
(
IUpsertPluginHook
.
class
).
sqlMapUpsertByExampleSelectiveElementGenerated
(
updateEle
,
columns
,
insertColumnsEle
,
insertValuesEle
,
setsEle
,
introspectedTable
))
{
document
.
getRootElement
().
addElement
(
updateEle
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加upsertSelective实现方法。"
);
}
...
...
@@ -478,16 +304,18 @@ public class UpsertPlugin extends BasePlugin {
// insert
insertEle
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
for
(
Element
element
:
thi
s
.
generateKeys
(
columns
))
{
for
(
Element
element
:
XmlElementGeneratorTool
s
.
generateKeys
(
columns
))
{
insertEle
.
addElement
(
element
);
}
insertEle
.
addElement
(
new
TextElement
(
"values"
));
for
(
Element
element
:
thi
s
.
generateValues
(
columns
))
{
for
(
Element
element
:
XmlElementGeneratorTool
s
.
generateValues
(
columns
))
{
insertEle
.
addElement
(
element
);
}
insertEle
.
addElement
(
new
TextElement
(
"on duplicate key update "
));
// set 操作增加增量插件支持
this
.
setsSupportIncrementsPlugin
(
insertEle
,
null
,
false
,
columns
,
introspectedTable
);
// set
for
(
Element
set
:
XmlElementGeneratorTools
.
generateSets
(
columns
))
{
insertEle
.
addElement
(
set
);
}
document
.
getRootElement
().
addElement
(
insertEle
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加upsert实现方法。"
);
...
...
@@ -508,8 +336,10 @@ public class UpsertPlugin extends BasePlugin {
// update
updateEle
.
addElement
(
new
TextElement
(
"update "
+
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
()));
updateEle
.
addElement
(
new
TextElement
(
"set"
));
// set 操作增加增量插件支持
this
.
setsSupportIncrementsPlugin
(
updateEle
,
"record."
,
false
,
columns
,
introspectedTable
);
// set
for
(
Element
set
:
XmlElementGeneratorTools
.
generateSets
(
columns
,
"record."
))
{
updateEle
.
addElement
(
set
);
}
// update where
updateEle
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
...
...
@@ -519,87 +349,30 @@ public class UpsertPlugin extends BasePlugin {
// insert
updateEle
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
for
(
Element
element
:
this
.
generateKeys
(
columns
,
"record."
))
{
for
(
Element
element
:
XmlElementGeneratorTools
.
generateKeys
(
columns
))
{
updateEle
.
addElement
(
element
);
}
this
.
generateExistsClause
(
introspectedTable
,
updateEle
,
false
,
columns
);
this
.
generateExistsClause
(
introspectedTable
,
updateEle
,
XmlElementGeneratorTools
.
generateValues
(
columns
,
"record."
,
false
)
);
document
.
getRootElement
().
addElement
(
updateEle
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加upsertByExample实现方法。"
);
}
}
/**
* sets 节点整合 IncrementsPlugin
* @param element
* @param prefix
* @param selective
* @param columns
* @param introspectedTable
*/
private
void
setsSupportIncrementsPlugin
(
XmlElement
element
,
String
prefix
,
boolean
selective
,
List
<
IntrospectedColumn
>
columns
,
IntrospectedTable
introspectedTable
)
{
IncrementsPluginTools
incTools
=
IncrementsPluginTools
.
getTools
(
context
,
introspectedTable
,
warnings
);
if
(
incTools
.
support
())
{
if
(
selective
)
{
element
.
addElement
(
incTools
.
generateSetsSelective
(
columns
,
prefix
));
}
else
{
for
(
Element
ele
:
this
.
generateCommColumns
(
columns
,
prefix
,
false
,
4
,
introspectedTable
))
{
element
.
addElement
(
ele
);
}
}
}
else
{
if
(
selective
)
{
element
.
addElement
(
XmlElementGeneratorTools
.
generateSetsSelective
(
columns
,
prefix
));
}
else
{
for
(
Element
ele
:
this
.
generateSets
(
columns
,
prefix
))
{
element
.
addElement
(
ele
);
}
}
}
}
/**
* exists 语句
* +635
* @param introspectedTable
* @param element
* @param selective
* @param columns
* @param values
*/
private
void
generateExistsClause
(
IntrospectedTable
introspectedTable
,
XmlElement
element
,
boolean
selective
,
List
<
IntrospectedColumn
>
column
s
)
{
private
void
generateExistsClause
(
IntrospectedTable
introspectedTable
,
XmlElement
element
,
List
<
Element
>
value
s
)
{
element
.
addElement
(
new
TextElement
(
"select"
));
if
(
selective
)
{
if
(
this
.
withSelectiveEnhancedPlugin
)
{
// selective
XmlElement
chooseEle
=
new
XmlElement
(
"choose"
);
element
.
addElement
(
chooseEle
);
XmlElement
selectWhenEle
=
new
XmlElement
(
"when"
);
selectWhenEle
.
addAttribute
(
new
Attribute
(
"test"
,
"selective != null and selective.length > 0"
));
chooseEle
.
addElement
(
selectWhenEle
);
XmlElement
valuesForeachEle
=
new
XmlElement
(
"foreach"
);
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
valuesForeachEle
.
addElement
(
new
TextElement
(
"#{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
selectWhenEle
.
addElement
(
valuesForeachEle
);
XmlElement
selectOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
selectOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
columns
,
"record."
,
false
));
chooseEle
.
addElement
(
selectOtherwiseEle
);
XmlElement
valuesTrimElement
=
new
XmlElement
(
"trim"
);
valuesTrimElement
.
addAttribute
(
new
Attribute
(
"suffixOverrides"
,
","
));
selectOtherwiseEle
.
addElement
(
valuesTrimElement
);
}
else
{
element
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
columns
,
"record."
,
false
));
}
}
else
{
for
(
Element
element1
:
this
.
generateValues
(
columns
,
"record."
,
false
))
{
element
.
addElement
(
element1
);
}
for
(
Element
value
:
values
)
{
element
.
addElement
(
value
);
}
element
.
addElement
(
new
TextElement
(
"from dual where not exists"
));
element
.
addElement
(
new
TextElement
(
"("
));
element
.
addElement
(
new
TextElement
(
"select 1 from "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
...
...
@@ -609,207 +382,4 @@ public class UpsertPlugin extends BasePlugin {
element
.
addElement
(
new
TextElement
(
")"
));
}
/**
* 生成values Ele
* @param columns
* @return
*/
private
List
<
Element
>
generateValues
(
List
<
IntrospectedColumn
>
columns
)
{
return
generateValues
(
columns
,
null
);
}
/**
* 生成values Ele
* @param columns
* @param prefix
* @return
*/
private
List
<
Element
>
generateValues
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
)
{
return
generateValues
(
columns
,
prefix
,
true
);
}
/**
* 生成values Ele
* @param columns
* @param prefix
* @param bracket
* @return
*/
private
List
<
Element
>
generateValues
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
,
boolean
bracket
)
{
return
generateCommColumns
(
columns
,
prefix
,
bracket
,
2
);
}
/**
* 生成keys Ele
* @param columns
* @return
*/
private
List
<
Element
>
generateKeys
(
List
<
IntrospectedColumn
>
columns
)
{
return
generateKeys
(
columns
,
null
);
}
/**
* 生成keys Ele
* @param columns
* @param prefix
* @return
*/
private
List
<
Element
>
generateKeys
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
)
{
return
generateCommColumns
(
columns
,
prefix
,
true
,
1
);
}
/**
* 生成sets Ele
* @param columns
* @param prefix
* @return
*/
private
List
<
Element
>
generateSets
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
)
{
return
generateCommColumns
(
columns
,
prefix
,
false
,
3
);
}
/**
* 通用遍历columns
* @param columns
* @param bracket
* @param prefix
* @param type 1:key,2:value,3:set
* @return
*/
private
List
<
Element
>
generateCommColumns
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
,
boolean
bracket
,
int
type
)
{
return
this
.
generateCommColumns
(
columns
,
prefix
,
bracket
,
type
,
null
);
}
/**
* 通用遍历columns
* @param columns
* @param bracket
* @param prefix
* @param type 1:key,2:value,3:set,4:set(withIncrementsPlugin)
* @param introspectedTable
* @return
*/
private
List
<
Element
>
generateCommColumns
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
,
boolean
bracket
,
int
type
,
IntrospectedTable
introspectedTable
)
{
IncrementsPluginTools
incTools
=
null
;
if
(
type
==
4
)
{
incTools
=
IncrementsPluginTools
.
getTools
(
context
,
introspectedTable
,
warnings
);
}
if
(
hasIdentityAndGeneratedAlwaysColumns
(
columns
))
{
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"
,
","
));
}
StringBuilder
sb
=
new
StringBuilder
();
for
(
IntrospectedColumn
introspectedColumn
:
columns
)
{
if
(
introspectedColumn
.
isGeneratedAlways
()
||
introspectedColumn
.
isIdentity
())
{
if
(
sb
.
length
()
>
0
)
{
eleTrim
.
addElement
(
new
TextElement
(
sb
.
toString
()));
sb
.
setLength
(
0
);
}
// if 节点
XmlElement
eleIf
=
new
XmlElement
(
"if"
);
eleIf
.
addAttribute
(
new
Attribute
(
"test"
,
introspectedColumn
.
getJavaProperty
(
prefix
)
+
" != null"
));
switch
(
type
)
{
case
4
:
if
(
incTools
.
supportColumn
(
introspectedColumn
))
{
for
(
Element
ele
:
incTools
.
generatedIncrementsElement
(
introspectedColumn
,
prefix
,
true
))
{
eleIf
.
addElement
(
ele
);
}
}
else
{
eleIf
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
" = "
+
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
)
+
","
));
}
break
;
case
3
:
eleIf
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
" = "
+
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
)
+
","
));
break
;
case
2
:
eleIf
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
)
+
","
));
break
;
case
1
:
eleIf
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
","
));
break
;
}
eleTrim
.
addElement
(
eleIf
);
}
else
{
switch
(
type
)
{
case
4
:
if
(
incTools
.
supportColumn
(
introspectedColumn
))
{
if
(
sb
.
length
()
>
0
)
{
eleTrim
.
addElement
(
new
TextElement
(
sb
.
toString
()));
sb
.
setLength
(
0
);
}
for
(
Element
ele
:
incTools
.
generatedIncrementsElement
(
introspectedColumn
,
prefix
,
true
))
{
eleTrim
.
addElement
(
ele
);
}
}
else
{
sb
.
append
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
));
sb
.
append
(
" = "
);
sb
.
append
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
));
}
break
;
case
3
:
sb
.
append
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
));
sb
.
append
(
" = "
);
sb
.
append
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
));
break
;
case
2
:
sb
.
append
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
));
break
;
case
1
:
sb
.
append
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
));
break
;
}
sb
.
append
(
", "
);
}
// 保持和官方一致 80 进行换行
if
(
type
==
1
||
type
==
2
)
{
if
(
sb
.
length
()
>
80
)
{
eleTrim
.
addElement
(
new
TextElement
(
sb
.
toString
()));
sb
.
setLength
(
0
);
OutputUtilities
.
xmlIndent
(
sb
,
1
);
}
}
else
{
eleTrim
.
addElement
(
new
TextElement
(
sb
.
toString
()));
sb
.
setLength
(
0
);
}
}
if
(
sb
.
length
()
>
0
)
{
eleTrim
.
addElement
(
new
TextElement
(
sb
.
toString
()));
}
return
Arrays
.
asList
(
eleTrim
);
}
else
if
(
type
==
4
)
{
return
incTools
.
generateSets
(
columns
,
prefix
,
bracket
);
}
else
{
return
XmlElementGeneratorTools
.
generateCommColumns
(
columns
,
prefix
,
bracket
,
type
);
}
}
/**
* 是否存在自增或者生成的column
* @param columns
* @return
*/
private
boolean
hasIdentityAndGeneratedAlwaysColumns
(
List
<
IntrospectedColumn
>
columns
)
{
for
(
IntrospectedColumn
ic
:
columns
)
{
if
(
ic
.
isGeneratedAlways
()
||
ic
.
isIdentity
())
{
return
true
;
}
}
return
false
;
}
}
\ No newline at end of file
src/main/java/com/itfsw/mybatis/generator/plugins/utils/BasePlugin.java
View file @
c33616c4
...
...
@@ -18,6 +18,7 @@ package com.itfsw.mybatis.generator.plugins.utils;
import
com.itfsw.mybatis.generator.plugins.CommentPlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.enhanced.TemplateCommentGenerator
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.HookAggregator
;
import
org.mybatis.generator.api.CommentGenerator
;
import
org.mybatis.generator.api.PluginAdapter
;
import
org.mybatis.generator.config.Context
;
...
...
@@ -45,19 +46,20 @@ public class BasePlugin extends PluginAdapter {
/**
* Set the context under which this plugin is running.
*
* @param context
* the new context
* @param context the new context
*/
@Override
public
void
setContext
(
Context
context
)
{
super
.
setContext
(
context
);
// 添加插件
HookAggregator
.
getInstance
().
setContext
(
context
);
// 配置插件使用的模板引擎
PluginConfiguration
cfg
=
PluginTools
.
getPluginConfiguration
(
context
,
CommentPlugin
.
class
);
if
(
cfg
==
null
||
cfg
.
getProperty
(
CommentPlugin
.
PRO_TEMPLATE
)
==
null
){
if
(
context
.
getCommentGenerator
()
instanceof
DefaultCommentGenerator
){
if
(
cfg
==
null
||
cfg
.
getProperty
(
CommentPlugin
.
PRO_TEMPLATE
)
==
null
)
{
if
(
context
.
getCommentGenerator
()
instanceof
DefaultCommentGenerator
)
{
// 使用默认模板引擎
commentGenerator
=
new
TemplateCommentGenerator
(
"default-comment.ftl"
,
true
);
}
else
{
...
...
@@ -79,7 +81,7 @@ public class BasePlugin extends PluginAdapter {
field
.
setAccessible
(
true
);
field
.
set
(
context
,
templateCommentGenerator
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"反射异常"
,
e
);
logger
.
error
(
"反射异常"
,
e
);
}
}
}
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/PluginTools.java
View file @
c33616c4
...
...
@@ -16,6 +16,7 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.HookAggregator
;
import
org.mybatis.generator.config.Context
;
import
org.mybatis.generator.config.PluginConfiguration
;
import
org.slf4j.Logger
;
...
...
@@ -36,6 +37,16 @@ import java.util.List;
public
class
PluginTools
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
PluginTools
.
class
);
/**
* 获取挂载
* @param clazz
* @param <T>
* @return
*/
public
static
<
T
>
T
getHook
(
Class
<
T
>
clazz
){
return
(
T
)
HookAggregator
.
getInstance
();
}
/**
* 检查插件依赖
*
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java
View file @
c33616c4
...
...
@@ -132,6 +132,58 @@ public class XmlElementGeneratorTools {
}
}
/**
* 移除 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
* @param element
* @param introspectedTable
*/
public
static
void
removeUseGeneratedKeys
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
GeneratedKey
gk
=
introspectedTable
.
getGeneratedKey
();
if
(
gk
!=
null
)
{
removeAttribute
(
element
,
"useGeneratedKeys"
);
removeAttribute
(
element
,
"keyProperty"
);
removeAttribute
(
element
,
"keyColumn"
);
}
}
/**
* 移除属性
* @param element
* @param name
*/
public
static
void
removeAttribute
(
XmlElement
element
,
String
name
){
Iterator
<
Attribute
>
iterator
=
element
.
getAttributes
().
iterator
();
while
(
iterator
.
hasNext
())
{
Attribute
attribute
=
iterator
.
next
();
if
(
attribute
.
getName
().
equals
(
name
))
{
iterator
.
remove
();
}
}
}
/**
* 替换属性
* @param element
* @param attribute
*/
public
static
void
replaceAttribute
(
XmlElement
element
,
Attribute
attribute
){
removeAttribute
(
element
,
attribute
.
getName
());
element
.
addAttribute
(
attribute
);
}
/**
* xmlElement 替换
* @param srcEle
* @param destEle
*/
public
static
void
replaceXmlElement
(
XmlElement
srcEle
,
XmlElement
destEle
){
srcEle
.
setName
(
destEle
.
getName
());
srcEle
.
getAttributes
().
clear
();
srcEle
.
getAttributes
().
addAll
(
destEle
.
getAttributes
());
srcEle
.
getElements
().
clear
();
srcEle
.
getElements
().
addAll
(
destEle
.
getElements
());
}
/**
* 生成keys Ele
* @param columns
...
...
@@ -156,7 +208,7 @@ public class XmlElementGeneratorTools {
* @param columns
* @return
*/
public
static
Element
generateKeysSelective
(
List
<
IntrospectedColumn
>
columns
)
{
public
static
Xml
Element
generateKeysSelective
(
List
<
IntrospectedColumn
>
columns
)
{
return
generateKeysSelective
(
columns
,
null
);
}
...
...
@@ -166,7 +218,7 @@ public class XmlElementGeneratorTools {
* @param prefix
* @return
*/
public
static
Element
generateKeysSelective
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
)
{
public
static
Xml
Element
generateKeysSelective
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
)
{
return
generateKeysSelective
(
columns
,
prefix
,
true
);
}
...
...
@@ -216,7 +268,7 @@ public class XmlElementGeneratorTools {
* @param columns
* @return
*/
public
static
Element
generateValuesSelective
(
List
<
IntrospectedColumn
>
columns
)
{
public
static
Xml
Element
generateValuesSelective
(
List
<
IntrospectedColumn
>
columns
)
{
return
generateValuesSelective
(
columns
,
null
);
}
...
...
@@ -226,7 +278,7 @@ public class XmlElementGeneratorTools {
* @param prefix
* @return
*/
public
static
Element
generateValuesSelective
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
)
{
public
static
Xml
Element
generateValuesSelective
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
)
{
return
generateValuesSelective
(
columns
,
prefix
,
true
);
}
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java
0 → 100644
View file @
c33616c4
/*
* 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.
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
.
hook
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.Plugin
;
import
org.mybatis.generator.api.dom.java.Interface
;
import
org.mybatis.generator.api.dom.java.Method
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
org.mybatis.generator.config.Context
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2018/4/27 11:33
* ---------------------------------------------------------------------------
*/
public
class
HookAggregator
implements
IUpsertPluginHook
{
protected
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
BasePlugin
.
class
);
// 日志
private
final
static
HookAggregator
instance
=
new
HookAggregator
();
private
Context
context
;
/**
* constructor
*/
public
HookAggregator
()
{
}
/**
* Getter method for property <tt>instance</tt>.
* @return property value of instance
* @author hewei
*/
public
static
HookAggregator
getInstance
()
{
return
instance
;
}
/**
* Setter method for property <tt>context</tt>.
* @param context value to be assigned to property context
* @author hewei
*/
public
void
setContext
(
Context
context
)
{
this
.
context
=
context
;
}
/**
* 获取挂载
* @param clazz
* @param <T>
* @return
*/
public
<
T
>
T
getHook
(
Class
<
T
>
clazz
)
{
return
(
T
)
this
;
}
/**
* 获取插件
* @param clazz
* @param <T>
* @return
*/
private
<
T
>
List
<
T
>
getPlugins
(
Class
<
T
>
clazz
)
{
List
list
=
new
ArrayList
();
// 反射获取插件列表,不能用单例去弄,不然因为类释放的问题而导致测试用例出问题
try
{
java
.
lang
.
reflect
.
Field
field
=
this
.
context
.
getPlugins
().
getClass
().
getDeclaredField
(
"plugins"
);
field
.
setAccessible
(
true
);
List
<
Plugin
>
plugins
=
(
List
<
Plugin
>)
field
.
get
(
this
.
context
.
getPlugins
());
for
(
Plugin
plugin
:
plugins
)
{
if
(
clazz
.
isInstance
(
plugin
))
{
list
.
add
(
plugin
);
}
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"获取插件列表失败!"
,
e
);
}
return
list
;
}
// ================================================= default ===============================================
@Override
public
boolean
clientUpsertSelectiveMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
for
(
IUpsertPluginHook
plugin
:
this
.
getPlugins
(
IUpsertPluginHook
.
class
))
{
if
(!
plugin
.
clientUpsertSelectiveMethodGenerated
(
method
,
interfaze
,
introspectedTable
))
{
return
false
;
}
}
return
true
;
}
@Override
public
boolean
clientUpsertByExampleSelectiveMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
for
(
IUpsertPluginHook
plugin
:
this
.
getPlugins
(
IUpsertPluginHook
.
class
))
{
if
(!
plugin
.
clientUpsertByExampleSelectiveMethodGenerated
(
method
,
interfaze
,
introspectedTable
))
{
return
false
;
}
}
return
true
;
}
@Override
public
boolean
sqlMapUpsertSelectiveElementGenerated
(
XmlElement
element
,
List
<
IntrospectedColumn
>
columns
,
XmlElement
insertColumnsEle
,
XmlElement
insertValuesEle
,
XmlElement
setsEle
,
IntrospectedTable
introspectedTable
)
{
for
(
IUpsertPluginHook
plugin
:
this
.
getPlugins
(
IUpsertPluginHook
.
class
))
{
if
(!
plugin
.
sqlMapUpsertSelectiveElementGenerated
(
element
,
columns
,
insertColumnsEle
,
insertValuesEle
,
setsEle
,
introspectedTable
))
{
return
false
;
}
}
return
true
;
}
@Override
public
boolean
sqlMapUpsertByExampleSelectiveElementGenerated
(
XmlElement
element
,
List
<
IntrospectedColumn
>
columns
,
XmlElement
insertColumnsEle
,
XmlElement
insertValuesEle
,
XmlElement
setsEle
,
IntrospectedTable
introspectedTable
)
{
for
(
IUpsertPluginHook
plugin
:
this
.
getPlugins
(
IUpsertPluginHook
.
class
))
{
if
(!
plugin
.
sqlMapUpsertByExampleSelectiveElementGenerated
(
element
,
columns
,
insertColumnsEle
,
insertValuesEle
,
setsEle
,
introspectedTable
))
{
return
false
;
}
}
return
true
;
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/IUpsertPluginHook.java
0 → 100644
View file @
c33616c4
/*
* 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.
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
.
hook
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.Interface
;
import
org.mybatis.generator.api.dom.java.Method
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
java.util.List
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2018/4/27 11:09
* ---------------------------------------------------------------------------
*/
public
interface
IUpsertPluginHook
{
/**
* upsertSelective 方法
* @param method
* @param interfaze
* @param introspectedTable
* @return
*/
boolean
clientUpsertSelectiveMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
);
/**
* upsertByExampleSelective 方法
* @param method
* @param interfaze
* @param introspectedTable
* @return
*/
boolean
clientUpsertByExampleSelectiveMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
);
/**
* upsertSelective xml
* @param element
* @param columns
* @param insertColumnsEle
* @param insertValuesEle
* @param setsEle
* @param introspectedTable
* @return
*/
boolean
sqlMapUpsertSelectiveElementGenerated
(
XmlElement
element
,
List
<
IntrospectedColumn
>
columns
,
XmlElement
insertColumnsEle
,
XmlElement
insertValuesEle
,
XmlElement
setsEle
,
IntrospectedTable
introspectedTable
);
/**
* upsertByExampleSelective xml
* @param element
* @param columns
* @param insertColumnsEle
* @param insertValuesEle
* @param setsEle
* @param introspectedTable
* @return
*/
boolean
sqlMapUpsertByExampleSelectiveElementGenerated
(
XmlElement
element
,
List
<
IntrospectedColumn
>
columns
,
XmlElement
insertColumnsEle
,
XmlElement
insertValuesEle
,
XmlElement
setsEle
,
IntrospectedTable
introspectedTable
);
}
src/test/java/com/itfsw/mybatis/generator/plugins/OldSelectiveEnhancedPluginTest.java
deleted
100644 → 0
View file @
cbfe10c9
/*
* Copyright (c) 2017.
*
* 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.
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.tools.*
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.Assert
;
import
org.junit.BeforeClass
;
import
org.junit.Test
;
import
org.mybatis.generator.exception.InvalidConfigurationException
;
import
org.mybatis.generator.exception.XMLParserException
;
import
java.io.IOException
;
import
java.lang.reflect.Array
;
import
java.sql.SQLException
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/7/28 15:47
* ---------------------------------------------------------------------------
*/
public
class
OldSelectiveEnhancedPluginTest
{
/**
* 初始化数据库
*/
@BeforeClass
public
static
void
init
()
throws
SQLException
,
IOException
,
ClassNotFoundException
{
DBHelper
.
createDB
(
"scripts/OldSelectiveEnhancedPlugin/init.sql"
);
}
/**
* 测试配置异常
*/
@Test
public
void
testWarnings
()
throws
IOException
,
XMLParserException
,
InvalidConfigurationException
,
InterruptedException
,
SQLException
{
// 1. 没有配置ModelColumnPlugin插件
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OldSelectiveEnhancedPlugin/mybatis-generator-without-ModelColumnPlugin.xml"
);
tool
.
generate
();
Assert
.
assertEquals
(
tool
.
getWarnings
().
get
(
0
),
"itfsw:插件com.itfsw.mybatis.generator.plugins.OldSelectiveEnhancedPlugin插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!"
);
// 2. 没有配置UpsertPlugin插件位置配置错误
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OldSelectiveEnhancedPlugin/mybatis-generator-with-UpsertPlugin-with-wrong-place.xml"
);
tool
.
generate
();
Assert
.
assertEquals
(
tool
.
getWarnings
().
get
(
0
),
"itfsw:插件com.itfsw.mybatis.generator.plugins.OldSelectiveEnhancedPlugin插件建议配置在插件com.itfsw.mybatis.generator.plugins.UpsertPlugin后面,否则某些功能可能得不到增强!"
);
}
/**
* 测试Model
*/
@Test
public
void
testModel
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OldSelectiveEnhancedPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
ObjectUtil
tb
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb"
);
ObjectUtil
TbColumnField1
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#field1"
);
ObjectUtil
TbColumnTsIncF2
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#tsIncF2"
);
Object
columns
=
Array
.
newInstance
(
TbColumnField1
.
getCls
(),
2
);
Array
.
set
(
columns
,
0
,
TbColumnField1
.
getObject
());
Array
.
set
(
columns
,
1
,
TbColumnTsIncF2
.
getObject
());
tb
.
invoke
(
"selective"
,
columns
);
Assert
.
assertTrue
((
Boolean
)
tb
.
invoke
(
"hasSelective"
));
Assert
.
assertTrue
((
Boolean
)
tb
.
invoke
(
"hasSelective"
,
"field_1"
));
Assert
.
assertFalse
((
Boolean
)
tb
.
invoke
(
"hasSelective"
,
"inc_f1"
));
Assert
.
assertTrue
((
Boolean
)
tb
.
invoke
(
"hasSelective"
,
"inc_f2"
));
}
});
}
/**
* 测试insertSelective增强
*/
@Test
public
void
testInsertSelective
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OldSelectiveEnhancedPlugin/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
tb
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb"
);
tb
.
set
(
"id"
,
111L
);
tb
.
set
(
"incF3"
,
10
l
);
tb
.
set
(
"tsIncF2"
,
5
l
);
// selective
ObjectUtil
TbColumnId
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#id"
);
ObjectUtil
TbColumnField1
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#field1"
);
ObjectUtil
TbColumnTsIncF2
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#tsIncF2"
);
Object
columns
=
Array
.
newInstance
(
TbColumnField1
.
getCls
(),
3
);
Array
.
set
(
columns
,
0
,
TbColumnId
.
getObject
());
Array
.
set
(
columns
,
1
,
TbColumnField1
.
getObject
());
Array
.
set
(
columns
,
2
,
TbColumnTsIncF2
.
getObject
());
tb
.
invoke
(
"selective"
,
columns
);
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"insertSelective"
,
tb
.
getObject
());
Assert
.
assertEquals
(
sql
,
"insert into tb ( id, field_1, inc_f2 ) values ( 111, 'null', 5 )"
);
Object
result
=
tbMapper
.
invoke
(
"insertSelective"
,
tb
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
}
});
}
/**
* 测试 updateByExampleSelective
*/
@Test
public
void
testUpdateByExampleSelective
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OldSelectiveEnhancedPlugin/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
TbExample
=
new
ObjectUtil
(
loader
,
packagz
+
".TbExample"
);
ObjectUtil
criteria
=
new
ObjectUtil
(
TbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andIdEqualTo"
,
1
l
);
ObjectUtil
tb
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb"
);
tb
.
set
(
"incF3"
,
10
l
);
tb
.
set
(
"tsIncF2"
,
5
l
);
// selective
ObjectUtil
TbColumnField1
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#field1"
);
ObjectUtil
TbColumnTsIncF2
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#tsIncF2"
);
Object
columns
=
Array
.
newInstance
(
TbColumnField1
.
getCls
(),
2
);
Array
.
set
(
columns
,
0
,
TbColumnField1
.
getObject
());
Array
.
set
(
columns
,
1
,
TbColumnTsIncF2
.
getObject
());
tb
.
invoke
(
"selective"
,
columns
);
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"updateByExampleSelective"
,
tb
.
getObject
(),
TbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb SET field_1 = 'null', inc_f2 = 5 WHERE ( id = '1' )"
);
Object
result
=
tbMapper
.
invoke
(
"updateByExampleSelective"
,
tb
.
getObject
(),
TbExample
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
}
});
}
/**
* 测试 updateByPrimaryKeySelective
*/
@Test
public
void
testUpdateByPrimaryKeySelective
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OldSelectiveEnhancedPlugin/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
tb
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb"
);
tb
.
set
(
"id"
,
2
l
);
tb
.
set
(
"incF3"
,
10
l
);
tb
.
set
(
"tsIncF2"
,
5
l
);
// selective
ObjectUtil
TbColumnField1
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#field1"
);
ObjectUtil
TbColumnTsIncF2
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#tsIncF2"
);
Object
columns
=
Array
.
newInstance
(
TbColumnField1
.
getCls
(),
2
);
Array
.
set
(
columns
,
0
,
TbColumnField1
.
getObject
());
Array
.
set
(
columns
,
1
,
TbColumnTsIncF2
.
getObject
());
tb
.
invoke
(
"selective"
,
columns
);
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"updateByPrimaryKeySelective"
,
tb
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb SET field_1 = 'null', inc_f2 = 5 where id = 2"
);
Object
result
=
tbMapper
.
invoke
(
"updateByPrimaryKeySelective"
,
tb
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
}
});
}
/**
* 测试 upsertSelective
*/
@Test
public
void
testUpsertSelective
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OldSelectiveEnhancedPlugin/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
tb
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb"
);
tb
.
set
(
"id"
,
10
l
);
tb
.
set
(
"incF3"
,
10
l
);
tb
.
set
(
"tsIncF2"
,
5
l
);
// selective
ObjectUtil
TbColumnId
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#id"
);
ObjectUtil
TbColumnField1
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#field1"
);
ObjectUtil
TbColumnTsIncF2
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#tsIncF2"
);
Object
columns
=
Array
.
newInstance
(
TbColumnField1
.
getCls
(),
3
);
Array
.
set
(
columns
,
0
,
TbColumnId
.
getObject
());
Array
.
set
(
columns
,
1
,
TbColumnField1
.
getObject
());
Array
.
set
(
columns
,
2
,
TbColumnTsIncF2
.
getObject
());
tb
.
invoke
(
"selective"
,
columns
);
// sql
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"
);
Object
result
=
tbMapper
.
invoke
(
"upsertSelective"
,
tb
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
}
});
}
/**
* 测试 upsertByExampleSelective
*/
@Test
public
void
testUpsertByExampleSelective
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OldSelectiveEnhancedPlugin/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
TbExample
=
new
ObjectUtil
(
loader
,
packagz
+
".TbExample"
);
ObjectUtil
criteria
=
new
ObjectUtil
(
TbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andIdEqualTo"
,
99
l
);
ObjectUtil
tb
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb"
);
tb
.
set
(
"id"
,
99
l
);
tb
.
set
(
"incF3"
,
10
l
);
tb
.
set
(
"tsIncF2"
,
5
l
);
// selective
ObjectUtil
TbColumnId
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#id"
);
ObjectUtil
TbColumnField1
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#field1"
);
ObjectUtil
TbColumnTsIncF2
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#tsIncF2"
);
Object
columns
=
Array
.
newInstance
(
TbColumnField1
.
getCls
(),
3
);
Array
.
set
(
columns
,
0
,
TbColumnId
.
getObject
());
Array
.
set
(
columns
,
1
,
TbColumnField1
.
getObject
());
Array
.
set
(
columns
,
2
,
TbColumnTsIncF2
.
getObject
());
tb
.
invoke
(
"selective"
,
columns
);
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"upsertByExampleSelective"
,
tb
.
getObject
(),
TbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb set id = 99, inc_f2 = 5, inc_f3 = 10 WHERE ( id = '99' ) ; insert into tb ( id, field_1, inc_f2 ) select 99, 'null', 5 from dual where not exists ( select 1 from tb WHERE ( id = '99' ) )"
);
Object
result
=
tbMapper
.
invoke
(
"upsertByExampleSelective"
,
tb
.
getObject
(),
TbExample
.
getObject
());
Assert
.
assertEquals
(
result
,
0
);
result
=
tbMapper
.
invoke
(
"upsertByExampleSelective"
,
tb
.
getObject
(),
TbExample
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
}
});
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPluginTest.java
View file @
c33616c4
...
...
@@ -42,7 +42,7 @@ public class SelectiveEnhancedPluginTest {
*/
@BeforeClass
public
static
void
init
()
throws
SQLException
,
IOException
,
ClassNotFoundException
{
DBHelper
.
createDB
(
"scripts/
Old
SelectiveEnhancedPlugin/init.sql"
);
DBHelper
.
createDB
(
"scripts/SelectiveEnhancedPlugin/init.sql"
);
}
/**
...
...
@@ -54,11 +54,6 @@ public class SelectiveEnhancedPluginTest {
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/SelectiveEnhancedPlugin/mybatis-generator-without-ModelColumnPlugin.xml"
);
tool
.
generate
();
Assert
.
assertEquals
(
tool
.
getWarnings
().
get
(
0
),
"itfsw:插件com.itfsw.mybatis.generator.plugins.SelectiveEnhancedPlugin插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!"
);
// 2. 同时配置了OldSelectiveEnhancedPlugin插件
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/SelectiveEnhancedPlugin/mybatis-generator-with-OldSelectiveEnhancedPlugin.xml"
);
tool
.
generate
();
Assert
.
assertEquals
(
tool
.
getWarnings
().
get
(
0
),
"itfsw:插件com.itfsw.mybatis.generator.plugins.SelectiveEnhancedPlugin不能和com.itfsw.mybatis.generator.plugins.OldSelectiveEnhancedPlugin插件同时使用!"
);
}
/**
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java
View file @
c33616c4
...
...
@@ -19,7 +19,6 @@ package com.itfsw.mybatis.generator.plugins;
import
com.itfsw.mybatis.generator.plugins.tools.*
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.Assert
;
import
org.junit.BeforeClass
;
import
org.junit.Test
;
import
org.mybatis.generator.exception.InvalidConfigurationException
;
import
org.mybatis.generator.exception.XMLParserException
;
...
...
@@ -37,13 +36,6 @@ import java.sql.SQLException;
* ---------------------------------------------------------------------------
*/
public
class
UpsertPluginTest
{
/**
* 初始化数据库
*/
@BeforeClass
public
static
void
init
()
throws
SQLException
,
IOException
,
ClassNotFoundException
{
DBHelper
.
createDB
(
"scripts/UpsertPlugin/init.sql"
);
}
/**
* 测试配置异常
...
...
@@ -67,7 +59,7 @@ public class UpsertPluginTest {
@Test
public
void
testUpsert
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/UpsertPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
tool
.
generate
(
()
->
DBHelper
.
createDB
(
"scripts/UpsertPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
...
...
@@ -99,7 +91,7 @@ public class UpsertPluginTest {
@Test
public
void
testUpsertWithBLOBs
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/UpsertPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
tool
.
generate
(
()
->
DBHelper
.
createDB
(
"scripts/UpsertPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
// 1. 多个 blob
...
...
@@ -153,7 +145,7 @@ public class UpsertPluginTest {
@Test
public
void
testUpsertSelective
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/UpsertPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
tool
.
generate
(
()
->
DBHelper
.
createDB
(
"scripts/UpsertPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
// 1. 普通
...
...
@@ -192,7 +184,7 @@ public class UpsertPluginTest {
@Test
public
void
testUpsertByExample
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/UpsertPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
tool
.
generate
(
()
->
DBHelper
.
createDB
(
"scripts/UpsertPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
...
...
@@ -228,7 +220,7 @@ public class UpsertPluginTest {
@Test
public
void
testUpsertByExampleWithBLOBs
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/UpsertPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
tool
.
generate
(
()
->
DBHelper
.
createDB
(
"scripts/UpsertPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
// 1. 多个 blob
...
...
@@ -289,7 +281,7 @@ public class UpsertPluginTest {
@Test
public
void
testUpsertByExampleSelective
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/UpsertPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
tool
.
generate
(
()
->
DBHelper
.
createDB
(
"scripts/UpsertPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
// 1. 普通
...
...
@@ -334,7 +326,7 @@ public class UpsertPluginTest {
@Test
public
void
testWithIdentityAndGeneratedAlwaysColumns
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/UpsertPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
tool
.
generate
(
()
->
DBHelper
.
createDB
(
"scripts/UpsertPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
// ------------------------------------------ upsert ---------------------------------------------------
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/tools/DBHelper.java
View file @
c33616c4
...
...
@@ -64,13 +64,14 @@ public class DBHelper {
String
password
=
properties
.
getProperty
(
"password"
);
// 获取connection
Class
.
forName
(
driver
);
try
(
Connection
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
Statement
statement
=
connection
.
createStatement
();
// 获取建表和初始化sql
InputStream
inputStream
=
Resources
.
getResourceAsStream
(
resource
);
InputStreamReader
inputStreamReader
=
new
InputStreamReader
(
inputStream
,
"UTF-8"
);
BufferedReader
bufferedReader
=
new
BufferedReader
(
inputStreamReader
);
)
{
// 读取sql语句执行
StringBuffer
sb
=
new
StringBuffer
();
String
line
;
...
...
@@ -81,16 +82,11 @@ public class DBHelper {
sb
.
setLength
(
0
);
}
}
bufferedReader
.
close
();
inputStreamReader
.
close
();
inputStream
.
close
();
statement
.
close
();
connection
.
close
();
}
}
/**
* 执行sql
*
* @param sqlSession
* @param sql
* @return
...
...
@@ -102,7 +98,6 @@ public class DBHelper {
/**
* 执行sql
*
* @param connection
* @param sql
* @return
...
...
src/test/resources/scripts/OldSelectiveEnhancedPlugin/init.sql
deleted
100644 → 0
View file @
cbfe10c9
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50617
Source Host : localhost:3306
Source Database : mybatis-generator-plugin
Target Server Type : MYSQL
Target Server Version : 50617
File Encoding : 65001
Date: 2017-07-05 17:21:41
*/
SET
FOREIGN_KEY_CHECKS
=
0
;
-- ----------------------------
-- Table structure for tb
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb`
;
CREATE
TABLE
`tb`
(
`id`
bigint
(
20
)
NOT
NULL
COMMENT
'注释1'
,
`field_1`
varchar
(
255
)
DEFAULT
NULL
COMMENT
'注释2'
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f3`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
AUTO_INCREMENT
=
5
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb
-- ----------------------------
INSERT
INTO
`tb`
VALUES
(
'1'
,
'fd1'
,
'0'
,
'0'
,
'0'
);
INSERT
INTO
`tb`
VALUES
(
'2'
,
'fd2'
,
'1'
,
'2'
,
'3'
);
INSERT
INTO
`tb`
VALUES
(
'3'
,
null
,
'3'
,
'2'
,
'1'
);
INSERT
INTO
`tb`
VALUES
(
'4'
,
'fd3'
,
'1'
,
'1'
,
'1'
);
\ No newline at end of file
src/test/resources/scripts/OldSelectiveEnhancedPlugin/mybatis-generator-with-UpsertPlugin-with-wrong-place.xml
deleted
100644 → 0
View file @
cbfe10c9
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2017.
~
~ 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.OldSelectiveEnhancedPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.UpsertPlugin"
/>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${driver}"
connectionURL=
"${url}"
userId=
"${username}"
password=
"${password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
""
targetProject=
""
/>
<!--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"
>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
<columnOverride
column=
"inc_f2"
property=
"tsIncF2"
/>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
src/test/resources/scripts/OldSelectiveEnhancedPlugin/mybatis-generator-without-ModelColumnPlugin.xml
deleted
100644 → 0
View file @
cbfe10c9
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2017.
~
~ 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.OldSelectiveEnhancedPlugin"
/>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${driver}"
connectionURL=
"${url}"
userId=
"${username}"
password=
"${password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
""
targetProject=
""
/>
<!--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"
>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
<columnOverride
column=
"inc_f2"
property=
"tsIncF2"
/>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
src/test/resources/scripts/OldSelectiveEnhancedPlugin/mybatis-generator.xml
deleted
100644 → 0
View file @
cbfe10c9
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2017.
~
~ 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.UpsertPlugin"
>
<property
name=
"allowMultiQueries"
value=
"true"
/>
</plugin>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.OldSelectiveEnhancedPlugin"
/>
<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=
""
/>
<!--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"
>
<columnOverride
column=
"inc_f2"
property=
"tsIncF2"
/>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
src/test/resources/scripts/SelectiveEnhancedPlugin/mybatis-generator-with-OldSelectiveEnhancedPlugin.xml
deleted
100644 → 0
View file @
cbfe10c9
<?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.SelectiveEnhancedPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.OldSelectiveEnhancedPlugin"
/>
<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=
""
/>
<!--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"
>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
<columnOverride
column=
"inc_f2"
property=
"tsIncF2"
/>
</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