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
2571f1c5
Commit
2571f1c5
authored
May 17, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
对于Model生成WithBLOBs类时,upsert插件实现行为和官方插件保持一致
parent
30a3539f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
238 additions
and
26 deletions
+238
-26
src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
...sw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
+22
-3
src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
...ava/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
+216
-23
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
View file @
2571f1c5
...
...
@@ -169,14 +169,32 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
List
<
XmlElement
>
eles
=
this
.
findEle
(
xmlElement
,
"trim"
);
this
.
replaceEle
(
eles
.
get
(
0
),
"record."
);
// upsertByExampleSelective的第二个trim比较特殊,需另行处理
this
.
replaceEleForUpsertByExampleSelective
(
eles
.
get
(
1
),
"record."
,
introspectedTable
);
this
.
replaceEleForUpsertByExampleSelective
(
eles
.
get
(
1
),
"record."
,
introspectedTable
,
false
);
List
<
XmlElement
>
eles1
=
this
.
findEle
(
xmlElement
,
"set"
);
for
(
XmlElement
ele
:
eles1
)
{
this
.
replaceEle
(
ele
,
"record."
);
}
}
// ====================================== 6. upsertSelectiveWithBLOBs ======================================
if
(
"upsertSelectiveWithBLOBs"
.
equals
(
id
))
{
List
<
XmlElement
>
eles
=
this
.
findEle
(
xmlElement
,
"trim"
);
for
(
XmlElement
ele
:
eles
)
{
this
.
replaceEle
(
ele
,
"_parameter."
);
}
}
// ====================================== 7. upsertByExampleSelectiveWithBLOBs ======================================
if
(
"upsertByExampleSelectiveWithBLOBs"
.
equals
(
id
))
{
List
<
XmlElement
>
eles
=
this
.
findEle
(
xmlElement
,
"trim"
);
this
.
replaceEle
(
eles
.
get
(
0
),
"record."
);
// upsertByExampleSelective的第二个trim比较特殊,需另行处理
this
.
replaceEleForUpsertByExampleSelective
(
eles
.
get
(
1
),
"record."
,
introspectedTable
,
true
);
List
<
XmlElement
>
eles1
=
this
.
findEle
(
xmlElement
,
"set"
);
for
(
XmlElement
ele
:
eles1
)
{
this
.
replaceEle
(
ele
,
"record."
);
}
}
}
}
return
true
;
...
...
@@ -262,14 +280,15 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
* @param element
* @param prefix
* @param introspectedTable
* @param withBLOBs
*/
private
void
replaceEleForUpsertByExampleSelective
(
XmlElement
element
,
String
prefix
,
IntrospectedTable
introspectedTable
)
{
private
void
replaceEleForUpsertByExampleSelective
(
XmlElement
element
,
String
prefix
,
IntrospectedTable
introspectedTable
,
boolean
withBLOBs
)
{
// choose
XmlElement
chooseEle
=
new
XmlElement
(
"choose"
);
// when
XmlElement
whenEle
=
new
XmlElement
(
"when"
);
whenEle
.
addAttribute
(
new
Attribute
(
"test"
,
prefix
+
"isSelective()"
));
for
(
IntrospectedColumn
introspectedColumn
:
introspectedTable
.
getAll
Columns
())
{
for
(
IntrospectedColumn
introspectedColumn
:
withBLOBs
?
introspectedTable
.
getAllColumns
()
:
introspectedTable
.
getNonBLOB
Columns
())
{
XmlElement
eleIf
=
new
XmlElement
(
"if"
);
eleIf
.
addAttribute
(
new
Attribute
(
"test"
,
prefix
+
"isSelective(\'"
+
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
"\')"
));
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
View file @
2571f1c5
...
...
@@ -19,6 +19,7 @@ package com.itfsw.mybatis.generator.plugins;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools
;
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.Attribute
;
...
...
@@ -45,6 +46,11 @@ public class UpsertPlugin extends BasePlugin {
public
static
final
String
METHOD_UPSERT_BY_EXAMPLE
=
"upsertByExample"
;
// 方法名
public
static
final
String
METHOD_UPSERT_BY_EXAMPLE_SELECTIVE
=
"upsertByExampleSelective"
;
// 方法名
public
static
final
String
METHOD_UPSERT_WITH_BLOBS
=
"upsertWithBLOBs"
;
// 方法名
public
static
final
String
METHOD_UPSERT_SELECTIVE_WITH_BLOBS
=
"upsertSelectiveWithBLOBs"
;
// 方法名
public
static
final
String
METHOD_UPSERT_BY_EXAMPLE_WITH_BLOBS
=
"upsertByExampleWithBLOBs"
;
// 方法名
public
static
final
String
METHOD_UPSERT_BY_EXAMPLE_SELECTIVE_WITH_BLOBS
=
"upsertByExampleSelectiveWithBLOBs"
;
// 方法名
public
static
final
String
PRE_ALLOW_MULTI_QUERIES
=
"allowMultiQueries"
;
// property allowMultiQueries
private
boolean
allowMultiQueries
=
false
;
// 是否允许多sql提交
...
...
@@ -88,7 +94,7 @@ public class UpsertPlugin extends BasePlugin {
METHOD_UPSERT
,
JavaVisibility
.
DEFAULT
,
FullyQualifiedJavaType
.
getIntInstance
(),
new
Parameter
(
introspectedTable
.
getRules
().
calculateAllFieldsClass
(
),
"record"
)
new
Parameter
(
JavaElementGeneratorTools
.
getModelTypeWithoutBLOBs
(
introspectedTable
),
"record"
)
);
commentGenerator
.
addGeneralMethodComment
(
mUpsert
,
introspectedTable
);
// interface 增加方法
...
...
@@ -100,7 +106,7 @@ public class UpsertPlugin extends BasePlugin {
METHOD_UPSERT_SELECTIVE
,
JavaVisibility
.
DEFAULT
,
FullyQualifiedJavaType
.
getIntInstance
(),
new
Parameter
(
introspectedTable
.
getRules
().
calculateAllFieldsClass
(
),
"record"
)
new
Parameter
(
JavaElementGeneratorTools
.
getModelTypeWithoutBLOBs
(
introspectedTable
),
"record"
)
);
commentGenerator
.
addGeneralMethodComment
(
mUpsertSelective
,
introspectedTable
);
// interface 增加方法
...
...
@@ -113,7 +119,7 @@ public class UpsertPlugin extends BasePlugin {
METHOD_UPSERT_BY_EXAMPLE
,
JavaVisibility
.
DEFAULT
,
FullyQualifiedJavaType
.
getIntInstance
(),
new
Parameter
(
introspectedTable
.
getRules
().
calculateAllFieldsClass
(
),
"record"
,
"@Param(\"record\")"
),
new
Parameter
(
JavaElementGeneratorTools
.
getModelTypeWithoutBLOBs
(
introspectedTable
),
"record"
,
"@Param(\"record\")"
),
new
Parameter
(
new
FullyQualifiedJavaType
(
introspectedTable
.
getExampleType
()),
"example"
,
"@Param(\"example\")"
)
);
commentGenerator
.
addGeneralMethodComment
(
mUpsertByExample
,
introspectedTable
);
...
...
@@ -126,7 +132,7 @@ public class UpsertPlugin extends BasePlugin {
METHOD_UPSERT_BY_EXAMPLE_SELECTIVE
,
JavaVisibility
.
DEFAULT
,
FullyQualifiedJavaType
.
getIntInstance
(),
new
Parameter
(
introspectedTable
.
getRules
().
calculateAllFieldsClass
(
),
"record"
,
"@Param(\"record\")"
),
new
Parameter
(
JavaElementGeneratorTools
.
getModelTypeWithoutBLOBs
(
introspectedTable
),
"record"
,
"@Param(\"record\")"
),
new
Parameter
(
new
FullyQualifiedJavaType
(
introspectedTable
.
getExampleType
()),
"example"
,
"@Param(\"example\")"
)
);
commentGenerator
.
addGeneralMethodComment
(
mUpsertByExampleSelective
,
introspectedTable
);
...
...
@@ -135,6 +141,60 @@ public class UpsertPlugin extends BasePlugin {
logger
.
debug
(
"itfsw(存在即更新插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加upsertByExampleSelective方法。"
);
}
if
(
introspectedTable
.
getRules
().
generateRecordWithBLOBsClass
()){
// ====================================== 1. upsertWithBLOBs ======================================
Method
mUpsertWithBLOBs
=
JavaElementGeneratorTools
.
generateMethod
(
METHOD_UPSERT_WITH_BLOBS
,
JavaVisibility
.
DEFAULT
,
FullyQualifiedJavaType
.
getIntInstance
(),
new
Parameter
(
JavaElementGeneratorTools
.
getModelTypeWithBLOBs
(
introspectedTable
),
"record"
)
);
commentGenerator
.
addGeneralMethodComment
(
mUpsertWithBLOBs
,
introspectedTable
);
// interface 增加方法
interfaze
.
addMethod
(
mUpsertWithBLOBs
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加upsert方法。"
);
// ====================================== 2. upsertSelectiveWithBLOBs ======================================
Method
mUpsertSelectiveWithBLOBs
=
JavaElementGeneratorTools
.
generateMethod
(
METHOD_UPSERT_SELECTIVE_WITH_BLOBS
,
JavaVisibility
.
DEFAULT
,
FullyQualifiedJavaType
.
getIntInstance
(),
new
Parameter
(
JavaElementGeneratorTools
.
getModelTypeWithBLOBs
(
introspectedTable
),
"record"
)
);
commentGenerator
.
addGeneralMethodComment
(
mUpsertSelectiveWithBLOBs
,
introspectedTable
);
// interface 增加方法
interfaze
.
addMethod
(
mUpsertSelectiveWithBLOBs
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加upsertSelective方法。"
);
if
(
this
.
allowMultiQueries
){
// ====================================== 3. upsertByExampleWithBLOBs ======================================
Method
mUpsertByExampleWithBLOBs
=
JavaElementGeneratorTools
.
generateMethod
(
METHOD_UPSERT_BY_EXAMPLE_WITH_BLOBS
,
JavaVisibility
.
DEFAULT
,
FullyQualifiedJavaType
.
getIntInstance
(),
new
Parameter
(
JavaElementGeneratorTools
.
getModelTypeWithBLOBs
(
introspectedTable
),
"record"
,
"@Param(\"record\")"
),
new
Parameter
(
new
FullyQualifiedJavaType
(
introspectedTable
.
getExampleType
()),
"example"
,
"@Param(\"example\")"
)
);
commentGenerator
.
addGeneralMethodComment
(
mUpsertByExampleWithBLOBs
,
introspectedTable
);
// interface 增加方法
interfaze
.
addMethod
(
mUpsertByExampleWithBLOBs
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加upsertByExample方法。"
);
// ====================================== 4. upsertByExampleSelectiveWithBLOBs ======================================
Method
mUpsertByExampleSelectiveWithBLOBs
=
JavaElementGeneratorTools
.
generateMethod
(
METHOD_UPSERT_BY_EXAMPLE_SELECTIVE_WITH_BLOBS
,
JavaVisibility
.
DEFAULT
,
FullyQualifiedJavaType
.
getIntInstance
(),
new
Parameter
(
JavaElementGeneratorTools
.
getModelTypeWithBLOBs
(
introspectedTable
),
"record"
,
"@Param(\"record\")"
),
new
Parameter
(
new
FullyQualifiedJavaType
(
introspectedTable
.
getExampleType
()),
"example"
,
"@Param(\"example\")"
)
);
commentGenerator
.
addGeneralMethodComment
(
mUpsertByExampleSelectiveWithBLOBs
,
introspectedTable
);
// interface 增加方法
interfaze
.
addMethod
(
mUpsertByExampleSelectiveWithBLOBs
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加upsertByExampleSelective方法。"
);
}
}
return
true
;
}
...
...
@@ -149,6 +209,137 @@ public class UpsertPlugin extends BasePlugin {
@Override
public
boolean
sqlMapDocumentGenerated
(
Document
document
,
IntrospectedTable
introspectedTable
)
{
this
.
generateXmlElementWithoutBLOBs
(
document
,
introspectedTable
);
if
(
introspectedTable
.
getRules
().
generateRecordWithBLOBsClass
()){
this
.
generateXmlElementWithBLOBs
(
document
,
introspectedTable
);
}
return
true
;
}
/**
* 当Model有生成WithBLOBs类时的情况
*
* @param document
* @param introspectedTable
*/
private
void
generateXmlElementWithBLOBs
(
Document
document
,
IntrospectedTable
introspectedTable
){
// ====================================== 1. upsert ======================================
XmlElement
eleUpsertWithBLOBs
=
new
XmlElement
(
"insert"
);
eleUpsertWithBLOBs
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_UPSERT_WITH_BLOBS
));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
commentGenerator
.
addComment
(
eleUpsertWithBLOBs
);
// 参数类型
eleUpsertWithBLOBs
.
addAttribute
(
new
Attribute
(
"parameterType"
,
JavaElementGeneratorTools
.
getModelTypeWithBLOBs
(
introspectedTable
).
getFullyQualifiedName
()));
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools
.
useGeneratedKeys
(
eleUpsertWithBLOBs
,
introspectedTable
);
// insert
eleUpsertWithBLOBs
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
eleUpsertWithBLOBs
.
addElement
(
XmlElementGeneratorTools
.
generateKeys
(
introspectedTable
.
getAllColumns
()));
eleUpsertWithBLOBs
.
addElement
(
new
TextElement
(
"values"
));
eleUpsertWithBLOBs
.
addElement
(
XmlElementGeneratorTools
.
generateValues
(
introspectedTable
.
getAllColumns
()));
eleUpsertWithBLOBs
.
addElement
(
new
TextElement
(
"on duplicate key update "
));
eleUpsertWithBLOBs
.
addElement
(
XmlElementGeneratorTools
.
generateSets
(
introspectedTable
.
getAllColumns
()));
document
.
getRootElement
().
addElement
(
eleUpsertWithBLOBs
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加upsert实现方法。"
);
// ====================================== 2. upsertSelective ======================================
XmlElement
eleUpsertSelectiveWithBLOBs
=
new
XmlElement
(
"insert"
);
eleUpsertSelectiveWithBLOBs
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_UPSERT_SELECTIVE_WITH_BLOBS
));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
commentGenerator
.
addComment
(
eleUpsertSelectiveWithBLOBs
);
// 参数类型
eleUpsertSelectiveWithBLOBs
.
addAttribute
(
new
Attribute
(
"parameterType"
,
JavaElementGeneratorTools
.
getModelTypeWithBLOBs
(
introspectedTable
).
getFullyQualifiedName
()));
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools
.
useGeneratedKeys
(
eleUpsertSelectiveWithBLOBs
,
introspectedTable
);
// insert
eleUpsertSelectiveWithBLOBs
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
eleUpsertSelectiveWithBLOBs
.
addElement
(
XmlElementGeneratorTools
.
generateKeysSelective
(
introspectedTable
.
getAllColumns
()));
eleUpsertSelectiveWithBLOBs
.
addElement
(
new
TextElement
(
"values"
));
eleUpsertSelectiveWithBLOBs
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
introspectedTable
.
getAllColumns
()));
eleUpsertSelectiveWithBLOBs
.
addElement
(
new
TextElement
(
"on duplicate key update "
));
eleUpsertSelectiveWithBLOBs
.
addElement
(
XmlElementGeneratorTools
.
generateSetsSelective
(
introspectedTable
.
getAllColumns
(),
null
,
false
));
document
.
getRootElement
().
addElement
(
eleUpsertSelectiveWithBLOBs
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加upsertSelective实现方法。"
);
if
(
this
.
allowMultiQueries
){
// ====================================== 2. upsertByExample ======================================
XmlElement
eleUpsertByExampleWithBLOBs
=
new
XmlElement
(
"insert"
);
eleUpsertByExampleWithBLOBs
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_UPSERT_BY_EXAMPLE_WITH_BLOBS
));
// 参数类型
eleUpsertByExampleWithBLOBs
.
addAttribute
(
new
Attribute
(
"parameterType"
,
"map"
));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
commentGenerator
.
addComment
(
eleUpsertByExampleWithBLOBs
);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools
.
useGeneratedKeys
(
eleUpsertByExampleWithBLOBs
,
introspectedTable
,
"record."
);
// insert
eleUpsertByExampleWithBLOBs
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
eleUpsertByExampleWithBLOBs
.
addElement
(
XmlElementGeneratorTools
.
generateKeys
(
introspectedTable
.
getAllColumns
()));
this
.
generateExistsClause
(
introspectedTable
,
eleUpsertByExampleWithBLOBs
,
false
,
true
);
// multiQueries
eleUpsertByExampleWithBLOBs
.
addElement
(
new
TextElement
(
";"
));
// update
eleUpsertByExampleWithBLOBs
.
addElement
(
new
TextElement
(
"update "
+
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
()));
eleUpsertByExampleWithBLOBs
.
addElement
(
new
TextElement
(
"set"
));
eleUpsertByExampleWithBLOBs
.
addElement
(
XmlElementGeneratorTools
.
generateSets
(
ListUtilities
.
removeIdentityAndGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
()),
"record."
));
// update where
eleUpsertByExampleWithBLOBs
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
document
.
getRootElement
().
addElement
(
eleUpsertByExampleWithBLOBs
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加upsertSelective实现方法。"
);
// ====================================== 4. upsertByExampleSelective ======================================
XmlElement
eleUpsertByExampleSelectiveWithBLOBs
=
new
XmlElement
(
"insert"
);
eleUpsertByExampleSelectiveWithBLOBs
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_UPSERT_BY_EXAMPLE_SELECTIVE_WITH_BLOBS
));
// 参数类型
eleUpsertByExampleSelectiveWithBLOBs
.
addAttribute
(
new
Attribute
(
"parameterType"
,
"map"
));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
commentGenerator
.
addComment
(
eleUpsertByExampleSelectiveWithBLOBs
);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools
.
useGeneratedKeys
(
eleUpsertByExampleSelectiveWithBLOBs
,
introspectedTable
,
"record."
);
// insert
eleUpsertByExampleSelectiveWithBLOBs
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
eleUpsertByExampleSelectiveWithBLOBs
.
addElement
(
XmlElementGeneratorTools
.
generateKeysSelective
(
introspectedTable
.
getAllColumns
(),
"record."
));
this
.
generateExistsClause
(
introspectedTable
,
eleUpsertByExampleSelectiveWithBLOBs
,
true
,
true
);
// multiQueries
eleUpsertByExampleSelectiveWithBLOBs
.
addElement
(
new
TextElement
(
";"
));
// update
eleUpsertByExampleSelectiveWithBLOBs
.
addElement
(
new
TextElement
(
"update "
+
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
()));
eleUpsertByExampleSelectiveWithBLOBs
.
addElement
(
new
TextElement
(
"set"
));
eleUpsertByExampleSelectiveWithBLOBs
.
addElement
(
XmlElementGeneratorTools
.
generateSetsSelective
(
ListUtilities
.
removeIdentityAndGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
()),
"record."
));
// update where
eleUpsertByExampleSelectiveWithBLOBs
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
document
.
getRootElement
().
addElement
(
eleUpsertByExampleSelectiveWithBLOBs
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加upsertSelective实现方法。"
);
}
}
/**
* 当Model没有生成WithBLOBs类时的情况
*
* @param document
* @param introspectedTable
*/
private
void
generateXmlElementWithoutBLOBs
(
Document
document
,
IntrospectedTable
introspectedTable
){
// ====================================== 1. upsert ======================================
XmlElement
eleUpsert
=
new
XmlElement
(
"insert"
);
eleUpsert
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_UPSERT
));
...
...
@@ -156,18 +347,18 @@ public class UpsertPlugin extends BasePlugin {
commentGenerator
.
addComment
(
eleUpsert
);
// 参数类型
eleUpsert
.
addAttribute
(
new
Attribute
(
"parameterType"
,
introspectedTable
.
getRules
().
calculateAllFieldsClass
(
).
getFullyQualifiedName
()));
eleUpsert
.
addAttribute
(
new
Attribute
(
"parameterType"
,
JavaElementGeneratorTools
.
getModelTypeWithoutBLOBs
(
introspectedTable
).
getFullyQualifiedName
()));
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools
.
useGeneratedKeys
(
eleUpsert
,
introspectedTable
);
// insert
eleUpsert
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
eleUpsert
.
addElement
(
XmlElementGeneratorTools
.
generateKeys
(
introspectedTable
.
get
All
Columns
()));
eleUpsert
.
addElement
(
XmlElementGeneratorTools
.
generateKeys
(
introspectedTable
.
get
NonBLOB
Columns
()));
eleUpsert
.
addElement
(
new
TextElement
(
"values"
));
eleUpsert
.
addElement
(
XmlElementGeneratorTools
.
generateValues
(
introspectedTable
.
get
All
Columns
()));
eleUpsert
.
addElement
(
XmlElementGeneratorTools
.
generateValues
(
introspectedTable
.
get
NonBLOB
Columns
()));
eleUpsert
.
addElement
(
new
TextElement
(
"on duplicate key update "
));
eleUpsert
.
addElement
(
XmlElementGeneratorTools
.
generateSets
(
introspectedTable
.
get
All
Columns
()));
eleUpsert
.
addElement
(
XmlElementGeneratorTools
.
generateSets
(
introspectedTable
.
get
NonBLOB
Columns
()));
document
.
getRootElement
().
addElement
(
eleUpsert
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加upsert实现方法。"
);
...
...
@@ -179,18 +370,18 @@ public class UpsertPlugin extends BasePlugin {
commentGenerator
.
addComment
(
eleUpsertSelective
);
// 参数类型
eleUpsertSelective
.
addAttribute
(
new
Attribute
(
"parameterType"
,
introspectedTable
.
getRules
().
calculateAllFieldsClass
(
).
getFullyQualifiedName
()));
eleUpsertSelective
.
addAttribute
(
new
Attribute
(
"parameterType"
,
JavaElementGeneratorTools
.
getModelTypeWithoutBLOBs
(
introspectedTable
).
getFullyQualifiedName
()));
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools
.
useGeneratedKeys
(
eleUpsertSelective
,
introspectedTable
);
// insert
eleUpsertSelective
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
eleUpsertSelective
.
addElement
(
XmlElementGeneratorTools
.
generateKeysSelective
(
introspectedTable
.
get
All
Columns
()));
eleUpsertSelective
.
addElement
(
XmlElementGeneratorTools
.
generateKeysSelective
(
introspectedTable
.
get
NonBLOB
Columns
()));
eleUpsertSelective
.
addElement
(
new
TextElement
(
"values"
));
eleUpsertSelective
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
introspectedTable
.
get
All
Columns
()));
eleUpsertSelective
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
introspectedTable
.
get
NonBLOB
Columns
()));
eleUpsertSelective
.
addElement
(
new
TextElement
(
"on duplicate key update "
));
eleUpsertSelective
.
addElement
(
XmlElementGeneratorTools
.
generateSetsSelective
(
introspectedTable
.
get
All
Columns
(),
null
,
false
));
eleUpsertSelective
.
addElement
(
XmlElementGeneratorTools
.
generateSetsSelective
(
introspectedTable
.
get
NonBLOB
Columns
(),
null
,
false
));
document
.
getRootElement
().
addElement
(
eleUpsertSelective
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加upsertSelective实现方法。"
);
...
...
@@ -209,8 +400,8 @@ public class UpsertPlugin extends BasePlugin {
// insert
eleUpsertByExample
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
eleUpsertByExample
.
addElement
(
XmlElementGeneratorTools
.
generateKeys
(
introspectedTable
.
get
All
Columns
()));
this
.
generateExistsClause
(
introspectedTable
,
eleUpsertByExample
,
false
);
eleUpsertByExample
.
addElement
(
XmlElementGeneratorTools
.
generateKeys
(
introspectedTable
.
get
NonBLOB
Columns
()));
this
.
generateExistsClause
(
introspectedTable
,
eleUpsertByExample
,
false
,
false
);
// multiQueries
eleUpsertByExample
.
addElement
(
new
TextElement
(
";"
));
...
...
@@ -218,7 +409,7 @@ public class UpsertPlugin extends BasePlugin {
// update
eleUpsertByExample
.
addElement
(
new
TextElement
(
"update "
+
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
()));
eleUpsertByExample
.
addElement
(
new
TextElement
(
"set"
));
eleUpsertByExample
.
addElement
(
XmlElementGeneratorTools
.
generateSets
(
ListUtilities
.
removeIdentityAndGeneratedAlwaysColumns
(
introspectedTable
.
get
All
Columns
()),
"record."
));
eleUpsertByExample
.
addElement
(
XmlElementGeneratorTools
.
generateSets
(
ListUtilities
.
removeIdentityAndGeneratedAlwaysColumns
(
introspectedTable
.
get
NonBLOB
Columns
()),
"record."
));
// update where
eleUpsertByExample
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
...
...
@@ -238,8 +429,8 @@ public class UpsertPlugin extends BasePlugin {
// insert
eleUpsertByExampleSelective
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
eleUpsertByExampleSelective
.
addElement
(
XmlElementGeneratorTools
.
generateKeysSelective
(
introspectedTable
.
get
All
Columns
(),
"record."
));
this
.
generateExistsClause
(
introspectedTable
,
eleUpsertByExampleSelective
,
true
);
eleUpsertByExampleSelective
.
addElement
(
XmlElementGeneratorTools
.
generateKeysSelective
(
introspectedTable
.
get
NonBLOB
Columns
(),
"record."
));
this
.
generateExistsClause
(
introspectedTable
,
eleUpsertByExampleSelective
,
true
,
false
);
// multiQueries
eleUpsertByExampleSelective
.
addElement
(
new
TextElement
(
";"
));
...
...
@@ -247,7 +438,7 @@ public class UpsertPlugin extends BasePlugin {
// update
eleUpsertByExampleSelective
.
addElement
(
new
TextElement
(
"update "
+
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
()));
eleUpsertByExampleSelective
.
addElement
(
new
TextElement
(
"set"
));
eleUpsertByExampleSelective
.
addElement
(
XmlElementGeneratorTools
.
generateSetsSelective
(
ListUtilities
.
removeIdentityAndGeneratedAlwaysColumns
(
introspectedTable
.
get
All
Columns
()),
"record."
));
eleUpsertByExampleSelective
.
addElement
(
XmlElementGeneratorTools
.
generateSetsSelective
(
ListUtilities
.
removeIdentityAndGeneratedAlwaysColumns
(
introspectedTable
.
get
NonBLOB
Columns
()),
"record."
));
// update where
eleUpsertByExampleSelective
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
...
...
@@ -255,8 +446,6 @@ public class UpsertPlugin extends BasePlugin {
document
.
getRootElement
().
addElement
(
eleUpsertByExampleSelective
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加upsertSelective实现方法。"
);
}
return
true
;
}
/**
...
...
@@ -265,13 +454,17 @@ public class UpsertPlugin extends BasePlugin {
* @param introspectedTable
* @param element
* @param selective
* @param withBLOBs
*/
private
void
generateExistsClause
(
IntrospectedTable
introspectedTable
,
XmlElement
element
,
boolean
selective
){
private
void
generateExistsClause
(
IntrospectedTable
introspectedTable
,
XmlElement
element
,
boolean
selective
,
boolean
withBLOBs
){
List
<
IntrospectedColumn
>
columns
=
withBLOBs
?
introspectedTable
.
getAllColumns
()
:
introspectedTable
.
getNonBLOBColumns
();
logger
.
warn
(
withBLOBs
+
" ::::: "
+
columns
.
size
());
element
.
addElement
(
new
TextElement
(
"select"
));
if
(
selective
){
element
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
introspectedTable
.
getAllColumns
()
,
"record."
,
false
));
element
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
columns
,
"record."
,
false
));
}
else
{
element
.
addElement
(
XmlElementGeneratorTools
.
generateValues
(
introspectedTable
.
getAllColumns
()
,
"record."
,
false
));
element
.
addElement
(
XmlElementGeneratorTools
.
generateValues
(
columns
,
"record."
,
false
));
}
element
.
addElement
(
new
TextElement
(
"from dual where not exists"
));
element
.
addElement
(
new
TextElement
(
"("
));
...
...
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