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
e8c4641f
Commit
e8c4641f
authored
May 04, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
乐观锁插件实现
parent
d38e66e5
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
218 additions
and
103 deletions
+218
-103
src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
...fsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
+180
-60
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java
...tis/generator/plugins/utils/XmlElementGeneratorTools.java
+12
-41
src/test/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPluginTest.java
...mybatis/generator/plugins/OptimisticLockerPluginTest.java
+21
-1
src/test/resources/scripts/OptimisticLockerPlugin/init.sql
src/test/resources/scripts/OptimisticLockerPlugin/init.sql
+1
-1
src/test/resources/scripts/OptimisticLockerPlugin/mybatis-generator.xml
...rces/scripts/OptimisticLockerPlugin/mybatis-generator.xml
+4
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
View file @
e8c4641f
This diff is collapsed.
Click to expand it.
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java
View file @
e8c4641f
...
@@ -50,33 +50,27 @@ public class XmlElementGeneratorTools {
...
@@ -50,33 +50,27 @@ public class XmlElementGeneratorTools {
* @return the selectKey element
* @return the selectKey element
*/
*/
public
static
Element
getSelectKey
(
IntrospectedColumn
introspectedColumn
,
GeneratedKey
generatedKey
)
{
public
static
Element
getSelectKey
(
IntrospectedColumn
introspectedColumn
,
GeneratedKey
generatedKey
)
{
String
identityColumnType
=
introspectedColumn
String
identityColumnType
=
introspectedColumn
.
getFullyQualifiedJavaType
().
getFullyQualifiedName
();
.
getFullyQualifiedJavaType
().
getFullyQualifiedName
();
XmlElement
answer
=
new
XmlElement
(
"selectKey"
);
XmlElement
answer
=
new
XmlElement
(
"selectKey"
);
answer
.
addAttribute
(
new
Attribute
(
"resultType"
,
identityColumnType
));
answer
.
addAttribute
(
new
Attribute
(
"resultType"
,
identityColumnType
));
answer
.
addAttribute
(
new
Attribute
(
answer
.
addAttribute
(
new
Attribute
(
"keyProperty"
,
introspectedColumn
.
getJavaProperty
()));
"keyProperty"
,
introspectedColumn
.
getJavaProperty
()));
answer
.
addAttribute
(
new
Attribute
(
"order"
,
generatedKey
.
getMyBatis3Order
()));
answer
.
addAttribute
(
new
Attribute
(
"order"
,
generatedKey
.
getMyBatis3Order
()));
answer
.
addElement
(
new
TextElement
(
generatedKey
answer
.
addElement
(
new
TextElement
(
generatedKey
.
getRuntimeSqlStatement
()));
.
getRuntimeSqlStatement
()));
return
answer
;
return
answer
;
}
}
public
static
Element
getBaseColumnListElement
(
IntrospectedTable
introspectedTable
)
{
public
static
Element
getBaseColumnListElement
(
IntrospectedTable
introspectedTable
)
{
XmlElement
answer
=
new
XmlElement
(
"include"
);
XmlElement
answer
=
new
XmlElement
(
"include"
);
answer
.
addAttribute
(
new
Attribute
(
"refid"
,
answer
.
addAttribute
(
new
Attribute
(
"refid"
,
introspectedTable
.
getBaseColumnListId
()));
introspectedTable
.
getBaseColumnListId
()));
return
answer
;
return
answer
;
}
}
public
static
Element
getBlobColumnListElement
(
IntrospectedTable
introspectedTable
)
{
public
static
Element
getBlobColumnListElement
(
IntrospectedTable
introspectedTable
)
{
XmlElement
answer
=
new
XmlElement
(
"include"
);
XmlElement
answer
=
new
XmlElement
(
"include"
);
answer
.
addAttribute
(
new
Attribute
(
"refid"
,
answer
.
addAttribute
(
new
Attribute
(
"refid"
,
introspectedTable
.
getBlobColumnListId
()));
introspectedTable
.
getBlobColumnListId
()));
return
answer
;
return
answer
;
}
}
...
@@ -85,8 +79,7 @@ public class XmlElementGeneratorTools {
...
@@ -85,8 +79,7 @@ public class XmlElementGeneratorTools {
ifElement
.
addAttribute
(
new
Attribute
(
"test"
,
"_parameter != null"
));
ifElement
.
addAttribute
(
new
Attribute
(
"test"
,
"_parameter != null"
));
XmlElement
includeElement
=
new
XmlElement
(
"include"
);
XmlElement
includeElement
=
new
XmlElement
(
"include"
);
includeElement
.
addAttribute
(
new
Attribute
(
"refid"
,
includeElement
.
addAttribute
(
new
Attribute
(
"refid"
,
introspectedTable
.
getExampleWhereClauseId
()));
introspectedTable
.
getExampleWhereClauseId
()));
ifElement
.
addElement
(
includeElement
);
ifElement
.
addElement
(
includeElement
);
return
ifElement
;
return
ifElement
;
...
@@ -97,8 +90,7 @@ public class XmlElementGeneratorTools {
...
@@ -97,8 +90,7 @@ public class XmlElementGeneratorTools {
ifElement
.
addAttribute
(
new
Attribute
(
"test"
,
"_parameter != null"
));
ifElement
.
addAttribute
(
new
Attribute
(
"test"
,
"_parameter != null"
));
XmlElement
includeElement
=
new
XmlElement
(
"include"
);
XmlElement
includeElement
=
new
XmlElement
(
"include"
);
includeElement
.
addAttribute
(
new
Attribute
(
"refid"
,
includeElement
.
addAttribute
(
new
Attribute
(
"refid"
,
introspectedTable
.
getMyBatis3UpdateByExampleWhereClauseId
()));
introspectedTable
.
getMyBatis3UpdateByExampleWhereClauseId
()));
ifElement
.
addElement
(
includeElement
);
ifElement
.
addElement
(
includeElement
);
return
ifElement
;
return
ifElement
;
...
@@ -270,18 +262,7 @@ public class XmlElementGeneratorTools {
...
@@ -270,18 +262,7 @@ public class XmlElementGeneratorTools {
* @return
* @return
*/
*/
public
static
List
<
Element
>
generateSets
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
)
{
public
static
List
<
Element
>
generateSets
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
)
{
return
generateSets
(
columns
,
prefix
,
false
);
return
generateCommColumns
(
columns
,
prefix
,
false
,
3
);
}
/**
* 生成sets Ele
* @param columns
* @param prefix
* @param bracket
* @return
*/
public
static
List
<
Element
>
generateSets
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
,
boolean
bracket
)
{
return
generateCommColumns
(
columns
,
prefix
,
bracket
,
3
);
}
}
/**
/**
...
@@ -290,7 +271,7 @@ public class XmlElementGeneratorTools {
...
@@ -290,7 +271,7 @@ public class XmlElementGeneratorTools {
* @return
* @return
*/
*/
public
static
XmlElement
generateSetsSelective
(
List
<
IntrospectedColumn
>
columns
)
{
public
static
XmlElement
generateSetsSelective
(
List
<
IntrospectedColumn
>
columns
)
{
return
generateSetsSelective
(
columns
,
null
,
false
);
return
generateSetsSelective
(
columns
,
null
);
}
}
/**
/**
...
@@ -300,18 +281,7 @@ public class XmlElementGeneratorTools {
...
@@ -300,18 +281,7 @@ public class XmlElementGeneratorTools {
* @return
* @return
*/
*/
public
static
XmlElement
generateSetsSelective
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
)
{
public
static
XmlElement
generateSetsSelective
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
)
{
return
generateSetsSelective
(
columns
,
prefix
,
false
);
return
generateCommColumnsSelective
(
columns
,
prefix
,
false
,
3
);
}
/**
* 生成sets Selective Ele
* @param columns
* @param prefix
* @param bracket
* @return
*/
public
static
XmlElement
generateSetsSelective
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
,
boolean
bracket
)
{
return
generateCommColumnsSelective
(
columns
,
prefix
,
bracket
,
3
);
}
}
/**
/**
...
@@ -325,6 +295,7 @@ public class XmlElementGeneratorTools {
...
@@ -325,6 +295,7 @@ public class XmlElementGeneratorTools {
private
static
List
<
Element
>
generateCommColumns
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
,
boolean
bracket
,
int
type
)
{
private
static
List
<
Element
>
generateCommColumns
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
,
boolean
bracket
,
int
type
)
{
List
<
Element
>
list
=
new
ArrayList
<>();
List
<
Element
>
list
=
new
ArrayList
<>();
// 只有upsert插件才会传入 IdentityAndGeneratedAlwaysColumn
if
(
hasIdentityAndGeneratedAlwaysColumns
(
columns
))
{
if
(
hasIdentityAndGeneratedAlwaysColumns
(
columns
))
{
XmlElement
trimEle
=
generateTrim
(
bracket
);
XmlElement
trimEle
=
generateTrim
(
bracket
);
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPluginTest.java
View file @
e8c4641f
...
@@ -47,8 +47,28 @@ public class OptimisticLockerPluginTest {
...
@@ -47,8 +47,28 @@ public class OptimisticLockerPluginTest {
*/
*/
@Test
@Test
public
void
testUpdateWithVersionByExampleSelective
()
throws
Exception
{
public
void
testUpdateWithVersionByExampleSelective
()
throws
Exception
{
// 测试不带or的更新
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OptimisticLockerPlugin/mybatis-generator.xml"
);
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OptimisticLockerPlugin/mybatis-generator.xml"
);
tool
.
generate
(
new
AbstractShellCallback
()
{
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
tb
=
new
ObjectUtil
(
loader
,
packagz
+
".Tb"
);
tb
.
set
(
"incF2"
,
10L
);
tb
.
set
(
"incF3"
,
5L
);
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"updateWithVersionByExample"
,
1L
,
tb
.
getObject
(),
tbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb SET field_1 = 'null' , inc_f2 = 5 WHERE ( id = '1' )"
);
}
});
tool
.
generate
(()
->
DBHelper
.
createDB
(
"scripts/OptimisticLockerPlugin/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
{
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
ObjectUtil
tbMapper
=
new
ObjectUtil
(
sqlSession
.
getMapper
(
loader
.
loadClass
(
packagz
+
".TbMapper"
)));
...
...
src/test/resources/scripts/OptimisticLockerPlugin/init.sql
View file @
e8c4641f
...
@@ -20,7 +20,7 @@ SET FOREIGN_KEY_CHECKS=0;
...
@@ -20,7 +20,7 @@ SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb`
;
DROP
TABLE
IF
EXISTS
`tb`
;
CREATE
TABLE
`tb`
(
CREATE
TABLE
`tb`
(
`id`
bigint
(
20
)
NOT
NULL
COMMENT
'注释1'
,
`id`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
COMMENT
'注释1'
,
`field1`
varchar
(
255
)
DEFAULT
NULL
COMMENT
'注释2'
,
`field1`
varchar
(
255
)
DEFAULT
NULL
COMMENT
'注释2'
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
DEFAULT
'0'
,
...
...
src/test/resources/scripts/OptimisticLockerPlugin/mybatis-generator.xml
View file @
e8c4641f
...
@@ -53,9 +53,13 @@
...
@@ -53,9 +53,13 @@
</table>
</table>
<table
tableName=
"tb_keys"
>
<table
tableName=
"tb_keys"
>
<property
name=
"versionColumn"
value=
"inc_f1"
/>
<property
name=
"versionColumn"
value=
"inc_f1"
/>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
</table>
<table
tableName=
"tb_blobs"
>
<table
tableName=
"tb_blobs"
>
<property
name=
"versionColumn"
value=
"inc_f1"
/>
<property
name=
"versionColumn"
value=
"inc_f1"
/>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
</table>
</context>
</context>
</generatorConfiguration>
</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