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
f9fbeeb3
Commit
f9fbeeb3
authored
May 07, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
乐观锁插件测试用例:同时整合Selective增强插件和增量插件
parent
6d379eb6
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
177 additions
and
0 deletions
+177
-0
src/test/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPluginTest.java
...mybatis/generator/plugins/OptimisticLockerPluginTest.java
+106
-0
src/test/resources/scripts/OptimisticLockerPlugin/mybatis-generator-with-SelectiveEnhancedPlugin-IncrementsPlugin.xml
...nerator-with-SelectiveEnhancedPlugin-IncrementsPlugin.xml
+71
-0
No files found.
src/test/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPluginTest.java
View file @
f9fbeeb3
...
@@ -871,4 +871,110 @@ public class OptimisticLockerPluginTest {
...
@@ -871,4 +871,110 @@ public class OptimisticLockerPluginTest {
}
}
});
});
}
}
/**
* 测试整合SelectiveEnhancedPlugin插件和IncrementsPlugin插件
*/
@Test
public
void
testWithSelectiveEnhancedPluginAndIncrementsPlugin
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OptimisticLockerPlugin/mybatis-generator-with-SelectiveEnhancedPlugin-IncrementsPlugin.xml"
);
// 测试updateWithVersionByExampleSelective
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
tbExample
=
new
ObjectUtil
(
loader
,
packagz
+
".TbExample"
);
ObjectUtil
criteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"createCriteria"
));
criteria
.
invoke
(
"andIdEqualTo"
,
1
l
);
ObjectUtil
tbBuilder
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder"
);
ObjectUtil
tbBuilderInc
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder$Inc#INC"
);
tbBuilder
.
invoke
(
"id"
,
1L
);
tbBuilder
.
invoke
(
"incF1"
,
121
l
,
tbBuilderInc
.
getObject
());
// 这个不会在sql才为正常
tbBuilder
.
invoke
(
"incF2"
,
5
l
,
tbBuilderInc
.
getObject
());
tbBuilder
.
invoke
(
"incF3"
,
10
l
);
// selective
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"
);
// 这个不会在sql才为正常
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
(),
"updateWithVersionByExampleSelective"
,
100L
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb SET inc_f1 = inc_f1 + 1, id = 1, inc_f2 = inc_f2 + 5 , inc_f3 = 10 WHERE inc_f1 = 100 and ( ( id = '1' ) )"
);
// sql(selective增强)
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"updateWithVersionByExampleSelective"
,
100L
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
(),
columns
);
Assert
.
assertEquals
(
sql
,
"update tb SET inc_f1 = inc_f1 + 1, id = 1 , field1 = 'null' , inc_f2 = inc_f2 + 5 WHERE inc_f1 = 100 and ( ( id = '1' ) )"
);
// 执行一次,因为版本号100不存在所以应该返回0(普通非空判断)
Object
result
=
tbMapper
.
invoke
(
"updateWithVersionByExampleSelective"
,
100L
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
(),
null
);
Assert
.
assertEquals
(
result
,
0
);
// id = 1 的版本号应该是0(selective 增强)
result
=
tbMapper
.
invoke
(
"updateWithVersionByExampleSelective"
,
0L
,
tbBuilder
.
invoke
(
"build"
),
tbExample
.
getObject
(),
columns
);
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
);
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f2"
),
7
);
}
});
// 测试updateWithVersionByPrimaryKeySelective
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
tbBuilder
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder"
);
ObjectUtil
tbBuilderInc
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb$Builder$Inc#INC"
);
tbBuilder
.
invoke
(
"id"
,
1L
);
tbBuilder
.
invoke
(
"incF1"
,
121
l
,
tbBuilderInc
.
getObject
());
// 这个不会在sql才为正常
tbBuilder
.
invoke
(
"incF2"
,
5
l
,
tbBuilderInc
.
getObject
());
tbBuilder
.
invoke
(
"incF3"
,
10
l
);
// selective
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"
);
// 这个不会在sql才为正常
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
(),
"updateWithVersionByPrimaryKeySelective"
,
100L
,
tbBuilder
.
invoke
(
"build"
));
Assert
.
assertEquals
(
sql
,
"update tb SET inc_f1 = inc_f1 + 1, inc_f2 = inc_f2 + 5 , inc_f3 = 10 where inc_f1 = 100 and id = 1"
);
// sql(selective增强)
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"updateWithVersionByPrimaryKeySelective"
,
100L
,
tbBuilder
.
invoke
(
"build"
),
columns
);
Assert
.
assertEquals
(
sql
,
"update tb SET inc_f1 = inc_f1 + 1, id = 1 , field1 = 'null' , inc_f2 = inc_f2 + 5 where inc_f1 = 100 and id = 1"
);
// 执行一次,因为版本号100不存在所以应该返回0(普通非空判断)
Object
result
=
tbMapper
.
invoke
(
"updateWithVersionByPrimaryKeySelective"
,
100L
,
tbBuilder
.
invoke
(
"build"
),
null
);
Assert
.
assertEquals
(
result
,
0
);
// id = 1 的版本号应该是0(selective 增强)
result
=
tbMapper
.
invoke
(
"updateWithVersionByPrimaryKeySelective"
,
0L
,
tbBuilder
.
invoke
(
"build"
),
columns
);
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
);
Assert
.
assertEquals
(
rs
.
getInt
(
"inc_f2"
),
7
);
}
});
}
}
}
\ No newline at end of file
src/test/resources/scripts/OptimisticLockerPlugin/mybatis-generator-with-SelectiveEnhancedPlugin-IncrementsPlugin.xml
0 → 100644
View file @
f9fbeeb3
<?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.OptimisticLockerPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.SelectiveEnhancedPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.IncrementsPlugin"
/>
<plugin
type=
"com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin"
/>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass=
"${driver}"
connectionURL=
"${url}"
userId=
"${username}"
password=
"${password}"
/>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator
targetPackage=
""
targetProject=
""
>
<!-- 是否对model添加 构造函数 -->
<property
name=
"constructorBased"
value=
"true"
/>
<!-- 给Model添加一个父类 -->
<!--<property name="rootClass" value="com.itfsw.base"/>-->
</javaModelGenerator>
<!--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"
>
<property
name=
"versionColumn"
value=
"inc_f1"
/>
<property
name=
"incrementsColumns"
value=
"inc_f1,inc_f2"
/>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
<table
tableName=
"tb_keys"
>
<property
name=
"versionColumn"
value=
"inc_f1"
/>
<property
name=
"incrementsColumns"
value=
"inc_f2"
/>
</table>
<table
tableName=
"tb_blobs"
>
<property
name=
"versionColumn"
value=
"inc_f1"
/>
<property
name=
"incrementsColumns"
value=
"inc_f2"
/>
<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