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
a87dcf9a
Commit
a87dcf9a
authored
May 17, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
UpsertPlugin 插件增加批量操作方法
parent
f6b040a4
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
407 additions
and
7 deletions
+407
-7
src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
...ava/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
+209
-4
src/test/java/com/itfsw/mybatis/generator/plugins/BatchInsertPluginTest.java
...tfsw/mybatis/generator/plugins/BatchInsertPluginTest.java
+2
-2
src/test/java/com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java
...com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java
+136
-1
src/test/resources/scripts/UpsertPlugin/init.sql
src/test/resources/scripts/UpsertPlugin/init.sql
+3
-0
src/test/resources/scripts/UpsertPlugin/mybatis-generator-with-allowBatchUpsert.xml
.../UpsertPlugin/mybatis-generator-with-allowBatchUpsert.xml
+57
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
View file @
a87dcf9a
...
...
@@ -23,9 +23,11 @@ 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.ListUtilities
;
import
org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities
;
import
org.mybatis.generator.internal.util.StringUtility
;
import
java.util.Arrays
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Properties
;
...
...
@@ -42,13 +44,19 @@ public class UpsertPlugin extends BasePlugin {
public
static
final
String
METHOD_UPSERT_WITH_BLOBS
=
"upsertWithBLOBs"
;
// 方法名
public
static
final
String
METHOD_UPSERT_SELECTIVE
=
"upsertSelective"
;
// 方法名
public
static
final
String
METHOD_BATCH_UPSERT
=
"batchUpsert"
;
// 方法名
public
static
final
String
METHOD_BATCH_UPSERT_WITH_BLOBS
=
"batchUpsertWithBLOBs"
;
// 方法名
public
static
final
String
METHOD_BATCH_UPSERT_SELECTIVE
=
"batchUpsertSelective"
;
// 方法名
public
static
final
String
METHOD_UPSERT_BY_EXAMPLE
=
"upsertByExample"
;
// 方法名
public
static
final
String
METHOD_UPSERT_BY_EXAMPLE_WITH_BLOBS
=
"upsertByExampleWithBLOBs"
;
// 方法名
public
static
final
String
METHOD_UPSERT_BY_EXAMPLE_SELECTIVE
=
"upsertByExampleSelective"
;
// 方法名
public
static
final
String
PRO_ALLOW_MULTI_QUERIES
=
"allowMultiQueries"
;
// property allowMultiQueries
public
static
final
String
PRO_ALLOW_BATCH_UPSERT
=
"allowBatchUpsert"
;
// property allowBatch
private
boolean
allowMultiQueries
=
false
;
// 是否允许多sql提交
private
boolean
allowBatchUpsert
=
false
;
/**
* {@inheritDoc}
...
...
@@ -71,6 +79,16 @@ public class UpsertPlugin extends BasePlugin {
warnings
.
add
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"插件您开启了allowMultiQueries支持,注意在jdbc url 配置中增加“allowMultiQueries=true”支持(不怎么建议使用该功能,开启多sql提交会增加sql注入的风险,请确保你所有sql都使用MyBatis书写,请不要使用statement进行sql提交)!"
);
}
// 是否允许批量
String
allowBatchUpsert
=
properties
.
getProperty
(
PRO_ALLOW_BATCH_UPSERT
);
this
.
allowBatchUpsert
=
allowBatchUpsert
==
null
?
false
:
StringUtility
.
isTrue
(
allowBatchUpsert
);
// 批量时依赖插件 ModelColumnPlugin
if
(
this
.
allowBatchUpsert
&&
!
PluginTools
.
checkDependencyPlugin
(
context
,
ModelColumnPlugin
.
class
))
{
// 提示用户注意信息
warnings
.
add
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"插件您开启了allowBatchUpsert支持,请配置依赖的插件"
+
ModelColumnPlugin
.
class
.
getTypeName
()
+
"!"
);
}
return
super
.
validate
(
warnings
);
}
...
...
@@ -108,7 +126,7 @@ public class UpsertPlugin extends BasePlugin {
commentGenerator
.
addGeneralMethodComment
(
mUpsertWithBLOBs
,
introspectedTable
);
// interface 增加方法
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
mUpsertWithBLOBs
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加upsert方法。"
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加upsert
WithBLOBs
方法。"
);
}
// ====================================== upsertSelective ======================================
...
...
@@ -155,7 +173,7 @@ public class UpsertPlugin extends BasePlugin {
commentGenerator
.
addGeneralMethodComment
(
mUpsertByExampleWithBLOBs
,
introspectedTable
);
// interface 增加方法
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
mUpsertByExampleWithBLOBs
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加upsertByExample方法。"
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加upsertByExample
WithBLOBs
方法。"
);
}
// ====================================== upsertByExampleSelective ======================================
...
...
@@ -175,6 +193,55 @@ public class UpsertPlugin extends BasePlugin {
}
}
if
(
this
.
allowBatchUpsert
)
{
// ====================================== batchUpsert ======================================
FullyQualifiedJavaType
returnType
=
FullyQualifiedJavaType
.
getNewListInstance
();
returnType
.
addTypeArgument
(
JavaElementGeneratorTools
.
getModelTypeWithoutBLOBs
(
introspectedTable
));
Method
mBatchUpsert
=
JavaElementGeneratorTools
.
generateMethod
(
METHOD_BATCH_UPSERT
,
JavaVisibility
.
DEFAULT
,
FullyQualifiedJavaType
.
getIntInstance
(),
new
Parameter
(
returnType
,
"list"
,
"@Param(\"list\")"
)
);
commentGenerator
.
addGeneralMethodComment
(
mBatchUpsert
,
introspectedTable
);
// interface 增加方法
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
mBatchUpsert
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加batchUpsert方法。"
);
// ====================================== batchUpsertWithBLOBs ======================================
// !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准
if
(
introspectedTable
.
hasBLOBColumns
())
{
returnType
=
FullyQualifiedJavaType
.
getNewListInstance
();
returnType
.
addTypeArgument
(
JavaElementGeneratorTools
.
getModelTypeWithBLOBs
(
introspectedTable
));
Method
mBatchUpsertWithBLOBs
=
JavaElementGeneratorTools
.
generateMethod
(
METHOD_BATCH_UPSERT_WITH_BLOBS
,
JavaVisibility
.
DEFAULT
,
FullyQualifiedJavaType
.
getIntInstance
(),
new
Parameter
(
returnType
,
"list"
,
"@Param(\"list\")"
)
);
commentGenerator
.
addGeneralMethodComment
(
mBatchUpsertWithBLOBs
,
introspectedTable
);
// interface 增加方法
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
mBatchUpsertWithBLOBs
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加batchUpsertWithBLOBs方法。"
);
}
// ====================================== batchUpsertSelective ======================================
returnType
=
FullyQualifiedJavaType
.
getNewListInstance
();
returnType
.
addTypeArgument
(
fullFieldModel
);
FullyQualifiedJavaType
selectiveType
=
new
FullyQualifiedJavaType
(
introspectedTable
.
getRules
().
calculateAllFieldsClass
().
getShortName
()
+
"."
+
ModelColumnPlugin
.
ENUM_NAME
);
Method
mBatchUpsertSelective
=
JavaElementGeneratorTools
.
generateMethod
(
METHOD_BATCH_UPSERT_SELECTIVE
,
JavaVisibility
.
DEFAULT
,
FullyQualifiedJavaType
.
getIntInstance
(),
new
Parameter
(
returnType
,
"list"
,
"@Param(\"list\")"
),
new
Parameter
(
selectiveType
,
"selective"
,
"@Param(\"selective\")"
,
true
)
);
commentGenerator
.
addGeneralMethodComment
(
mBatchUpsertSelective
,
introspectedTable
);
// interface 增加方法
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
mBatchUpsertSelective
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加batchUpsertSelective方法。"
);
}
return
super
.
clientGenerated
(
interfaze
,
topLevelClass
,
introspectedTable
);
}
...
...
@@ -193,9 +260,147 @@ public class UpsertPlugin extends BasePlugin {
this
.
generateXmlElement
(
document
,
introspectedTable
,
true
);
}
if
(
this
.
allowBatchUpsert
)
{
this
.
generateBatchXmlElementSelective
(
document
,
introspectedTable
);
this
.
generateBatchXmlElement
(
document
,
introspectedTable
,
false
);
if
(
introspectedTable
.
hasBLOBColumns
())
{
this
.
generateBatchXmlElement
(
document
,
introspectedTable
,
true
);
}
}
return
super
.
sqlMapDocumentGenerated
(
document
,
introspectedTable
);
}
/**
* 批量
* @param document
* @param introspectedTable
*/
private
void
generateBatchXmlElementSelective
(
Document
document
,
IntrospectedTable
introspectedTable
)
{
XmlElement
insertEle
=
new
XmlElement
(
"insert"
);
insertEle
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_BATCH_UPSERT_SELECTIVE
));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
commentGenerator
.
addComment
(
insertEle
);
// 参数类型
insertEle
.
addAttribute
(
new
Attribute
(
"parameterType"
,
"map"
));
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools
.
useGeneratedKeys
(
insertEle
,
introspectedTable
);
// insert
insertEle
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()
+
" ("
));
XmlElement
foreachInsertColumns
=
new
XmlElement
(
"foreach"
);
foreachInsertColumns
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
foreachInsertColumns
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
foreachInsertColumns
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
foreachInsertColumns
.
addElement
(
new
TextElement
(
"${column.value}"
));
insertEle
.
addElement
(
foreachInsertColumns
);
insertEle
.
addElement
(
new
TextElement
(
")"
));
// values
insertEle
.
addElement
(
new
TextElement
(
"values"
));
// foreach values
XmlElement
foreachValues
=
new
XmlElement
(
"foreach"
);
foreachValues
.
addAttribute
(
new
Attribute
(
"collection"
,
"list"
));
foreachValues
.
addAttribute
(
new
Attribute
(
"item"
,
"item"
));
foreachValues
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
foreachValues
.
addElement
(
new
TextElement
(
"("
));
// foreach 所有插入的列,比较是否存在
XmlElement
foreachInsertColumnsCheck
=
new
XmlElement
(
"foreach"
);
foreachInsertColumnsCheck
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
foreachInsertColumnsCheck
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
foreachInsertColumnsCheck
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
// 所有表字段
List
<
IntrospectedColumn
>
columns
=
ListUtilities
.
removeIdentityAndGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
());
List
<
IntrospectedColumn
>
columns1
=
ListUtilities
.
removeIdentityAndGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
());
for
(
int
i
=
0
;
i
<
columns1
.
size
();
i
++)
{
IntrospectedColumn
introspectedColumn
=
columns
.
get
(
i
);
XmlElement
check
=
new
XmlElement
(
"if"
);
check
.
addAttribute
(
new
Attribute
(
"test"
,
"'"
+
introspectedColumn
.
getActualColumnName
()
+
"' == column.value"
));
check
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
"item."
)));
foreachInsertColumnsCheck
.
addElement
(
check
);
}
foreachValues
.
addElement
(
foreachInsertColumnsCheck
);
foreachValues
.
addElement
(
new
TextElement
(
")"
));
insertEle
.
addElement
(
foreachValues
);
insertEle
.
addElement
(
new
TextElement
(
"on duplicate key update "
));
// set
XmlElement
foreachSetColumns
=
new
XmlElement
(
"foreach"
);
insertEle
.
addElement
(
foreachSetColumns
);
foreachSetColumns
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
foreachSetColumns
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
foreachSetColumns
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
foreachSetColumns
.
addElement
(
new
TextElement
(
"${column.value} = values(${column.value})"
));
document
.
getRootElement
().
addElement
(
insertEle
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加batchUpsertSelective实现方法。"
);
}
/**
* 批量
* @param document
* @param introspectedTable
* @param withBLOBs
*/
private
void
generateBatchXmlElement
(
Document
document
,
IntrospectedTable
introspectedTable
,
boolean
withBLOBs
)
{
List
<
IntrospectedColumn
>
columns
=
ListUtilities
.
removeGeneratedAlwaysColumns
(
withBLOBs
?
introspectedTable
.
getAllColumns
()
:
introspectedTable
.
getNonBLOBColumns
());
XmlElement
insertEle
=
new
XmlElement
(
"insert"
);
insertEle
.
addAttribute
(
new
Attribute
(
"id"
,
withBLOBs
?
METHOD_BATCH_UPSERT_WITH_BLOBS
:
METHOD_BATCH_UPSERT
));
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
commentGenerator
.
addComment
(
insertEle
);
// 参数类型
insertEle
.
addAttribute
(
new
Attribute
(
"parameterType"
,
"map"
));
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools
.
useGeneratedKeys
(
insertEle
,
introspectedTable
);
// insert
insertEle
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
for
(
Element
element
:
XmlElementGeneratorTools
.
generateUpsertKeys
(
columns
,
null
))
{
insertEle
.
addElement
(
element
);
}
insertEle
.
addElement
(
new
TextElement
(
"values"
));
// values
XmlElement
foreachEle
=
new
XmlElement
(
"foreach"
);
insertEle
.
addElement
(
foreachEle
);
foreachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"list"
));
foreachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"item"
));
foreachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
for
(
Element
element
:
XmlElementGeneratorTools
.
generateUpsertValues
(
columns
,
"item."
,
true
))
{
foreachEle
.
addElement
(
element
);
}
insertEle
.
addElement
(
new
TextElement
(
"on duplicate key update "
));
// set
Iterator
<
IntrospectedColumn
>
columnIterator
=
columns
.
iterator
();
while
(
columnIterator
.
hasNext
())
{
IntrospectedColumn
column
=
columnIterator
.
next
();
insertEle
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
column
)
+
" = values("
+
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
column
)
+
")"
+
(
columnIterator
.
hasNext
()
?
","
:
""
)
));
}
document
.
getRootElement
().
addElement
(
insertEle
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加"
+
(
withBLOBs
?
"batchUpsertWithBLOBs"
:
"batchUpsert"
)
+
"实现方法。"
);
}
/**
* 当Selective情况
* @param document
...
...
@@ -315,7 +520,7 @@ public class UpsertPlugin extends BasePlugin {
}
document
.
getRootElement
().
addElement
(
insertEle
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加
upsert
实现方法。"
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加
"
+
(
withBLOBs
?
"upsertWithBLOBs"
:
"upsert"
)
+
"
实现方法。"
);
if
(
this
.
allowMultiQueries
)
{
// ====================================== upsertByExample ======================================
...
...
@@ -352,7 +557,7 @@ public class UpsertPlugin extends BasePlugin {
this
.
generateExistsClause
(
introspectedTable
,
updateEle
,
XmlElementGeneratorTools
.
generateUpsertValues
(
columns
,
"record."
,
false
));
document
.
getRootElement
().
addElement
(
updateEle
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加
upsertByExample
实现方法。"
);
logger
.
debug
(
"itfsw(存在即更新插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加
"
+
(
withBLOBs
?
"upsertByExampleWithBLOBs"
:
"upsertByExample"
)
+
"
实现方法。"
);
}
}
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/BatchInsertPluginTest.java
View file @
a87dcf9a
...
...
@@ -147,7 +147,7 @@ public class BatchInsertPluginTest {
@Test
public
void
testBatchInsert
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
tool
.
generate
(
()
->
DBHelper
.
resetDB
(
"scripts/BatchInsertPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
// 1. 测试sql
...
...
@@ -184,7 +184,7 @@ public class BatchInsertPluginTest {
@Test
public
void
testBatchInsertSelective
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/BatchInsertPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
tool
.
generate
(
()
->
DBHelper
.
resetDB
(
"scripts/BatchInsertPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
// 1. 测试sql
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/UpsertPluginTest.java
View file @
a87dcf9a
...
...
@@ -25,8 +25,11 @@ import org.mybatis.generator.exception.InvalidConfigurationException;
import
org.mybatis.generator.exception.XMLParserException
;
import
java.io.IOException
;
import
java.lang.reflect.Array
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* ---------------------------------------------------------------------------
...
...
@@ -41,7 +44,7 @@ public class UpsertPluginTest {
* 初始化
*/
@BeforeClass
public
static
void
init
()
throws
Exception
{
public
static
void
init
()
throws
Exception
{
DBHelper
.
createDB
(
"scripts/UpsertPlugin/init.sql"
);
}
...
...
@@ -421,4 +424,136 @@ public class UpsertPluginTest {
}
});
}
/**
* 测试批量batchUpsert
*/
@Test
public
void
testBatchUpsert
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/UpsertPlugin/mybatis-generator-with-allowBatchUpsert.xml"
);
tool
.
generate
(()
->
DBHelper
.
resetDB
(
"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"
)));
List
<
Object
>
params
=
new
ArrayList
<>();
params
.
add
(
new
ObjectUtil
(
loader
,
packagz
+
".Tb"
).
set
(
"id"
,
1L
).
set
(
"field1"
,
"ts1"
).
getObject
());
params
.
add
(
new
ObjectUtil
(
loader
,
packagz
+
".Tb"
).
set
(
"id"
,
6L
).
set
(
"field1"
,
"ts2"
).
set
(
"field2"
,
1
).
getObject
());
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"batchUpsert"
,
params
);
Assert
.
assertEquals
(
sql
,
"insert into tb (id, field1, field2) values (1, 'ts1', null ) , (6, 'ts2', 1 ) on duplicate key update id = values(id), field1 = values(field1), field2 = values(field2)"
);
// 2. 执行sql
Object
count
=
tbMapper
.
invoke
(
"batchUpsert"
,
params
);
Assert
.
assertEquals
(
count
,
3
);
// 验证
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
,
"select * from tb where id = 1"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getString
(
"field1"
),
"ts1"
);
rs
=
DBHelper
.
execute
(
sqlSession
,
"select * from tb where id = 6"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getString
(
"field1"
),
"ts2"
);
}
});
}
/**
* 测试批量batchUpsertWithBLOBs
*/
@Test
public
void
testBatchUpsertWithBLOBs
()
throws
Exception
{
// 1. batchUpsert
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/UpsertPlugin/mybatis-generator-with-allowBatchUpsert.xml"
);
tool
.
generate
(()
->
DBHelper
.
resetDB
(
"scripts/UpsertPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
ObjectUtil
tbBlobsMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbBlobsMapper"
)));
List
<
Object
>
params
=
new
ArrayList
<>();
params
.
add
(
new
ObjectUtil
(
loader
,
packagz
+
".TbBlobs"
).
set
(
"id"
,
1L
).
set
(
"field1"
,
"ts1"
).
getObject
());
params
.
add
(
new
ObjectUtil
(
loader
,
packagz
+
".TbBlobs"
).
set
(
"id"
,
6L
).
set
(
"field1"
,
"ts2"
).
getObject
());
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbBlobsMapper
.
getObject
(),
"batchUpsert"
,
params
);
Assert
.
assertEquals
(
sql
,
"insert into tb_blobs (id, field1) values (1, 'ts1') , (6, 'ts2') on duplicate key update id = values(id), field1 = values(field1)"
);
// 2. 执行sql
Object
count
=
tbBlobsMapper
.
invoke
(
"batchUpsert"
,
params
);
Assert
.
assertEquals
(
count
,
3
);
// 验证
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
,
"select * from tb_blobs where id = 1"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getString
(
"field1"
),
"ts1"
);
rs
=
DBHelper
.
execute
(
sqlSession
,
"select * from tb_blobs where id = 6"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getString
(
"field1"
),
"ts2"
);
}
});
// 2. batchUpsertWithBLOBs
tool
.
generate
(()
->
DBHelper
.
resetDB
(
"scripts/UpsertPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
ObjectUtil
tbBlobsMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbBlobsMapper"
)));
List
<
Object
>
params
=
new
ArrayList
<>();
params
.
add
(
new
ObjectUtil
(
loader
,
packagz
+
".TbBlobsWithBLOBs"
).
set
(
"id"
,
1L
).
set
(
"field1"
,
"ts1"
).
set
(
"field3"
,
"ff1"
).
getObject
());
params
.
add
(
new
ObjectUtil
(
loader
,
packagz
+
".TbBlobsWithBLOBs"
).
set
(
"id"
,
6L
).
set
(
"field1"
,
"ts2"
).
set
(
"field3"
,
"ff2"
).
getObject
());
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbBlobsMapper
.
getObject
(),
"batchUpsertWithBLOBs"
,
params
);
Assert
.
assertEquals
(
sql
,
"insert into tb_blobs (id, field1, field2, field3) values (1, 'ts1', 'null', 'ff1') , (6, 'ts2', 'null', 'ff2') on duplicate key update id = values(id), field1 = values(field1), field2 = values(field2), field3 = values(field3)"
);
// 2. 执行sql
Object
count
=
tbBlobsMapper
.
invoke
(
"batchUpsertWithBLOBs"
,
params
);
Assert
.
assertEquals
(
count
,
3
);
// 验证
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
,
"select * from tb_blobs where id = 1"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getString
(
"field1"
),
"ts1"
);
Assert
.
assertEquals
(
rs
.
getString
(
"field3"
),
"ff1"
);
rs
=
DBHelper
.
execute
(
sqlSession
,
"select * from tb_blobs where id = 6"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getString
(
"field1"
),
"ts2"
);
Assert
.
assertEquals
(
rs
.
getString
(
"field3"
),
"ff2"
);
}
});
}
/**
* 测试批量batchUpsertSelective
*/
@Test
public
void
testBatchUpsertSelective
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/UpsertPlugin/mybatis-generator-with-allowBatchUpsert.xml"
);
tool
.
generate
(()
->
DBHelper
.
resetDB
(
"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"
)));
List
<
Object
>
params
=
new
ArrayList
<>();
params
.
add
(
new
ObjectUtil
(
loader
,
packagz
+
".Tb"
).
set
(
"id"
,
1L
).
set
(
"field1"
,
"ts1"
).
getObject
());
params
.
add
(
new
ObjectUtil
(
loader
,
packagz
+
".Tb"
).
set
(
"id"
,
6L
).
set
(
"field1"
,
"ts2"
).
set
(
"field2"
,
1
).
getObject
());
ObjectUtil
columnId
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#id"
);
ObjectUtil
columnField1
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#field1"
);
Object
columns
=
Array
.
newInstance
(
columnId
.
getCls
(),
2
);
Array
.
set
(
columns
,
0
,
columnId
.
getObject
());
Array
.
set
(
columns
,
1
,
columnField1
.
getObject
());
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"batchUpsertSelective"
,
params
,
columns
);
Assert
.
assertEquals
(
sql
,
"insert into tb ( id , field1 ) values ( 1 , 'ts1' ) , ( 6 , 'ts2' ) on duplicate key update id = values(id) , field1 = values(field1)"
);
// 2. 执行sql
Object
count
=
tbMapper
.
invoke
(
"batchUpsertSelective"
,
params
,
columns
);
Assert
.
assertEquals
(
count
,
3
);
// 验证
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
,
"select * from tb where id = 1"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getString
(
"field1"
),
"ts1"
);
rs
=
DBHelper
.
execute
(
sqlSession
,
"select * from tb where id = 6"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getString
(
"field1"
),
"ts2"
);
Assert
.
assertNull
(
rs
.
getString
(
"field2"
));
}
});
}
}
src/test/resources/scripts/UpsertPlugin/init.sql
View file @
a87dcf9a
...
...
@@ -48,6 +48,9 @@ CREATE TABLE `tb_blobs` (
-- ----------------------------
-- Records of tb_blobs
-- ----------------------------
INSERT
INTO
`tb_blobs`
VALUES
(
'1'
,
'fd1'
,
null
,
'KK1'
);
INSERT
INTO
`tb_blobs`
VALUES
(
'2'
,
null
,
'2'
,
null
);
INSERT
INTO
`tb_blobs`
VALUES
(
'3'
,
'fd3'
,
'3'
,
'KK3'
);
-- ----------------------------
-- Table structure for tb_keys
...
...
src/test/resources/scripts/UpsertPlugin/mybatis-generator-with-allowBatchUpsert.xml
0 → 100644
View file @
a87dcf9a
<?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.UpsertPlugin"
>
<property
name=
"allowBatchUpsert"
value=
"true"
/>
</plugin>
<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"
/>
<table
tableName=
"tb_keys"
/>
<table
tableName=
"tb_single_blob"
/>
<table
tableName=
"tb_blobs"
/>
<table
tableName=
"tb_with_inc_id"
>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
<table
tableName=
"tb_blobs_with_inc_id"
>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</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