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
cbfe10c9
Commit
cbfe10c9
authored
Apr 25, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
补充 UpserPlugin+IncrementsPlugin+SelectiveEnhancedPlugin 整合测试用例
parent
0f16031f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
132 additions
and
23 deletions
+132
-23
src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
...ava/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
+19
-13
src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
...itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
+113
-10
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
View file @
cbfe10c9
...
@@ -257,6 +257,7 @@ public class UpsertPlugin extends BasePlugin {
...
@@ -257,6 +257,7 @@ public class UpsertPlugin extends BasePlugin {
insertTrimElement
.
addAttribute
(
new
Attribute
(
"suffixOverrides"
,
","
));
insertTrimElement
.
addAttribute
(
new
Attribute
(
"suffixOverrides"
,
","
));
insertOtherwiseEle
.
addElement
(
insertTrimElement
);
insertOtherwiseEle
.
addElement
(
insertTrimElement
);
eleUpsertSelective
.
addElement
(
new
TextElement
(
"values"
));
XmlElement
valuesChooseEle
=
new
XmlElement
(
"choose"
);
XmlElement
valuesChooseEle
=
new
XmlElement
(
"choose"
);
eleUpsertSelective
.
addElement
(
valuesChooseEle
);
eleUpsertSelective
.
addElement
(
valuesChooseEle
);
...
@@ -268,21 +269,15 @@ public class UpsertPlugin extends BasePlugin {
...
@@ -268,21 +269,15 @@ public class UpsertPlugin extends BasePlugin {
XmlElement
valuesForeachEle
=
new
XmlElement
(
"foreach"
);
XmlElement
valuesForeachEle
=
new
XmlElement
(
"foreach"
);
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"open"
,
"
values
("
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"open"
,
"("
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"close"
,
")"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"close"
,
")"
));
valuesForeachEle
.
addElement
(
new
TextElement
(
"#{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
valuesForeachEle
.
addElement
(
new
TextElement
(
"#{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
valuesWhenEle
.
addElement
(
valuesForeachEle
);
valuesWhenEle
.
addElement
(
valuesForeachEle
);
XmlElement
valuesOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
XmlElement
valuesOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
valuesOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
columns
,
"record."
));
valuesChooseEle
.
addElement
(
valuesOtherwiseEle
);
valuesChooseEle
.
addElement
(
valuesOtherwiseEle
);
valuesOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
columns
,
"record."
));
XmlElement
valuesTrimElement
=
new
XmlElement
(
"trim"
);
valuesTrimElement
.
addAttribute
(
new
Attribute
(
"prefix"
,
"values ("
));
valuesTrimElement
.
addAttribute
(
new
Attribute
(
"suffix"
,
")"
));
valuesTrimElement
.
addAttribute
(
new
Attribute
(
"suffixOverrides"
,
","
));
valuesOtherwiseEle
.
addElement
(
valuesTrimElement
);
eleUpsertSelective
.
addElement
(
new
TextElement
(
"on duplicate key update "
));
eleUpsertSelective
.
addElement
(
new
TextElement
(
"on duplicate key update "
));
...
@@ -295,11 +290,17 @@ public class UpsertPlugin extends BasePlugin {
...
@@ -295,11 +290,17 @@ public class UpsertPlugin extends BasePlugin {
setChooseEle
.
addElement
(
setWhenEle
);
setChooseEle
.
addElement
(
setWhenEle
);
XmlElement
setForeachEle
=
new
XmlElement
(
"foreach"
);
XmlElement
setForeachEle
=
new
XmlElement
(
"foreach"
);
setWhenEle
.
addElement
(
setForeachEle
);
setForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
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}}"
));
setForeachEle
.
addElement
(
new
TextElement
(
"${column.value} = #{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
setWhenEle
.
addElement
(
setForeachEle
);
}
XmlElement
setOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
XmlElement
setOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
// set 操作增加增量插件支持
// set 操作增加增量插件支持
...
@@ -334,11 +335,16 @@ public class UpsertPlugin extends BasePlugin {
...
@@ -334,11 +335,16 @@ public class UpsertPlugin extends BasePlugin {
setChooseEle
.
addElement
(
setWhenEle
);
setChooseEle
.
addElement
(
setWhenEle
);
setForeachEle
=
new
XmlElement
(
"foreach"
);
setForeachEle
=
new
XmlElement
(
"foreach"
);
setWhenEle
.
addElement
(
setForeachEle
);
setForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
setForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
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}}"
));
setForeachEle
.
addElement
(
new
TextElement
(
"${column.value} = #{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
setWhenEle
.
addElement
(
setForeachEle
);
}
setOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
setOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
...
@@ -580,7 +586,7 @@ public class UpsertPlugin extends BasePlugin {
...
@@ -580,7 +586,7 @@ public class UpsertPlugin extends BasePlugin {
selectWhenEle
.
addElement
(
valuesForeachEle
);
selectWhenEle
.
addElement
(
valuesForeachEle
);
XmlElement
selectOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
XmlElement
selectOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
selectOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
columns
,
"record."
));
selectOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
columns
,
"record."
,
false
));
chooseEle
.
addElement
(
selectOtherwiseEle
);
chooseEle
.
addElement
(
selectOtherwiseEle
);
XmlElement
valuesTrimElement
=
new
XmlElement
(
"trim"
);
XmlElement
valuesTrimElement
=
new
XmlElement
(
"trim"
);
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/IncrementsPluginTest.java
View file @
cbfe10c9
...
@@ -19,6 +19,7 @@ package com.itfsw.mybatis.generator.plugins;
...
@@ -19,6 +19,7 @@ package com.itfsw.mybatis.generator.plugins;
import
com.itfsw.mybatis.generator.plugins.tools.*
;
import
com.itfsw.mybatis.generator.plugins.tools.*
;
import
org.apache.ibatis.session.SqlSession
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.Assert
;
import
org.junit.Assert
;
import
org.junit.BeforeClass
;
import
org.junit.Test
;
import
org.junit.Test
;
import
java.lang.reflect.Array
;
import
java.lang.reflect.Array
;
...
@@ -36,6 +37,15 @@ import java.util.List;
...
@@ -36,6 +37,15 @@ import java.util.List;
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
*/
*/
public
class
IncrementsPluginTest
{
public
class
IncrementsPluginTest
{
/**
* 初始化
* @throws Exception
*/
@BeforeClass
public
static
void
init
()
throws
Exception
{
DBHelper
.
createDB
(
"scripts/IncrementsPlugin/init.sql"
);
}
/**
/**
* 测试没有配置ModelBuilderPlugin
* 测试没有配置ModelBuilderPlugin
*/
*/
...
@@ -421,12 +431,11 @@ public class IncrementsPluginTest {
...
@@ -421,12 +431,11 @@ public class IncrementsPluginTest {
@Test
@Test
public
void
testWithUpsertAndSelectiveEnhancedPlugin
()
throws
Exception
{
public
void
testWithUpsertAndSelectiveEnhancedPlugin
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/IncrementsPlugin/mybatis-generator-with-upsert-and-selective-enhanced-plugin.xml"
);
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/IncrementsPlugin/mybatis-generator-with-upsert-and-selective-enhanced-plugin.xml"
);
// upsertSelective 基于原生非空判断
tool
.
generate
(()
->
DBHelper
.
createDB
(
"scripts/IncrementsPlugin/init.sql"
),
new
AbstractShellCallback
()
{
tool
.
generate
(()
->
DBHelper
.
createDB
(
"scripts/IncrementsPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
// --------------------------- upsertSelective (非空判断) ---------------------------------
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbBuilder
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder"
);
ObjectUtil
tbBuilder
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder"
);
...
@@ -446,13 +455,57 @@ public class IncrementsPluginTest {
...
@@ -446,13 +455,57 @@ public class IncrementsPluginTest {
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
,
"select * from tb where id = 10"
);
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
,
"select * from tb where id = 10"
);
rs
.
first
();
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
20
);
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
20
);
}
});
// upsertByExampleSelective 基于原生非空判断
tool
.
generate
(()
->
DBHelper
.
createDB
(
"scripts/IncrementsPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
// 1. 测试updateByExampleSelective
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
packagz
+
".TbExample"
);
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andField1EqualTo"
,
"ts123"
);
ObjectUtil
tbBuilder
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder"
);
ObjectUtil
tbBuilderInc
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder$Inc#INC"
);
tbBuilder
.
invoke
(
"id"
,
11L
);
tbBuilder
.
invoke
(
"field1"
,
"ts123"
);
tbBuilder
.
invoke
(
"incF1"
,
100L
,
tbBuilderInc
.
getObject
());
// sql
// 非空判断
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"upsertByExampleSelective"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb set id = 11, field1 = 'ts123', inc_f1 = inc_f1 + 100 WHERE ( field1 = 'ts123' ) ; insert into tb ( id, field1, inc_f1 ) select 11, 'ts123', 100 from dual where not exists ( select 1 from tb WHERE ( field1 = 'ts123' ) )"
);
// 执行
Object
result
=
tbMapper
.
invoke
(
"upsertByExampleSelective"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
(),
Array
.
newInstance
(
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#field1"
).
getCls
(),
0
));
Assert
.
assertEquals
(
result
,
0
);
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select inc_f1 from tb where id = 11"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
100
);
// --------------------------- upsertSelective (指定字段) ---------------------------------
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
tbBuilder
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder"
);
ObjectUtil
tbBuilderDec
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder$Inc#INC"
);
tbBuilderInc
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder$Inc#INC"
);
tbBuilder
.
invoke
(
"incF1"
,
50L
,
tbBuilderDec
.
getObject
());
result
=
tbMapper
.
invoke
(
"upsertByExampleSelective"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
(),
Array
.
newInstance
(
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#field1"
).
getCls
(),
0
));
Assert
.
assertEquals
(
result
,
1
);
// 验证执行结果
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select inc_f1 from tb where id = 11"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
150
);
}
});
// upsertSelective 基于指定字段
tool
.
generate
(()
->
DBHelper
.
createDB
(
"scripts/IncrementsPlugin/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"
)));
ObjectUtil
tbBuilder
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder"
);
ObjectUtil
tbBuilderInc
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder$Inc#INC"
);
tbBuilder
.
invoke
(
"id"
,
20L
);
tbBuilder
.
invoke
(
"id"
,
20L
);
tbBuilder
.
invoke
(
"field1"
,
"ts1"
);
tbBuilder
.
invoke
(
"field1"
,
"ts1"
);
tbBuilder
.
invoke
(
"incF1"
,
20L
,
tbBuilderInc
.
getObject
());
tbBuilder
.
invoke
(
"incF1"
,
20L
,
tbBuilderInc
.
getObject
());
...
@@ -468,18 +521,68 @@ public class IncrementsPluginTest {
...
@@ -468,18 +521,68 @@ public class IncrementsPluginTest {
Array
.
set
(
columns
,
3
,
TbColumnIncF2
.
getObject
());
Array
.
set
(
columns
,
3
,
TbColumnIncF2
.
getObject
());
// sql
// sql
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"upsertSelective"
,
tbBuilder
.
invoke
(
"build"
),
columns
);
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"upsertSelective"
,
tbBuilder
.
invoke
(
"build"
),
columns
);
Assert
.
assertEquals
(
sql
,
"insert into tb ( id , field1 , inc_f1 , inc_f2 ) values ( 20 , 'ts1' , 20 , null ) on duplicate key update id = 20 , field1 = 'ts1' , inc_f1 = inc_f1 + 20 , inc_f2 = null"
);
Assert
.
assertEquals
(
sql
,
"insert into tb ( id , field1 , inc_f1 , inc_f2 ) values ( 20 , 'ts1' , 20 , null ) on duplicate key update id = 20 , field1 = 'ts1' , inc_f1 = inc_f1 + 20 , inc_f2 = null"
);
result
=
tbMapper
.
invoke
(
"upsertSelective"
,
tbBuilder
.
invoke
(
"build"
),
columns
);
Object
result
=
tbMapper
.
invoke
(
"upsertSelective"
,
tbBuilder
.
invoke
(
"build"
),
columns
);
Assert
.
assertEquals
(
result
,
1
);
Assert
.
assertEquals
(
result
,
1
);
// 再次执行触发update
// 再次执行触发update
tbBuilder
.
invoke
(
"incF1"
,
20L
,
tbBuilderInc
.
getObject
());
tbBuilder
.
invoke
(
"incF1"
,
20L
,
tbBuilderInc
.
getObject
());
result
=
tbMapper
.
invoke
(
"upsertSelective"
,
tbBuilder
.
invoke
(
"build"
),
columns
);
result
=
tbMapper
.
invoke
(
"upsertSelective"
,
tbBuilder
.
invoke
(
"build"
),
columns
);
Assert
.
assertEquals
(
result
,
2
);
Assert
.
assertEquals
(
result
,
2
);
rs
=
DBHelper
.
execute
(
sqlSession
,
"select * from tb where id = 20"
);
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
,
"select * from tb where id = 20"
);
rs
.
first
();
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
40
);
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
40
);
}
}
});
});
// upsertByExampleSelective 基于指定字段
tool
.
generate
(()
->
DBHelper
.
createDB
(
"scripts/IncrementsPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
// 1. 测试updateByExampleSelective
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbExample
=
new
ObjectUtil
(
loader
,
packagz
+
".TbExample"
);
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andField1EqualTo"
,
"ts123"
);
ObjectUtil
tbBuilder
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder"
);
ObjectUtil
tbBuilderInc
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder$Inc#DEC"
);
tbBuilder
.
invoke
(
"id"
,
11L
);
tbBuilder
.
invoke
(
"field1"
,
"ts123"
);
tbBuilder
.
invoke
(
"incF1"
,
100L
,
tbBuilderInc
.
getObject
());
ObjectUtil
TbColumnId
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#id"
);
ObjectUtil
TbColumnField1
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#field1"
);
ObjectUtil
TbColumnIncF1
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#incF1"
);
ObjectUtil
TbColumnIncF2
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Column#incF2"
);
Object
columns
=
Array
.
newInstance
(
TbColumnField1
.
getCls
(),
4
);
Array
.
set
(
columns
,
0
,
TbColumnId
.
getObject
());
Array
.
set
(
columns
,
1
,
TbColumnField1
.
getObject
());
Array
.
set
(
columns
,
2
,
TbColumnIncF1
.
getObject
());
Array
.
set
(
columns
,
3
,
TbColumnIncF2
.
getObject
());
// sql
// 非空判断
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"upsertByExampleSelective"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
(),
columns
);
Assert
.
assertEquals
(
sql
,
"update tb set id = 11 , field1 = 'ts123' , inc_f1 = inc_f1 - 100 , inc_f2 = null WHERE ( field1 = 'ts123' ) ; insert into tb ( id , field1 , inc_f1 , inc_f2 ) select 11 , 'ts123' , 100 , null from dual where not exists ( select 1 from tb WHERE ( field1 = 'ts123' ) )"
);
// 执行
Object
result
=
tbMapper
.
invoke
(
"upsertByExampleSelective"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
(),
columns
);
Assert
.
assertEquals
(
result
,
0
);
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select inc_f1 from tb where id = 11"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
100
);
ObjectUtil
tbBuilderDec
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder$Inc#DEC"
);
tbBuilder
.
invoke
(
"incF1"
,
50L
,
tbBuilderDec
.
getObject
());
result
=
tbMapper
.
invoke
(
"upsertByExampleSelective"
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
(),
columns
);
Assert
.
assertEquals
(
result
,
1
);
// 验证执行结果
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select inc_f1 from tb where id = 11"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
50
);
}
});
}
}
}
}
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