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
9724ff57
Commit
9724ff57
authored
Oct 31, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
乐观锁配合逻辑删除插件使用
parent
edccee50
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
333 additions
and
23 deletions
+333
-23
src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java
.../itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java
+14
-6
src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
...fsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
+133
-11
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java
.../mybatis/generator/plugins/utils/hook/HookAggregator.java
+4
-4
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/ILogicalDeletePluginHook.java
...enerator/plugins/utils/hook/ILogicalDeletePluginHook.java
+10
-2
src/test/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPluginTest.java
...mybatis/generator/plugins/OptimisticLockerPluginTest.java
+39
-0
src/test/resources/scripts/OptimisticLockerPlugin/mybatis-generator-with-LogicalDeletePlugin-customizedNextVersion.xml
...erator-with-LogicalDeletePlugin-customizedNextVersion.xml
+71
-0
src/test/resources/scripts/OptimisticLockerPlugin/mybatis-generator-with-LogicalDeletePlugin.xml
...ckerPlugin/mybatis-generator-with-LogicalDeletePlugin.xml
+62
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java
View file @
9724ff57
...
@@ -16,10 +16,8 @@
...
@@ -16,10 +16,8 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.*
;
import
com.itfsw.mybatis.generator.plugins.utils.FormatTools
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.ILogicalDeletePluginHook
;
import
com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools
;
import
com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.*
;
import
org.mybatis.generator.api.dom.java.*
;
...
@@ -168,6 +166,8 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -168,6 +166,8 @@ public class LogicalDeletePlugin extends BasePlugin {
// interface 增加方法
// interface 增加方法
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
mLogicalDeleteByExample
);
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
mLogicalDeleteByExample
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加方法logicalDeleteByExample。"
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加方法logicalDeleteByExample。"
);
// hook
PluginTools
.
getHook
(
ILogicalDeletePluginHook
.
class
).
clientLogicalDeleteByExampleMethodGenerated
(
mLogicalDeleteByExample
,
interfaze
,
introspectedTable
);
// 2. 判断是否有主键,生成主键删除方法
// 2. 判断是否有主键,生成主键删除方法
if
(
introspectedTable
.
hasPrimaryKeyColumns
())
{
if
(
introspectedTable
.
hasPrimaryKeyColumns
())
{
...
@@ -232,6 +232,8 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -232,6 +232,8 @@ public class LogicalDeletePlugin extends BasePlugin {
interfaze
.
addImportedTypes
(
importedTypes
);
interfaze
.
addImportedTypes
(
importedTypes
);
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
mLogicalDeleteByPrimaryKey
);
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
mLogicalDeleteByPrimaryKey
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加方法logicalDeleteByPrimaryKey。"
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加方法logicalDeleteByPrimaryKey。"
);
// hook
PluginTools
.
getHook
(
ILogicalDeletePluginHook
.
class
).
clientLogicalDeleteByPrimaryKeyMethodGenerated
(
mLogicalDeleteByPrimaryKey
,
interfaze
,
introspectedTable
);
// 增强selectByPrimaryKey
// 增强selectByPrimaryKey
mSelectByPrimaryKey
.
addParameter
(
new
Parameter
(
mSelectByPrimaryKey
.
addParameter
(
new
Parameter
(
...
@@ -279,7 +281,8 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -279,7 +281,8 @@ public class LogicalDeletePlugin extends BasePlugin {
||
JDBCType
.
LONGVARCHAR
==
type
||
JDBCType
.
LONGVARCHAR
==
type
||
JDBCType
.
NCHAR
==
type
||
JDBCType
.
NCHAR
==
type
||
JDBCType
.
NVARCHAR
==
type
||
JDBCType
.
NVARCHAR
==
type
||
JDBCType
.
VARCHAR
==
type
)
{
||
JDBCType
.
VARCHAR
==
type
||
JDBCType
.
BIGINT
==
type
)
{
sb
.
append
(
"'"
);
sb
.
append
(
"'"
);
sb
.
append
(
this
.
logicalDeleteValue
);
sb
.
append
(
this
.
logicalDeleteValue
);
sb
.
append
(
"'"
);
sb
.
append
(
"'"
);
...
@@ -292,6 +295,8 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -292,6 +295,8 @@ public class LogicalDeletePlugin extends BasePlugin {
logicalDeleteByExample
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
logicalDeleteByExample
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
document
.
getRootElement
().
addElement
(
logicalDeleteByExample
);
document
.
getRootElement
().
addElement
(
logicalDeleteByExample
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加方法logicalDeleteByExample的实现。"
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加方法logicalDeleteByExample的实现。"
);
// hook
PluginTools
.
getHook
(
ILogicalDeletePluginHook
.
class
).
sqlMapLogicalDeleteByExampleElementGenerated
(
document
,
logicalDeleteByExample
,
this
.
logicalDeleteColumn
,
this
.
logicalDeleteValue
,
introspectedTable
);
// 2. 判断是否有主键,生成主键删除方法
// 2. 判断是否有主键,生成主键删除方法
if
(
introspectedTable
.
hasPrimaryKeyColumns
())
{
if
(
introspectedTable
.
hasPrimaryKeyColumns
())
{
...
@@ -330,7 +335,8 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -330,7 +335,8 @@ public class LogicalDeletePlugin extends BasePlugin {
||
JDBCType
.
LONGVARCHAR
==
type1
||
JDBCType
.
LONGVARCHAR
==
type1
||
JDBCType
.
NCHAR
==
type1
||
JDBCType
.
NCHAR
==
type1
||
JDBCType
.
NVARCHAR
==
type1
||
JDBCType
.
NVARCHAR
==
type1
||
JDBCType
.
VARCHAR
==
type1
)
{
||
JDBCType
.
VARCHAR
==
type1
||
JDBCType
.
BIGINT
==
type1
)
{
sb1
.
append
(
"'"
);
sb1
.
append
(
"'"
);
sb1
.
append
(
this
.
logicalDeleteValue
);
sb1
.
append
(
this
.
logicalDeleteValue
);
sb1
.
append
(
"'"
);
sb1
.
append
(
"'"
);
...
@@ -344,6 +350,8 @@ public class LogicalDeletePlugin extends BasePlugin {
...
@@ -344,6 +350,8 @@ public class LogicalDeletePlugin extends BasePlugin {
document
.
getRootElement
().
addElement
(
logicalDeleteByPrimaryKey
);
document
.
getRootElement
().
addElement
(
logicalDeleteByPrimaryKey
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加方法logicalDeleteByPrimaryKey的实现。"
);
logger
.
debug
(
"itfsw(逻辑删除插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加方法logicalDeleteByPrimaryKey的实现。"
);
// hook
PluginTools
.
getHook
(
ILogicalDeletePluginHook
.
class
).
sqlMapLogicalDeleteByPrimaryKeyElementGenerated
(
document
,
logicalDeleteByPrimaryKey
,
this
.
logicalDeleteColumn
,
this
.
logicalDeleteValue
,
introspectedTable
);
// 3. 增强selectByPrimaryKey
// 3. 增强selectByPrimaryKey
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
View file @
9724ff57
...
@@ -29,6 +29,7 @@ import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
...
@@ -29,6 +29,7 @@ import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import
org.mybatis.generator.internal.util.JavaBeansUtil
;
import
org.mybatis.generator.internal.util.JavaBeansUtil
;
import
org.mybatis.generator.internal.util.StringUtility
;
import
org.mybatis.generator.internal.util.StringUtility
;
import
java.sql.JDBCType
;
import
java.util.*
;
import
java.util.*
;
/**
/**
...
@@ -119,10 +120,23 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP
...
@@ -119,10 +120,23 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP
@Override
@Override
public
boolean
clientLogicalDeleteByExampleMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
public
boolean
clientLogicalDeleteByExampleMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
if
(
this
.
versionColumn
!=
null
)
{
FormatTools
.
addMethodWithBestPosition
(
if
(
this
.
customizedNextVersion
)
{
interfaze
,
Method
newMethod
=
JavaElementGeneratorTools
.
generateMethod
(
this
.
replaceDeleteExampleMethod
(
introspectedTable
,
method
,
interfaze
,
METHOD_LOGICAL_DELETE_WITH_VERSION_BY_EXAMPLE
)
METHOD_LOGICAL_DELETE_WITH_VERSION_BY_EXAMPLE
,
);
JavaVisibility
.
DEFAULT
,
FullyQualifiedJavaType
.
getIntInstance
(),
new
Parameter
(
this
.
versionColumn
.
getFullyQualifiedJavaType
(),
"version"
,
"@Param(\"version\")"
),
new
Parameter
(
this
.
versionColumn
.
getFullyQualifiedJavaType
(),
"nextVersion"
,
"@Param(\"nextVersion\")"
),
new
Parameter
(
new
FullyQualifiedJavaType
(
introspectedTable
.
getExampleType
()),
"example"
,
"@Param(\"example\")"
)
);
commentGenerator
.
addGeneralMethodComment
(
newMethod
,
introspectedTable
);
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
newMethod
);
}
else
{
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
this
.
replaceDeleteExampleMethod
(
introspectedTable
,
method
,
interfaze
,
METHOD_LOGICAL_DELETE_WITH_VERSION_BY_EXAMPLE
)
);
}
}
}
return
true
;
return
true
;
}
}
...
@@ -130,10 +144,23 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP
...
@@ -130,10 +144,23 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP
@Override
@Override
public
boolean
clientLogicalDeleteByPrimaryKeyMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
public
boolean
clientLogicalDeleteByPrimaryKeyMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
if
(
this
.
versionColumn
!=
null
)
{
FormatTools
.
addMethodWithBestPosition
(
if
(
this
.
customizedNextVersion
)
{
interfaze
,
Method
newMethod
=
JavaElementGeneratorTools
.
generateMethod
(
this
.
replaceDeleteExampleMethod
(
introspectedTable
,
method
,
interfaze
,
METHOD_LOGICAL_DELETE_WITH_VERSION_BY_PRIMARY_KEY
)
METHOD_LOGICAL_DELETE_WITH_VERSION_BY_PRIMARY_KEY
,
);
JavaVisibility
.
DEFAULT
,
FullyQualifiedJavaType
.
getIntInstance
(),
new
Parameter
(
this
.
versionColumn
.
getFullyQualifiedJavaType
(),
"version"
,
"@Param(\"version\")"
),
new
Parameter
(
this
.
versionColumn
.
getFullyQualifiedJavaType
(),
"nextVersion"
,
"@Param(\"nextVersion\")"
),
new
Parameter
(
method
.
getParameters
().
get
(
0
).
getType
(),
method
.
getParameters
().
get
(
0
).
getName
(),
"@Param(\"key\")"
)
);
commentGenerator
.
addGeneralMethodComment
(
newMethod
,
introspectedTable
);
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
newMethod
);
}
else
{
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
this
.
replaceDeletePrimaryKeyMethod
(
introspectedTable
,
method
,
interfaze
,
METHOD_LOGICAL_DELETE_WITH_VERSION_BY_PRIMARY_KEY
)
);
}
}
}
return
true
;
return
true
;
}
}
...
@@ -446,12 +473,30 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP
...
@@ -446,12 +473,30 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP
}
}
@Override
@Override
public
boolean
sqlMapLogicalDeleteByExampleElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
public
boolean
sqlMapLogicalDeleteByExampleElementGenerated
(
Document
document
,
XmlElement
element
,
IntrospectedColumn
logicalDeleteColumn
,
String
logicalDeleteValue
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
FormatTools
.
addElementWithBestPosition
(
document
.
getRootElement
(),
this
.
generateSqlMapLogicalDelete
(
introspectedTable
,
METHOD_LOGICAL_DELETE_WITH_VERSION_BY_EXAMPLE
,
logicalDeleteColumn
,
logicalDeleteValue
,
true
));
}
return
true
;
return
true
;
}
}
@Override
@Override
public
boolean
sqlMapLogicalDeleteByPrimaryKeyElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
public
boolean
sqlMapLogicalDeleteByPrimaryKeyElementGenerated
(
Document
document
,
XmlElement
element
,
IntrospectedColumn
logicalDeleteColumn
,
String
logicalDeleteValue
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
FormatTools
.
addElementWithBestPosition
(
document
.
getRootElement
(),
this
.
generateSqlMapLogicalDelete
(
introspectedTable
,
METHOD_LOGICAL_DELETE_WITH_VERSION_BY_PRIMARY_KEY
,
logicalDeleteColumn
,
logicalDeleteValue
,
false
));
}
return
true
;
return
true
;
}
}
...
@@ -563,7 +608,7 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP
...
@@ -563,7 +608,7 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP
FormatTools
.
replaceGeneralMethodComment
(
commentGenerator
,
withVersionMethod
,
introspectedTable
);
FormatTools
.
replaceGeneralMethodComment
(
commentGenerator
,
withVersionMethod
,
introspectedTable
);
Parameter
versionParam
=
new
Parameter
(
this
.
versionColumn
.
getFullyQualifiedJavaType
(),
"version"
,
"@Param(\"version\")"
);
Parameter
versionParam
=
new
Parameter
(
this
.
versionColumn
.
getFullyQualifiedJavaType
(),
"version"
,
"@Param(\"version\")"
);
Parameter
keyParam
=
new
Parameter
(
method
.
getParameters
().
get
(
0
).
getType
(),
"key"
,
"@Param(\"key\")"
);
Parameter
keyParam
=
new
Parameter
(
method
.
getParameters
().
get
(
0
).
getType
(),
method
.
getParameters
().
get
(
0
).
getName
()
,
"@Param(\"key\")"
);
withVersionMethod
.
getParameters
().
clear
();
withVersionMethod
.
getParameters
().
clear
();
withVersionMethod
.
addParameter
(
versionParam
);
withVersionMethod
.
addParameter
(
versionParam
);
...
@@ -736,6 +781,83 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP
...
@@ -736,6 +781,83 @@ public class OptimisticLockerPlugin extends BasePlugin implements IModelBuilderP
return
updateEle
;
return
updateEle
;
}
}
/**
* 生成LogicalDelete sql map
* @param introspectedTable
* @param id
* @param logicalDeleteColumn
* @param logicalDeleteValue
* @param byExample
* @return
*/
private
XmlElement
generateSqlMapLogicalDelete
(
IntrospectedTable
introspectedTable
,
String
id
,
IntrospectedColumn
logicalDeleteColumn
,
String
logicalDeleteValue
,
boolean
byExample
)
{
XmlElement
updateEle
=
new
XmlElement
(
"update"
);
updateEle
.
addAttribute
(
new
Attribute
(
"id"
,
id
));
updateEle
.
addAttribute
(
new
Attribute
(
"parameterType"
,
"map"
));
commentGenerator
.
addComment
(
updateEle
);
updateEle
.
addElement
(
new
TextElement
(
"update "
+
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
()));
StringBuilder
sb
=
new
StringBuilder
(
"set "
);
// 版本自增
if
(
this
.
customizedNextVersion
)
{
sb
.
append
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
this
.
versionColumn
));
sb
.
append
(
" = "
);
sb
.
append
(
"#{nextVersion,jdbcType="
);
sb
.
append
(
this
.
versionColumn
.
getJdbcTypeName
());
if
(
StringUtility
.
stringHasValue
(
this
.
versionColumn
.
getTypeHandler
()))
{
sb
.
append
(
",typeHandler="
);
sb
.
append
(
this
.
versionColumn
.
getTypeHandler
());
}
sb
.
append
(
"}"
);
}
else
{
sb
.
append
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
this
.
versionColumn
));
sb
.
append
(
" = "
);
sb
.
append
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
this
.
versionColumn
));
sb
.
append
(
" + 1"
);
}
sb
.
append
(
","
);
// 逻辑删除 set
sb
.
append
(
logicalDeleteColumn
.
getActualColumnName
());
sb
.
append
(
" = "
);
// 判断字段类型
JDBCType
type
=
JDBCType
.
valueOf
(
logicalDeleteColumn
.
getJdbcType
());
if
(
logicalDeleteValue
==
null
||
"NULL"
.
equalsIgnoreCase
(
logicalDeleteValue
))
{
sb
.
append
(
"NULL"
);
}
else
if
(
JDBCType
.
CHAR
==
type
||
JDBCType
.
LONGNVARCHAR
==
type
||
JDBCType
.
LONGVARCHAR
==
type
||
JDBCType
.
NCHAR
==
type
||
JDBCType
.
NVARCHAR
==
type
||
JDBCType
.
VARCHAR
==
type
||
JDBCType
.
BIGINT
==
type
)
{
sb
.
append
(
"'"
);
sb
.
append
(
logicalDeleteValue
);
sb
.
append
(
"'"
);
}
else
{
sb
.
append
(
logicalDeleteValue
);
}
updateEle
.
addElement
(
new
TextElement
(
sb
.
toString
()));
// 更新条件
if
(
byExample
)
{
XmlElement
ifElement
=
new
XmlElement
(
"if"
);
ifElement
.
addAttribute
(
new
Attribute
(
"test"
,
"_parameter != null"
));
XmlElement
includeElement
=
new
XmlElement
(
"include"
);
includeElement
.
addAttribute
(
new
Attribute
(
"refid"
,
SQL_UPDATE_BY_EXAMPLE_WITH_VERSION_WHERE_CLAUSE
));
ifElement
.
addElement
(
includeElement
);
updateEle
.
addElement
(
ifElement
);
}
else
{
updateEle
=
this
.
replacePrimaryKeyXmlElement
(
introspectedTable
,
updateEle
,
id
,
false
);
}
return
updateEle
;
}
/**
/**
* 生成版本号set节点
* 生成版本号set节点
* @param selective
* @param selective
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java
View file @
9724ff57
...
@@ -290,9 +290,9 @@ public class HookAggregator implements IUpsertPluginHook,
...
@@ -290,9 +290,9 @@ public class HookAggregator implements IUpsertPluginHook,
}
}
@Override
@Override
public
boolean
sqlMapLogicalDeleteByExampleElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
public
boolean
sqlMapLogicalDeleteByExampleElementGenerated
(
Document
document
,
XmlElement
element
,
IntrospectedColumn
logicalDeleteColumn
,
String
logicalDeleteValue
,
IntrospectedTable
introspectedTable
)
{
for
(
ILogicalDeletePluginHook
plugin
:
this
.
getPlugins
(
ILogicalDeletePluginHook
.
class
))
{
for
(
ILogicalDeletePluginHook
plugin
:
this
.
getPlugins
(
ILogicalDeletePluginHook
.
class
))
{
if
(!
plugin
.
sqlMapLogicalDeleteByExampleElementGenerated
(
element
,
introspectedTable
))
{
if
(!
plugin
.
sqlMapLogicalDeleteByExampleElementGenerated
(
document
,
element
,
logicalDeleteColumn
,
logicalDeleteValue
,
introspectedTable
))
{
return
false
;
return
false
;
}
}
}
}
...
@@ -300,9 +300,9 @@ public class HookAggregator implements IUpsertPluginHook,
...
@@ -300,9 +300,9 @@ public class HookAggregator implements IUpsertPluginHook,
}
}
@Override
@Override
public
boolean
sqlMapLogicalDeleteByPrimaryKeyElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
public
boolean
sqlMapLogicalDeleteByPrimaryKeyElementGenerated
(
Document
document
,
XmlElement
element
,
IntrospectedColumn
logicalDeleteColumn
,
String
logicalDeleteValue
,
IntrospectedTable
introspectedTable
)
{
for
(
ILogicalDeletePluginHook
plugin
:
this
.
getPlugins
(
ILogicalDeletePluginHook
.
class
))
{
for
(
ILogicalDeletePluginHook
plugin
:
this
.
getPlugins
(
ILogicalDeletePluginHook
.
class
))
{
if
(!
plugin
.
sqlMapLogicalDeleteByPrimaryKeyElementGenerated
(
element
,
introspectedTable
))
{
if
(!
plugin
.
sqlMapLogicalDeleteByPrimaryKeyElementGenerated
(
document
,
element
,
logicalDeleteColumn
,
logicalDeleteValue
,
introspectedTable
))
{
return
false
;
return
false
;
}
}
}
}
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/ILogicalDeletePluginHook.java
View file @
9724ff57
...
@@ -16,9 +16,11 @@
...
@@ -16,9 +16,11 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
.
hook
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
.
hook
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.Interface
;
import
org.mybatis.generator.api.dom.java.Interface
;
import
org.mybatis.generator.api.dom.java.Method
;
import
org.mybatis.generator.api.dom.java.Method
;
import
org.mybatis.generator.api.dom.xml.Document
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
/**
/**
...
@@ -50,17 +52,23 @@ public interface ILogicalDeletePluginHook {
...
@@ -50,17 +52,23 @@ public interface ILogicalDeletePluginHook {
/**
/**
* logicalDeleteByExample
* logicalDeleteByExample
* @param document
* @param element
* @param element
* @param logicalDeleteColumn
* @param logicalDeleteValue
* @param introspectedTable
* @param introspectedTable
* @return
* @return
*/
*/
boolean
sqlMapLogicalDeleteByExampleElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
);
boolean
sqlMapLogicalDeleteByExampleElementGenerated
(
Document
document
,
XmlElement
element
,
IntrospectedColumn
logicalDeleteColumn
,
String
logicalDeleteValue
,
IntrospectedTable
introspectedTable
);
/**
/**
* logicalDeleteByPrimaryKey
* logicalDeleteByPrimaryKey
* @param document
* @param element
* @param element
* @param logicalDeleteColumn
* @param logicalDeleteValue
* @param introspectedTable
* @param introspectedTable
* @return
* @return
*/
*/
boolean
sqlMapLogicalDeleteByPrimaryKeyElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
);
boolean
sqlMapLogicalDeleteByPrimaryKeyElementGenerated
(
Document
document
,
XmlElement
element
,
IntrospectedColumn
logicalDeleteColumn
,
String
logicalDeleteValue
,
IntrospectedTable
introspectedTable
);
}
}
src/test/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPluginTest.java
View file @
9724ff57
...
@@ -334,6 +334,45 @@ public class OptimisticLockerPluginTest {
...
@@ -334,6 +334,45 @@ public class OptimisticLockerPluginTest {
});
});
}
}
/**
* 测试整合LogicalDelete插件
*/
@Test
public
void
testWithLogicalDeletePlugin
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OptimisticLockerPlugin/mybatis-generator-with-LogicalDeletePlugin.xml"
);
tool
.
generate
(()
->
DBHelper
.
resetDB
(
"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"
,
1L
);
// 验证sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
OptimisticLockerPlugin
.
METHOD_LOGICAL_DELETE_WITH_VERSION_BY_EXAMPLE
,
0L
,
tbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb set inc_f1 = inc_f1 + 1,inc_f2 = '9L' WHERE inc_f1 = 0 and ( ( id = '1' ) )"
);
// 验证执行
Object
result
=
tbMapper
.
invoke
(
OptimisticLockerPlugin
.
METHOD_LOGICAL_DELETE_WITH_VERSION_BY_EXAMPLE
,
0L
,
tbExample
.
getObject
());
Assert
.
assertEquals
(
result
,
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"
),
9
);
}
});
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OptimisticLockerPlugin/mybatis-generator-with-LogicalDeletePlugin-customizedNextVersion.xml"
);
tool
.
generate
(()
->
DBHelper
.
resetDB
(
"scripts/OptimisticLockerPlugin/init.sql"
),
new
AbstractShellCallback
()
{
@Override
public
void
reloadProject
(
SqlSession
sqlSession
,
ClassLoader
loader
,
String
packagz
)
throws
Exception
{
}
});
}
/**
/**
* 测试 updateWithVersionByExampleWithBLOBs
* 测试 updateWithVersionByExampleWithBLOBs
*/
*/
...
...
src/test/resources/scripts/OptimisticLockerPlugin/mybatis-generator-with-LogicalDeletePlugin-customizedNextVersion.xml
0 → 100644
View file @
9724ff57
<?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.LogicalDeletePlugin"
>
<!-- 逻辑删除-已删除值 -->
<property
name=
"logicalDeleteValue"
value=
"9L"
/>
<!-- 逻辑删除-未删除值 -->
<property
name=
"logicalUnDeleteValue"
value=
"0L"
/>
</plugin>
<!--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=
"logicalDeleteColumn"
value=
"inc_f2"
/>
<property
name=
"customizedNextVersion"
value=
"true"
/>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
<table
tableName=
"tb_keys"
>
<property
name=
"versionColumn"
value=
"inc_f1"
/>
<property
name=
"logicalDeleteColumn"
value=
"inc_f2"
/>
<property
name=
"customizedNextVersion"
value=
"true"
/>
</table>
</context>
</generatorConfiguration>
\ No newline at end of file
src/test/resources/scripts/OptimisticLockerPlugin/mybatis-generator-with-LogicalDeletePlugin.xml
0 → 100644
View file @
9724ff57
<?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.LogicalDeletePlugin"
>
<!-- 逻辑删除-已删除值 -->
<property
name=
"logicalDeleteValue"
value=
"9L"
/>
<!-- 逻辑删除-未删除值 -->
<property
name=
"logicalUnDeleteValue"
value=
"0L"
/>
</plugin>
<!--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=
"logicalDeleteColumn"
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