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
50d1af8b
Commit
50d1af8b
authored
May 04, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
乐观锁插件测试用例
parent
6f0d9225
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
149 additions
and
3 deletions
+149
-3
src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
...fsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
+1
-1
src/test/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPluginTest.java
...mybatis/generator/plugins/OptimisticLockerPluginTest.java
+148
-2
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
View file @
50d1af8b
...
@@ -610,7 +610,7 @@ public class OptimisticLockerPlugin extends BasePlugin {
...
@@ -610,7 +610,7 @@ public class OptimisticLockerPlugin extends BasePlugin {
updateEle
.
addElement
(
ifElement
);
updateEle
.
addElement
(
ifElement
);
}
else
{
}
else
{
this
.
replacePrimaryKeyXmlElement
(
introspectedTable
,
updateEle
,
id
,
true
);
updateEle
=
this
.
replacePrimaryKeyXmlElement
(
introspectedTable
,
updateEle
,
id
,
true
);
}
}
return
updateEle
;
return
updateEle
;
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPluginTest.java
View file @
50d1af8b
...
@@ -179,10 +179,10 @@ public class OptimisticLockerPluginTest {
...
@@ -179,10 +179,10 @@ public class OptimisticLockerPluginTest {
}
}
/**
/**
* 测试 updateWithVersionByExample
WithoutBLOBs
* 测试 updateWithVersionByExample
*/
*/
@Test
@Test
public
void
testUpdateWithVersionByExample
WithoutBLOBs
()
throws
Exception
{
public
void
testUpdateWithVersionByExample
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OptimisticLockerPlugin/mybatis-generator.xml"
);
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OptimisticLockerPlugin/mybatis-generator.xml"
);
// 测试不带or的更新
// 测试不带or的更新
tool
.
generate
(()
->
DBHelper
.
createDB
(
"scripts/OptimisticLockerPlugin/init.sql"
),
new
AbstractShellCallback
()
{
tool
.
generate
(()
->
DBHelper
.
createDB
(
"scripts/OptimisticLockerPlugin/init.sql"
),
new
AbstractShellCallback
()
{
...
@@ -328,4 +328,150 @@ public class OptimisticLockerPluginTest {
...
@@ -328,4 +328,150 @@ public class OptimisticLockerPluginTest {
}
}
});
});
}
}
/**
* 测试 updateWithVersionByPrimaryKeySelective
*/
@Test
public
void
testUpdateWithVersionByPrimaryKeySelective
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OptimisticLockerPlugin/mybatis-generator.xml"
);
tool
.
generate
(()
->
DBHelper
.
createDB
(
"scripts/OptimisticLockerPlugin/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
tb
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb"
);
tb
.
set
(
"id"
,
1L
);
tb
.
set
(
"incF1"
,
152L
);
// 这个不会在sql才为正常
tb
.
set
(
"incF2"
,
10L
);
tb
.
set
(
"incF3"
,
5L
);
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"updateWithVersionByPrimaryKeySelective"
,
100L
,
tb
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb SET inc_f1 = inc_f1 + 1, inc_f2 = 10, inc_f3 = 5 where inc_f1 = 100 and id = 1"
);
// 执行一次,因为版本号100不存在所以应该返回0
Object
result
=
tbMapper
.
invoke
(
"updateWithVersionByPrimaryKeySelective"
,
100L
,
tb
.
getObject
());
Assert
.
assertEquals
(
result
,
0
);
// id = 1 的版本号应该是0
result
=
tbMapper
.
invoke
(
"updateWithVersionByPrimaryKeySelective"
,
0L
,
tb
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
// 执行完成后版本号应该加1
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select * from tb where id = 1"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
1
);
}
});
}
/**
* 测试 updateWithVersionByPrimaryKey
*/
@Test
public
void
testUpdateWithVersionByPrimaryKey
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OptimisticLockerPlugin/mybatis-generator.xml"
);
tool
.
generate
(()
->
DBHelper
.
createDB
(
"scripts/OptimisticLockerPlugin/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
tb
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb"
);
tb
.
set
(
"id"
,
1L
);
tb
.
set
(
"incF1"
,
152L
);
// 这个不会在sql才为正常
tb
.
set
(
"incF2"
,
10L
);
tb
.
set
(
"incF3"
,
5L
);
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"updateWithVersionByPrimaryKey"
,
100L
,
tb
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb set inc_f1 = inc_f1 + 1, field1 = 'null', inc_f2 = 10, inc_f3 = 5 where inc_f1 = 100 and id = 1"
);
// 执行一次,因为版本号100不存在所以应该返回0
Object
result
=
tbMapper
.
invoke
(
"updateWithVersionByPrimaryKey"
,
100L
,
tb
.
getObject
());
Assert
.
assertEquals
(
result
,
0
);
// id = 1 的版本号应该是0
result
=
tbMapper
.
invoke
(
"updateWithVersionByPrimaryKey"
,
0L
,
tb
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
// 执行完成后版本号应该加1
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select * from tb where id = 1"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
1
);
}
});
}
/**
* 测试 updateWithVersionByPrimaryKeyWithBLOBs
*/
@Test
public
void
testUpdateWithVersionByPrimaryKeyWithBLOBs
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OptimisticLockerPlugin/mybatis-generator.xml"
);
// 测试执行withoutBLOBs
tool
.
generate
(()
->
DBHelper
.
createDB
(
"scripts/OptimisticLockerPlugin/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"
)));
ObjectUtil
tbBlobs
=
new
ObjectUtil
(
loader
,
packagz
+
".TbBlobs"
);
tbBlobs
.
set
(
"id"
,
1L
);
tbBlobs
.
set
(
"incF1"
,
152L
);
// 这个不会在sql才为正常
tbBlobs
.
set
(
"incF2"
,
10L
);
tbBlobs
.
set
(
"incF3"
,
5L
);
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbBlobsMapper
.
getObject
(),
"updateWithVersionByPrimaryKey"
,
100L
,
tbBlobs
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb_blobs set inc_f1 = inc_f1 + 1, field1 = 'null', inc_f2 = 10, inc_f3 = 5 where inc_f1 = 100 and id = 1"
);
// 执行一次,因为版本号100不存在所以应该返回0
Object
result
=
tbBlobsMapper
.
invoke
(
"updateWithVersionByPrimaryKey"
,
100L
,
tbBlobs
.
getObject
());
Assert
.
assertEquals
(
result
,
0
);
// id = 1 的版本号应该是1
result
=
tbBlobsMapper
.
invoke
(
"updateWithVersionByPrimaryKey"
,
1L
,
tbBlobs
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
// 执行完成后版本号应该加1
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select * from tb_blobs where id = 1"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
2
);
}
});
// 测试执行withBLOBs
tool
.
generate
(()
->
DBHelper
.
createDB
(
"scripts/OptimisticLockerPlugin/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"
)));
ObjectUtil
tbBlobsWithBLOBs
=
new
ObjectUtil
(
loader
,
packagz
+
".TbBlobsWithBLOBs"
);
tbBlobsWithBLOBs
.
set
(
"id"
,
1L
);
tbBlobsWithBLOBs
.
set
(
"incF1"
,
152L
);
// 这个不会在sql才为正常
tbBlobsWithBLOBs
.
set
(
"incF2"
,
10L
);
tbBlobsWithBLOBs
.
set
(
"incF3"
,
5L
);
tbBlobsWithBLOBs
.
set
(
"field1"
,
"ts1"
);
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbBlobsMapper
.
getObject
(),
"updateWithVersionByPrimaryKeyWithBLOBs"
,
100L
,
tbBlobsWithBLOBs
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb_blobs set inc_f1 = inc_f1 + 1, field1 = 'ts1', inc_f2 = 10, inc_f3 = 5, field2 = 'null', field3 = 'null' where inc_f1 = 100 and id = 1"
);
// 执行一次,因为版本号100不存在所以应该返回0
Object
result
=
tbBlobsMapper
.
invoke
(
"updateWithVersionByPrimaryKeyWithBLOBs"
,
100L
,
tbBlobsWithBLOBs
.
getObject
());
Assert
.
assertEquals
(
result
,
0
);
// id = 1 的版本号应该是1
result
=
tbBlobsMapper
.
invoke
(
"updateWithVersionByPrimaryKeyWithBLOBs"
,
1L
,
tbBlobsWithBLOBs
.
getObject
());
Assert
.
assertEquals
(
result
,
1
);
// 执行完成后版本号应该加1
ResultSet
rs
=
DBHelper
.
execute
(
sqlSession
.
getConnection
(),
"select * from tb_blobs where id = 1"
);
rs
.
first
();
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f1"
),
2
);
Assert
.
assertEquals
(
rs
.
getString
(
"field1"
),
"ts1"
);
}
});
}
}
}
\ 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