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
331634de
Commit
331634de
authored
May 03, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
乐观锁插件实现
parent
ab702538
Changes
17
Hide whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
1070 additions
and
180 deletions
+1070
-180
src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
...com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
+2
-2
src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java
.../itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java
+1
-1
src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
...fsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
+461
-4
src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
...sw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
+14
-19
src/main/java/com/itfsw/mybatis/generator/plugins/utils/BasePlugin.java
...com/itfsw/mybatis/generator/plugins/utils/BasePlugin.java
+1
-4
src/main/java/com/itfsw/mybatis/generator/plugins/utils/BeanUtils.java
.../com/itfsw/mybatis/generator/plugins/utils/BeanUtils.java
+72
-0
src/main/java/com/itfsw/mybatis/generator/plugins/utils/FormatTools.java
...om/itfsw/mybatis/generator/plugins/utils/FormatTools.java
+94
-41
src/main/java/com/itfsw/mybatis/generator/plugins/utils/IntrospectedTableTools.java
...batis/generator/plugins/utils/IntrospectedTableTools.java
+4
-17
src/main/java/com/itfsw/mybatis/generator/plugins/utils/JavaElementGeneratorTools.java
...is/generator/plugins/utils/JavaElementGeneratorTools.java
+0
-24
src/main/java/com/itfsw/mybatis/generator/plugins/utils/PluginTools.java
...om/itfsw/mybatis/generator/plugins/utils/PluginTools.java
+1
-4
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java
...tis/generator/plugins/utils/XmlElementGeneratorTools.java
+0
-58
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementTools.java
...tfsw/mybatis/generator/plugins/utils/XmlElementTools.java
+165
-0
src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/InnerTypeFullyQualifiedJavaType.java
...ugins/utils/enhanced/InnerTypeFullyQualifiedJavaType.java
+3
-3
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java
.../mybatis/generator/plugins/utils/hook/HookAggregator.java
+2
-3
src/test/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPluginTest.java
...mybatis/generator/plugins/OptimisticLockerPluginTest.java
+69
-0
src/test/resources/scripts/OptimisticLockerPlugin/init.sql
src/test/resources/scripts/OptimisticLockerPlugin/init.sql
+120
-0
src/test/resources/scripts/OptimisticLockerPlugin/mybatis-generator.xml
...rces/scripts/OptimisticLockerPlugin/mybatis-generator.xml
+61
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java
View file @
331634de
...
...
@@ -335,9 +335,9 @@ public class IncrementsPlugin extends BasePlugin implements IModelBuilderPluginH
private
void
generatedWithSelective
(
XmlElement
element
,
IntrospectedTable
introspectedTable
,
boolean
hasPrefix
)
{
if
(
incTools
.
support
())
{
// 查找 set->if->text
List
<
XmlElement
>
sets
=
XmlElement
Generator
Tools
.
findXmlElements
(
element
,
"set"
);
List
<
XmlElement
>
sets
=
XmlElementTools
.
findXmlElements
(
element
,
"set"
);
if
(
sets
.
size
()
>
0
)
{
List
<
XmlElement
>
ifs
=
XmlElement
Generator
Tools
.
findXmlElements
(
sets
.
get
(
0
),
"if"
);
List
<
XmlElement
>
ifs
=
XmlElementTools
.
findXmlElements
(
sets
.
get
(
0
),
"if"
);
if
(
ifs
.
size
()
>
0
)
{
for
(
XmlElement
xmlElement
:
ifs
)
{
// 下面为if的text节点
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/LogicalDeletePlugin.java
View file @
331634de
...
...
@@ -567,7 +567,7 @@ public class LogicalDeletePlugin extends BasePlugin {
innerClass
.
addMethod
(
method
);
// TODO 过期方法
Method
mAndDeleted
=
JavaElementGeneratorTools
.
clone
Method
(
method
);
Method
mAndDeleted
=
new
Method
(
method
);
mAndDeleted
.
setName
(
"andDeleted"
);
mAndDeleted
.
addAnnotation
(
"@Deprecated"
);
innerClass
.
addMethod
(
mAndDeleted
);
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPlugin.java
View file @
331634de
...
...
@@ -17,10 +17,22 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
org.mybatis.generator.api.GeneratedJavaFile
;
import
com.itfsw.mybatis.generator.plugins.utils.BeanUtils
;
import
com.itfsw.mybatis.generator.plugins.utils.FormatTools
;
import
com.itfsw.mybatis.generator.plugins.utils.XmlElementTools
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.FullyQualifiedJavaType
;
import
org.mybatis.generator.api.dom.java.Interface
;
import
org.mybatis.generator.api.dom.java.Method
;
import
org.mybatis.generator.api.dom.java.Parameter
;
import
org.mybatis.generator.api.dom.xml.*
;
import
org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities
;
import
org.mybatis.generator.internal.util.StringUtility
;
import
java.util.List
;
import
java.util.*
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
/**
* ---------------------------------------------------------------------------
...
...
@@ -31,8 +43,453 @@ import java.util.List;
* ---------------------------------------------------------------------------
*/
public
class
OptimisticLockerPlugin
extends
BasePlugin
{
public
static
final
String
METHOD_DELETE_WITH_VERSION_BY_EXAMPLE
=
"deleteWithVersionByExample"
;
// 方法名
public
static
final
String
METHOD_DELETE_WITH_VERSION_BY_PRIMARY_KEY
=
"deleteWithVersionByPrimaryKey"
;
// 方法名
public
static
final
String
METHOD_UPDATE_WITH_VERSION_BY_EXAMPLE_SELECTIVE
=
"updateWithVersionByExampleSelective"
;
// 方法名
public
static
final
String
METHOD_UPDATE_WITH_VERSION_BY_EXAMPLE_WITH_BLOBS
=
"updateWithVersionByExampleWithBLOBs"
;
// 方法名
public
static
final
String
METHOD_UPDATE_WITH_VERSION_BY_EXAMPLE_WITHOUT_BLOBS
=
"updateWithVersionByExampleWithoutBLOBs"
;
// 方法名
public
static
final
String
METHOD_UPDATE_WITH_VERSION_BY_PRIMARY_KEY_SELECTIVE
=
"updateWithVersionByPrimaryKeySelective"
;
// 方法名
public
static
final
String
METHOD_UPDATE_WITH_VERSION_BY_PRIMARY_KEY_WITH_BLOBS
=
"updateWithVersionByPrimaryKeyWithBLOBs"
;
// 方法名
public
static
final
String
METHOD_UPDATE_WITH_VERSION_BY_PRIMARY_KEY_WITHOUT_BLOBS
=
"updateWithVersionByPrimaryKeyWithoutBLOBs"
;
// 方法名
public
static
final
String
SQL_UPDATE_BY_EXAMPLE_WITH_VERSION_WHERE_CLAUSE
=
"Update_By_Example_With_Version_Where_Clause"
;
public
static
final
String
PRO_VERSION_COLUMN
=
"versionColumn"
;
// 版本列-Key
private
Map
<
IntrospectedTable
,
List
<
XmlElement
>>
sqlMaps
=
new
HashMap
<>();
// sqlMap xml 节点
private
IntrospectedColumn
versionColumn
;
// 版本列
@Override
public
void
initialized
(
IntrospectedTable
introspectedTable
)
{
sqlMaps
.
put
(
introspectedTable
,
new
ArrayList
<>());
// 读取并验证版本列
String
versionColumn
=
introspectedTable
.
getTableConfigurationProperty
(
PRO_VERSION_COLUMN
);
if
(
versionColumn
!=
null
)
{
this
.
versionColumn
=
introspectedTable
.
getColumn
(
versionColumn
);
if
(
this
.
versionColumn
==
null
)
{
warnings
.
add
(
"itfsw(乐观锁插件):表"
+
introspectedTable
.
getFullyQualifiedTable
()
+
"配置的版本列("
+
introspectedTable
.
getTableConfigurationProperty
(
PRO_VERSION_COLUMN
)
+
")没有找到!"
);
}
}
super
.
initialized
(
introspectedTable
);
}
// ========================================= method 生成 ============================================
@Override
public
boolean
clientDeleteByExampleMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceDeleteExampleMethod
(
introspectedTable
,
method
,
interfaze
,
METHOD_DELETE_WITH_VERSION_BY_EXAMPLE
);
}
return
super
.
clientDeleteByExampleMethodGenerated
(
method
,
interfaze
,
introspectedTable
);
}
@Override
public
boolean
clientDeleteByPrimaryKeyMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceDeletePrimaryKeyMethod
(
introspectedTable
,
method
,
interfaze
,
METHOD_DELETE_WITH_VERSION_BY_PRIMARY_KEY
);
}
return
super
.
clientDeleteByPrimaryKeyMethodGenerated
(
method
,
interfaze
,
introspectedTable
);
}
@Override
public
boolean
clientUpdateByExampleSelectiveMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceUpdateExampleMethod
(
introspectedTable
,
method
,
interfaze
,
METHOD_UPDATE_WITH_VERSION_BY_EXAMPLE_SELECTIVE
);
}
return
super
.
clientUpdateByExampleSelectiveMethodGenerated
(
method
,
interfaze
,
introspectedTable
);
}
@Override
public
boolean
clientUpdateByExampleWithBLOBsMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceUpdateExampleMethod
(
introspectedTable
,
method
,
interfaze
,
METHOD_UPDATE_WITH_VERSION_BY_EXAMPLE_WITH_BLOBS
);
}
return
super
.
clientUpdateByExampleWithBLOBsMethodGenerated
(
method
,
interfaze
,
introspectedTable
);
}
@Override
public
boolean
clientUpdateByExampleWithoutBLOBsMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceUpdateExampleMethod
(
introspectedTable
,
method
,
interfaze
,
METHOD_UPDATE_WITH_VERSION_BY_EXAMPLE_WITHOUT_BLOBS
);
}
return
super
.
clientUpdateByExampleWithoutBLOBsMethodGenerated
(
method
,
interfaze
,
introspectedTable
);
}
@Override
public
boolean
clientUpdateByPrimaryKeySelectiveMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceUpdatePrimaryKeyXmlMethod
(
introspectedTable
,
method
,
interfaze
,
METHOD_UPDATE_WITH_VERSION_BY_PRIMARY_KEY_SELECTIVE
);
}
return
super
.
clientUpdateByPrimaryKeySelectiveMethodGenerated
(
method
,
interfaze
,
introspectedTable
);
}
@Override
public
boolean
clientUpdateByPrimaryKeyWithBLOBsMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceUpdatePrimaryKeyXmlMethod
(
introspectedTable
,
method
,
interfaze
,
METHOD_UPDATE_WITH_VERSION_BY_PRIMARY_KEY_WITH_BLOBS
);
}
return
super
.
clientUpdateByPrimaryKeyWithBLOBsMethodGenerated
(
method
,
interfaze
,
introspectedTable
);
}
@Override
public
boolean
clientUpdateByPrimaryKeyWithoutBLOBsMethodGenerated
(
Method
method
,
Interface
interfaze
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceUpdatePrimaryKeyXmlMethod
(
introspectedTable
,
method
,
interfaze
,
METHOD_UPDATE_WITH_VERSION_BY_PRIMARY_KEY_WITHOUT_BLOBS
);
}
return
super
.
clientUpdateByPrimaryKeyWithoutBLOBsMethodGenerated
(
method
,
interfaze
,
introspectedTable
);
}
// ========================================= sqlMap 生成 ============================================
@Override
public
boolean
sqlMapDeleteByExampleElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceExampleXmlElement
(
introspectedTable
,
element
,
METHOD_DELETE_WITH_VERSION_BY_EXAMPLE
);
}
return
super
.
sqlMapDeleteByExampleElementGenerated
(
element
,
introspectedTable
);
}
/**
* @param element
* @param introspectedTable
* @return
* @see org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.DeleteByPrimaryKeyElementGenerator#addElements(XmlElement)
*/
@Override
public
boolean
sqlMapDeleteByPrimaryKeyElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replacePrimaryKeyXmlElement
(
introspectedTable
,
element
,
METHOD_DELETE_WITH_VERSION_BY_PRIMARY_KEY
,
false
);
}
return
super
.
sqlMapDeleteByPrimaryKeyElementGenerated
(
element
,
introspectedTable
);
}
@Override
public
boolean
sqlMapUpdateByExampleSelectiveElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceExampleXmlElement
(
introspectedTable
,
element
,
METHOD_UPDATE_WITH_VERSION_BY_EXAMPLE_SELECTIVE
);
}
return
super
.
sqlMapUpdateByExampleSelectiveElementGenerated
(
element
,
introspectedTable
);
}
@Override
public
boolean
sqlMapUpdateByExampleWithBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceExampleXmlElement
(
introspectedTable
,
element
,
METHOD_UPDATE_WITH_VERSION_BY_EXAMPLE_WITH_BLOBS
);
}
return
super
.
sqlMapUpdateByExampleWithBLOBsElementGenerated
(
element
,
introspectedTable
);
}
@Override
public
boolean
sqlMapUpdateByExampleWithoutBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceExampleXmlElement
(
introspectedTable
,
element
,
METHOD_UPDATE_WITH_VERSION_BY_EXAMPLE_WITHOUT_BLOBS
);
}
return
super
.
sqlMapUpdateByExampleWithoutBLOBsElementGenerated
(
element
,
introspectedTable
);
}
@Override
public
boolean
sqlMapUpdateByPrimaryKeySelectiveElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceUpdateByPrimaryKeyXmlElement
(
introspectedTable
,
element
,
METHOD_UPDATE_WITH_VERSION_BY_PRIMARY_KEY_SELECTIVE
);
}
return
super
.
sqlMapUpdateByPrimaryKeySelectiveElementGenerated
(
element
,
introspectedTable
);
}
@Override
public
boolean
sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceUpdateByPrimaryKeyXmlElement
(
introspectedTable
,
element
,
METHOD_UPDATE_WITH_VERSION_BY_PRIMARY_KEY_WITH_BLOBS
);
}
return
super
.
sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated
(
element
,
introspectedTable
);
}
@Override
public
boolean
sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
this
.
replaceUpdateByPrimaryKeyXmlElement
(
introspectedTable
,
element
,
METHOD_UPDATE_WITH_VERSION_BY_PRIMARY_KEY_WITHOUT_BLOBS
);
}
return
super
.
sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated
(
element
,
introspectedTable
);
}
@Override
public
boolean
sqlMapDocumentGenerated
(
Document
document
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
for
(
XmlElement
element
:
sqlMaps
.
get
(
introspectedTable
))
{
FormatTools
.
addElementWithBestPosition
(
document
.
getRootElement
(),
element
);
}
}
return
super
.
sqlMapDocumentGenerated
(
document
,
introspectedTable
);
}
@Override
public
List
<
GeneratedJavaFile
>
contextGenerateAdditionalJavaFiles
(
IntrospectedTable
introspectedTable
)
{
return
super
.
contextGenerateAdditionalJavaFiles
(
introspectedTable
);
public
boolean
sqlMapExampleWhereClauseElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
if
(
this
.
versionColumn
!=
null
)
{
if
(
XmlElementTools
.
getAttribute
(
element
,
"id"
).
getValue
().
equals
(
"Update_By_Example_Where_Clause"
))
{
XmlElement
withVersionEle
=
new
XmlElement
(
"sql"
);
withVersionEle
.
addAttribute
(
new
Attribute
(
"id"
,
SQL_UPDATE_BY_EXAMPLE_WITH_VERSION_WHERE_CLAUSE
));
commentGenerator
.
addComment
(
withVersionEle
);
// 版本语句
XmlElement
whereEle
=
new
XmlElement
(
"where "
);
withVersionEle
.
addElement
(
whereEle
);
whereEle
.
addElement
(
new
TextElement
(
this
.
generateVersionEleStr
()));
// 附加原生语句
XmlElement
ifEle
=
new
XmlElement
(
"if"
);
whereEle
.
addElement
(
ifEle
);
ifEle
.
addAttribute
(
new
Attribute
(
"test"
,
"example.oredCriteria.size() > 0"
));
// 原生的foreach
XmlElement
foreachEle
=
(
XmlElement
)
XmlElementTools
.
findXmlElements
(
element
,
"where"
).
get
(
0
).
getElements
().
get
(
0
);
ifEle
.
addElement
(
foreachEle
);
foreachEle
.
addAttribute
(
new
Attribute
(
"open"
,
"and ("
));
foreachEle
.
addAttribute
(
new
Attribute
(
"close"
,
")"
));
sqlMaps
.
get
(
introspectedTable
).
add
(
withVersionEle
);
context
.
getPlugins
().
sqlMapExampleWhereClauseElementGenerated
(
withVersionEle
,
introspectedTable
);
}
}
return
super
.
sqlMapExampleWhereClauseElementGenerated
(
element
,
introspectedTable
);
}
// =================================================== 一些生成方法 ========================================
/**
* 生成版本判断节点
* @return
*/
private
String
generateVersionEleStr
()
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
this
.
versionColumn
));
sb
.
append
(
" = "
);
sb
.
append
(
"#{version,jdbcType="
);
sb
.
append
(
this
.
versionColumn
.
getJdbcTypeName
());
if
(
StringUtility
.
stringHasValue
(
this
.
versionColumn
.
getTypeHandler
()))
{
sb
.
append
(
",typeHandler="
);
sb
.
append
(
this
.
versionColumn
.
getTypeHandler
());
}
sb
.
append
(
"}"
);
return
sb
.
toString
();
}
/**
* 替换Example 方法
* @param introspectedTable
* @param method
* @param interfaze
* @param methodName
*/
private
void
replaceUpdateExampleMethod
(
IntrospectedTable
introspectedTable
,
Method
method
,
Interface
interfaze
,
String
methodName
)
{
Method
withVersionMethod
=
new
Method
(
method
);
// 替换方法名
withVersionMethod
.
setName
(
methodName
);
FormatTools
.
replaceGeneralMethodComment
(
commentGenerator
,
withVersionMethod
,
introspectedTable
);
Parameter
versionParam
=
new
Parameter
(
this
.
versionColumn
.
getFullyQualifiedJavaType
(),
"version"
,
"@Param(\"version\")"
);
withVersionMethod
.
addParameter
(
0
,
versionParam
);
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
withVersionMethod
);
}
/**
* 替换Example 方法
* @param introspectedTable
* @param method
* @param interfaze
* @param methodName
*/
private
void
replaceDeleteExampleMethod
(
IntrospectedTable
introspectedTable
,
Method
method
,
Interface
interfaze
,
String
methodName
)
{
Method
withVersionMethod
=
new
Method
(
method
);
// 替换方法名
withVersionMethod
.
setName
(
methodName
);
FormatTools
.
replaceGeneralMethodComment
(
commentGenerator
,
withVersionMethod
,
introspectedTable
);
Parameter
versionParam
=
new
Parameter
(
this
.
versionColumn
.
getFullyQualifiedJavaType
(),
"version"
,
"@Param(\"version\")"
);
Parameter
exampleParam
=
new
Parameter
(
new
FullyQualifiedJavaType
(
introspectedTable
.
getExampleType
()),
"example"
,
"@Param(\"example\")"
);
withVersionMethod
.
getParameters
().
clear
();
withVersionMethod
.
addParameter
(
versionParam
);
withVersionMethod
.
addParameter
(
exampleParam
);
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
withVersionMethod
);
}
/**
* 替换主键 方法
* @param introspectedTable
* @param method
* @param interfaze
* @param methodName
*/
private
void
replaceUpdatePrimaryKeyXmlMethod
(
IntrospectedTable
introspectedTable
,
Method
method
,
Interface
interfaze
,
String
methodName
)
{
Method
withVersionMethod
=
new
Method
(
method
);
// 替换方法名
withVersionMethod
.
setName
(
methodName
);
FormatTools
.
replaceGeneralMethodComment
(
commentGenerator
,
withVersionMethod
,
introspectedTable
);
Parameter
versionParam
=
new
Parameter
(
this
.
versionColumn
.
getFullyQualifiedJavaType
(),
"version"
,
"@Param(\"version\")"
);
Parameter
recordParam
=
new
Parameter
(
method
.
getParameters
().
get
(
0
).
getType
(),
"record"
,
"@Param(\"record\")"
);
withVersionMethod
.
getParameters
().
clear
();
withVersionMethod
.
addParameter
(
versionParam
);
withVersionMethod
.
addParameter
(
recordParam
);
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
withVersionMethod
);
}
/**
* 替换主键 方法
* @param introspectedTable
* @param method
* @param interfaze
* @param methodName
*/
private
void
replaceDeletePrimaryKeyMethod
(
IntrospectedTable
introspectedTable
,
Method
method
,
Interface
interfaze
,
String
methodName
)
{
Method
withVersionMethod
=
new
Method
(
method
);
// 替换方法名
withVersionMethod
.
setName
(
methodName
);
FormatTools
.
replaceGeneralMethodComment
(
commentGenerator
,
withVersionMethod
,
introspectedTable
);
Parameter
versionParam
=
new
Parameter
(
this
.
versionColumn
.
getFullyQualifiedJavaType
(),
"version"
,
"@Param(\"version\")"
);
Parameter
keyParam
=
new
Parameter
(
method
.
getParameters
().
get
(
0
).
getType
(),
"key"
,
"@Param(\"key\")"
);
withVersionMethod
.
getParameters
().
clear
();
withVersionMethod
.
addParameter
(
versionParam
);
withVersionMethod
.
addParameter
(
keyParam
);
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
withVersionMethod
);
}
/**
* updateByPrimaryKeyXXXXX 因为替换了传入参数,使用注解record来赋值
* @param introspectedTable
* @param element
* @param id
* @return
*/
private
XmlElement
replaceUpdateByPrimaryKeyXmlElement
(
IntrospectedTable
introspectedTable
,
XmlElement
element
,
String
id
)
{
XmlElement
withVersionEle
=
XmlElementTools
.
clone
(
element
);
// 查找所有文本节点,替换set操作text xml 节点
for
(
TextElement
textElement
:
XmlElementTools
.
findAllTextElements
(
withVersionEle
))
{
if
(
textElement
.
getContent
().
matches
(
".*#\\{\\w+,.*}.*"
))
{
Matcher
matcher
=
Pattern
.
compile
(
"(.*#\\{)(\\w+,.*}.*)"
).
matcher
(
textElement
.
getContent
());
if
(
matcher
.
find
())
{
String
context
=
matcher
.
group
(
1
)
+
"record."
+
matcher
.
group
(
2
);
try
{
BeanUtils
.
setProperty
(
textElement
,
"content"
,
context
);
}
catch
(
Exception
e
)
{
warnings
.
add
(
"Java反射失败!"
);
}
}
}
}
return
replacePrimaryKeyXmlElement
(
introspectedTable
,
withVersionEle
,
id
,
true
);
}
/**
* 替换Example
* @param introspectedTable
* @param element
* @param id
*/
private
XmlElement
replaceExampleXmlElement
(
IntrospectedTable
introspectedTable
,
XmlElement
element
,
String
id
)
{
XmlElement
withVersionEle
=
XmlElementTools
.
clone
(
element
);
XmlElementTools
.
replaceAttribute
(
withVersionEle
,
new
Attribute
(
"id"
,
id
));
XmlElementTools
.
replaceAttribute
(
withVersionEle
,
new
Attribute
(
"parameterType"
,
"map"
));
FormatTools
.
replaceComment
(
commentGenerator
,
withVersionEle
);
// 替换查询语句
List
<
XmlElement
>
ifEles
=
XmlElementTools
.
findXmlElements
(
withVersionEle
,
"if"
);
for
(
XmlElement
ifEle
:
ifEles
)
{
List
<
XmlElement
>
includeEles
=
XmlElementTools
.
findXmlElements
(
ifEle
,
"include"
);
for
(
XmlElement
includeEle
:
includeEles
)
{
if
(
XmlElementTools
.
getAttribute
(
includeEle
,
"refid"
)
!=
null
)
{
XmlElementTools
.
replaceAttribute
(
includeEle
,
new
Attribute
(
"refid"
,
SQL_UPDATE_BY_EXAMPLE_WITH_VERSION_WHERE_CLAUSE
));
}
}
}
this
.
sqlMaps
.
get
(
introspectedTable
).
add
(
withVersionEle
);
return
withVersionEle
;
}
/**
* 替换 主键查询
* @param introspectedTable
* @param element
* @param id
* @param update
*/
private
XmlElement
replacePrimaryKeyXmlElement
(
IntrospectedTable
introspectedTable
,
XmlElement
element
,
String
id
,
boolean
update
)
{
XmlElement
withVersionEle
=
XmlElementTools
.
clone
(
element
);
XmlElementTools
.
replaceAttribute
(
withVersionEle
,
new
Attribute
(
"id"
,
id
));
XmlElementTools
.
replaceAttribute
(
withVersionEle
,
new
Attribute
(
"parameterType"
,
"map"
));
FormatTools
.
replaceComment
(
commentGenerator
,
withVersionEle
);
// 替换查询语句
Iterator
<
Element
>
elementIterator
=
withVersionEle
.
getElements
().
iterator
();
boolean
flag
=
false
;
while
(
elementIterator
.
hasNext
())
{
Element
ele
=
elementIterator
.
next
();
if
(
ele
instanceof
TextElement
&&
((
TextElement
)
ele
).
getContent
().
matches
(
".*where.*"
))
{
flag
=
true
;
}
if
(
flag
)
{
elementIterator
.
remove
();
}
}
// where 语句
withVersionEle
.
addElement
(
new
TextElement
(
"where "
+
this
.
generateVersionEleStr
()));
if
(
introspectedTable
.
getPrimaryKeyColumns
().
size
()
==
1
)
{
IntrospectedColumn
introspectedColumn
=
introspectedTable
.
getPrimaryKeyColumns
().
get
(
0
);
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
" and "
);
sb
.
append
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
));
sb
.
append
(
" = "
);
if
(
update
)
{
sb
.
append
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
"record."
));
}
else
{
sb
.
append
(
"#{key,jdbcType="
);
sb
.
append
(
introspectedColumn
.
getJdbcTypeName
());
if
(
StringUtility
.
stringHasValue
(
introspectedColumn
.
getTypeHandler
()))
{
sb
.
append
(
",typeHandler="
);
sb
.
append
(
introspectedColumn
.
getTypeHandler
());
}
sb
.
append
(
"}"
);
}
withVersionEle
.
addElement
(
new
TextElement
(
sb
.
toString
()));
}
else
{
for
(
IntrospectedColumn
introspectedColumn
:
introspectedTable
.
getPrimaryKeyColumns
())
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
" and "
);
sb
.
append
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
));
sb
.
append
(
" = "
);
sb
.
append
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
update
?
"record."
:
"key."
));
withVersionEle
.
addElement
(
new
TextElement
(
sb
.
toString
()));
}
}
this
.
sqlMaps
.
get
(
introspectedTable
).
add
(
withVersionEle
);
return
withVersionEle
;
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
View file @
331634de
...
...
@@ -16,9 +16,7 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.PluginTools
;
import
com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools
;
import
com.itfsw.mybatis.generator.plugins.utils.*
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.IIncrementsPluginHook
;
import
com.itfsw.mybatis.generator.plugins.utils.hook.IUpsertPluginHook
;
import
org.mybatis.generator.api.IntrospectedColumn
;
...
...
@@ -82,8 +80,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
FullyQualifiedJavaType
selectiveType
=
new
FullyQualifiedJavaType
(
fullFieldModel
.
getShortName
()
+
"."
+
ModelColumnPlugin
.
ENUM_NAME
);
method
.
addParameter
(
new
Parameter
(
selectiveType
,
"selective"
,
"@Param(\"selective\")"
,
true
));
method
.
getJavaDocLines
().
clear
();
commentGenerator
.
addGeneralMethodComment
(
method
,
introspectedTable
);
FormatTools
.
replaceGeneralMethodComment
(
commentGenerator
,
method
,
introspectedTable
);
return
super
.
clientInsertSelectiveMethodGenerated
(
method
,
interfaze
,
introspectedTable
);
}
...
...
@@ -112,8 +109,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
FullyQualifiedJavaType
selectiveType
=
new
FullyQualifiedJavaType
(
fullFieldModel
.
getShortName
()
+
"."
+
ModelColumnPlugin
.
ENUM_NAME
);
method
.
addParameter
(
new
Parameter
(
selectiveType
,
"selective"
,
"@Param(\"selective\")"
,
true
));
method
.
getJavaDocLines
().
clear
();
commentGenerator
.
addGeneralMethodComment
(
method
,
introspectedTable
);
FormatTools
.
replaceGeneralMethodComment
(
commentGenerator
,
method
,
introspectedTable
);
return
super
.
clientUpdateByExampleSelectiveMethodGenerated
(
method
,
interfaze
,
introspectedTable
);
}
...
...
@@ -144,8 +140,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
FullyQualifiedJavaType
selectiveType
=
new
FullyQualifiedJavaType
(
fullFieldModel
.
getShortName
()
+
"."
+
ModelColumnPlugin
.
ENUM_NAME
);
method
.
addParameter
(
new
Parameter
(
selectiveType
,
"selective"
,
"@Param(\"selective\")"
,
true
));
method
.
getJavaDocLines
().
clear
();
commentGenerator
.
addGeneralMethodComment
(
method
,
introspectedTable
);
FormatTools
.
replaceGeneralMethodComment
(
commentGenerator
,
method
,
introspectedTable
);
return
super
.
clientUpdateByPrimaryKeySelectiveMethodGenerated
(
method
,
interfaze
,
introspectedTable
);
}
...
...
@@ -194,7 +189,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
answer
.
addElement
(
new
TextElement
(
"values"
));
answer
.
addElement
(
this
.
generateInsertValuesSelective
(
ListUtilities
.
removeIdentityAndGeneratedAlwaysColumns
(
introspectedTable
.
getAllColumns
())));
XmlElement
Generator
Tools
.
replaceXmlElement
(
element
,
answer
);
XmlElementTools
.
replaceXmlElement
(
element
,
answer
);
return
super
.
sqlMapInsertSelectiveElementGenerated
(
element
,
introspectedTable
);
}
...
...
@@ -226,7 +221,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
answer
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
XmlElement
Generator
Tools
.
replaceXmlElement
(
element
,
answer
);
XmlElementTools
.
replaceXmlElement
(
element
,
answer
);
return
super
.
sqlMapUpdateByExampleSelectiveElementGenerated
(
element
,
introspectedTable
);
}
...
...
@@ -259,7 +254,7 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
XmlElementGeneratorTools
.
generateWhereByPrimaryKeyTo
(
answer
,
introspectedTable
.
getPrimaryKeyColumns
(),
"record."
);
XmlElement
Generator
Tools
.
replaceXmlElement
(
element
,
answer
);
XmlElementTools
.
replaceXmlElement
(
element
,
answer
);
return
super
.
sqlMapUpdateByPrimaryKeySelectiveElementGenerated
(
element
,
introspectedTable
);
}
...
...
@@ -315,16 +310,16 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
@Override
public
boolean
sqlMapUpsertSelectiveElementGenerated
(
XmlElement
element
,
List
<
IntrospectedColumn
>
columns
,
XmlElement
insertColumnsEle
,
XmlElement
insertValuesEle
,
XmlElement
setsEle
,
IntrospectedTable
introspectedTable
)
{
// parameterType
XmlElement
Generator
Tools
.
replaceAttribute
(
element
,
new
Attribute
(
"parameterType"
,
"map"
));
XmlElementTools
.
replaceAttribute
(
element
,
new
Attribute
(
"parameterType"
,
"map"
));
// 替换insert column
XmlElement
Generator
Tools
.
replaceXmlElement
(
insertColumnsEle
,
this
.
generateInsertColumnSelective
(
columns
));
XmlElementTools
.
replaceXmlElement
(
insertColumnsEle
,
this
.
generateInsertColumnSelective
(
columns
));
// 替换insert values
XmlElement
Generator
Tools
.
replaceXmlElement
(
insertValuesEle
,
this
.
generateInsertValuesSelective
(
columns
));
XmlElementTools
.
replaceXmlElement
(
insertValuesEle
,
this
.
generateInsertValuesSelective
(
columns
));
// 替换update set
XmlElement
Generator
Tools
.
replaceXmlElement
(
setsEle
,
this
.
generateSetsSelective
(
columns
));
XmlElementTools
.
replaceXmlElement
(
setsEle
,
this
.
generateSetsSelective
(
columns
));
return
true
;
}
...
...
@@ -343,13 +338,13 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
public
boolean
sqlMapUpsertByExampleSelectiveElementGenerated
(
XmlElement
element
,
List
<
IntrospectedColumn
>
columns
,
XmlElement
insertColumnsEle
,
XmlElement
insertValuesEle
,
XmlElement
setsEle
,
IntrospectedTable
introspectedTable
)
{
// 替换insert column
XmlElement
Generator
Tools
.
replaceXmlElement
(
insertColumnsEle
,
this
.
generateInsertColumnSelective
(
columns
));
XmlElementTools
.
replaceXmlElement
(
insertColumnsEle
,
this
.
generateInsertColumnSelective
(
columns
));
// 替换insert values
XmlElement
Generator
Tools
.
replaceXmlElement
(
insertValuesEle
,
this
.
generateInsertValuesSelective
(
columns
,
false
));
XmlElementTools
.
replaceXmlElement
(
insertValuesEle
,
this
.
generateInsertValuesSelective
(
columns
,
false
));
// 替换update set
XmlElement
Generator
Tools
.
replaceXmlElement
(
setsEle
,
this
.
generateSetsSelective
(
columns
));
XmlElementTools
.
replaceXmlElement
(
setsEle
,
this
.
generateSetsSelective
(
columns
));
return
true
;
}
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/BasePlugin.java
View file @
331634de
...
...
@@ -28,7 +28,6 @@ import org.mybatis.generator.internal.util.StringUtility;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.lang.reflect.Field
;
import
java.util.List
;
/**
...
...
@@ -77,9 +76,7 @@ public class BasePlugin extends PluginAdapter {
// 先执行一次生成CommentGenerator操作,然后再替换
context
.
getCommentGenerator
();
Field
field
=
Context
.
class
.
getDeclaredField
(
"commentGenerator"
);
field
.
setAccessible
(
true
);
field
.
set
(
context
,
templateCommentGenerator
);
BeanUtils
.
setProperty
(
context
,
"commentGenerator"
,
templateCommentGenerator
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"反射异常"
,
e
);
}
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/BeanUtils.java
0 → 100644
View file @
331634de
/*
* 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.
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.Method
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2018/5/3 18:39
* ---------------------------------------------------------------------------
*/
public
class
BeanUtils
{
/**
* 设置属性
* @param bean
* @param name
* @param value
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
public
static
void
setProperty
(
final
Object
bean
,
final
String
name
,
final
Object
value
)
throws
NoSuchFieldException
,
IllegalAccessException
{
Field
field
=
bean
.
getClass
().
getDeclaredField
(
name
);
field
.
setAccessible
(
true
);
field
.
set
(
bean
,
value
);
}
/**
* 获取属性
* @param bean
* @param name
* @return
*/
public
static
Object
getProperty
(
final
Object
bean
,
final
String
name
)
throws
NoSuchFieldException
,
IllegalAccessException
{
Field
field
=
bean
.
getClass
().
getDeclaredField
(
name
);
field
.
setAccessible
(
true
);
return
field
.
get
(
bean
);
}
/**
* 执行无参方法
* @param bean
* @param name
* @return
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public
static
Object
invoke
(
final
Object
bean
,
final
String
name
)
throws
NoSuchMethodException
,
InvocationTargetException
,
IllegalAccessException
{
Method
method
=
bean
.
getClass
().
getDeclaredMethod
(
name
);
method
.
setAccessible
(
true
);
return
method
.
invoke
(
bean
);
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/FormatTools.java
View file @
331634de
...
...
@@ -16,14 +16,18 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
;
import
org.mybatis.generator.api.CommentGenerator
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.InnerClass
;
import
org.mybatis.generator.api.dom.java.Interface
;
import
org.mybatis.generator.api.dom.java.Method
;
import
org.mybatis.generator.api.dom.java.TopLevelClass
;
import
org.mybatis.generator.api.dom.xml.Attribute
;
import
org.mybatis.generator.api.dom.xml.Element
;
import
org.mybatis.generator.api.dom.xml.TextElement
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
java.util.Iterator
;
import
java.util.List
;
/**
...
...
@@ -37,80 +41,88 @@ import java.util.List;
public
class
FormatTools
{
/**
* 在最佳位置添加方法
*
* @param innerClass
* @param method
*/
public
static
void
addMethodWithBestPosition
(
InnerClass
innerClass
,
Method
method
){
public
static
void
addMethodWithBestPosition
(
InnerClass
innerClass
,
Method
method
)
{
addMethodWithBestPosition
(
method
,
innerClass
.
getMethods
());
}
/**
* 在最佳位置添加方法
*
* @param interfacz
* @param method
*/
public
static
void
addMethodWithBestPosition
(
Interface
interfacz
,
Method
method
){
public
static
void
addMethodWithBestPosition
(
Interface
interfacz
,
Method
method
)
{
addMethodWithBestPosition
(
method
,
interfacz
.
getMethods
());
}
/**
* 在最佳位置添加方法
*
* @param topLevelClass
* @param method
*/
public
static
void
addMethodWithBestPosition
(
TopLevelClass
topLevelClass
,
Method
method
){
public
static
void
addMethodWithBestPosition
(
TopLevelClass
topLevelClass
,
Method
method
)
{
addMethodWithBestPosition
(
method
,
topLevelClass
.
getMethods
());
}
/**
* 在最佳位置添加节点
*
* @param rootElement
* @param element
*/
public
static
void
addElementWithBestPosition
(
XmlElement
rootElement
,
XmlElement
element
){
String
id
=
getIdFromElement
(
element
);
if
(
id
==
null
){
rootElement
.
addElement
(
element
);
public
static
void
addElementWithBestPosition
(
XmlElement
rootElement
,
XmlElement
element
)
{
// sql 元素都放在sql后面
if
(
element
.
getName
().
equals
(
"sql"
))
{
int
index
=
0
;
for
(
Element
ele
:
rootElement
.
getElements
())
{
if
(
ele
instanceof
XmlElement
&&
((
XmlElement
)
ele
).
getName
().
equals
(
"sql"
))
{
index
++;
}
}
rootElement
.
addElement
(
index
,
element
);
}
else
{
List
<
Element
>
elements
=
rootElement
.
getElements
();
int
index
=
-
1
;
for
(
int
i
=
0
;
i
<
elements
.
size
();
i
++){
Element
ele
=
elements
.
get
(
i
);
if
(
ele
instanceof
XmlElement
){
String
eleId
=
getIdFromElement
((
XmlElement
)
ele
);
if
(
eleId
!=
null
){
if
(
eleId
.
startsWith
(
id
)){
if
(
index
==
-
1
){
index
=
i
;
// 根据id 排序
String
id
=
getIdFromElement
(
element
);
if
(
id
==
null
)
{
rootElement
.
addElement
(
element
);
}
else
{
List
<
Element
>
elements
=
rootElement
.
getElements
();
int
index
=
-
1
;
for
(
int
i
=
0
;
i
<
elements
.
size
();
i
++)
{
Element
ele
=
elements
.
get
(
i
);
if
(
ele
instanceof
XmlElement
)
{
String
eleId
=
getIdFromElement
((
XmlElement
)
ele
);
if
(
eleId
!=
null
)
{
if
(
eleId
.
startsWith
(
id
))
{
if
(
index
==
-
1
)
{
index
=
i
;
}
}
else
if
(
id
.
startsWith
(
eleId
))
{
index
=
i
+
1
;
}
}
else
if
(
id
.
startsWith
(
eleId
)){
index
=
i
+
1
;
}
}
}
}
if
(
index
==
-
1
||
index
>=
elements
.
size
()){
rootElement
.
addElement
(
element
);
}
else
{
elements
.
add
(
index
,
element
);
if
(
index
==
-
1
||
index
>=
elements
.
size
())
{
rootElement
.
addElement
(
element
);
}
else
{
elements
.
add
(
index
,
element
);
}
}
}
}
/**
* 找出节点ID值
*
* @param element
* @return
*/
private
static
String
getIdFromElement
(
XmlElement
element
){
for
(
Attribute
attribute
:
element
.
getAttributes
()){
if
(
attribute
.
getName
().
equals
(
"id"
)){
private
static
String
getIdFromElement
(
XmlElement
element
)
{
for
(
Attribute
attribute
:
element
.
getAttributes
())
{
if
(
attribute
.
getName
().
equals
(
"id"
))
{
return
attribute
.
getValue
();
}
}
...
...
@@ -119,33 +131,74 @@ public class FormatTools {
/**
* 获取最佳添加位置
*
* @param method
* @param methods
* @return
*/
private
static
void
addMethodWithBestPosition
(
Method
method
,
List
<
Method
>
methods
){
private
static
void
addMethodWithBestPosition
(
Method
method
,
List
<
Method
>
methods
)
{
int
index
=
-
1
;
for
(
int
i
=
0
;
i
<
methods
.
size
();
i
++){
for
(
int
i
=
0
;
i
<
methods
.
size
();
i
++)
{
Method
m
=
methods
.
get
(
i
);
if
(
m
.
getName
().
equals
(
method
.
getName
())){
if
(
m
.
getParameters
().
size
()
<=
method
.
getParameters
().
size
()){
if
(
m
.
getName
().
equals
(
method
.
getName
()))
{
if
(
m
.
getParameters
().
size
()
<=
method
.
getParameters
().
size
())
{
index
=
i
+
1
;
}
else
{
index
=
i
;
}
}
else
if
(
m
.
getName
().
startsWith
(
method
.
getName
())){
if
(
index
==
-
1
)
{
}
else
if
(
m
.
getName
().
startsWith
(
method
.
getName
()))
{
if
(
index
==
-
1
)
{
index
=
i
;
}
}
else
if
(
method
.
getName
().
startsWith
(
m
.
getName
())){
}
else
if
(
method
.
getName
().
startsWith
(
m
.
getName
()))
{
index
=
i
+
1
;
}
}
if
(
index
==
-
1
||
index
>=
methods
.
size
()){
if
(
index
==
-
1
||
index
>=
methods
.
size
())
{
methods
.
add
(
methods
.
size
(),
method
);
}
else
{
methods
.
add
(
index
,
method
);
}
}
/**
* 替换已有方法注释
* @param commentGenerator
* @param method
* @param introspectedTable
*/
public
static
void
replaceGeneralMethodComment
(
CommentGenerator
commentGenerator
,
Method
method
,
IntrospectedTable
introspectedTable
)
{
method
.
getJavaDocLines
().
clear
();
commentGenerator
.
addGeneralMethodComment
(
method
,
introspectedTable
);
}
/**
* 替换已有注释
* @param commentGenerator
* @param element
*/
public
static
void
replaceComment
(
CommentGenerator
commentGenerator
,
XmlElement
element
)
{
Iterator
<
Element
>
elementIterator
=
element
.
getElements
().
iterator
();
boolean
flag
=
false
;
while
(
elementIterator
.
hasNext
())
{
Element
ele
=
elementIterator
.
next
();
if
(
ele
instanceof
TextElement
&&
((
TextElement
)
ele
).
getContent
().
matches
(
"<!--"
))
{
flag
=
true
;
}
if
(
flag
)
{
elementIterator
.
remove
();
}
if
(
ele
instanceof
TextElement
&&
((
TextElement
)
ele
).
getContent
().
matches
(
"-->"
))
{
flag
=
false
;
}
}
XmlElement
tmpEle
=
new
XmlElement
(
"tmp"
);
commentGenerator
.
addComment
(
tmpEle
);
for
(
int
i
=
tmpEle
.
getElements
().
size
()
-
1
;
i
>=
0
;
i
--)
{
element
.
addElement
(
0
,
tmpEle
.
getElements
().
get
(
i
));
}
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/IntrospectedTableTools.java
View file @
331634de
...
...
@@ -17,7 +17,6 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
;
import
com.itfsw.mybatis.generator.plugins.ExampleTargetPlugin
;
import
org.mybatis.generator.api.FullyQualifiedTable
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.config.Context
;
...
...
@@ -25,9 +24,7 @@ import org.mybatis.generator.config.JavaModelGeneratorConfiguration;
import
org.mybatis.generator.config.PluginConfiguration
;
import
org.mybatis.generator.internal.util.StringUtility
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.Method
;
/**
* ---------------------------------------------------------------------------
...
...
@@ -52,22 +49,12 @@ public class IntrospectedTableTools {
introspectedTable
.
getTableConfiguration
().
setDomainObjectName
(
domainObjectName
);
// FullyQualifiedTable修正
Field
domainObjectNameField
=
FullyQualifiedTable
.
class
.
getDeclaredField
(
"domainObjectName"
);
domainObjectNameField
.
setAccessible
(
true
);
domainObjectNameField
.
set
(
introspectedTable
.
getFullyQualifiedTable
(),
domainObjectName
);
BeanUtils
.
setProperty
(
introspectedTable
.
getFullyQualifiedTable
(),
"domainObjectName"
,
domainObjectName
);
// 重新修正introspectedTable属性信息
Method
calculateJavaClientAttributes
=
IntrospectedTable
.
class
.
getDeclaredMethod
(
"calculateJavaClientAttributes"
);
calculateJavaClientAttributes
.
setAccessible
(
true
);
calculateJavaClientAttributes
.
invoke
(
introspectedTable
);
Method
calculateModelAttributes
=
IntrospectedTable
.
class
.
getDeclaredMethod
(
"calculateModelAttributes"
);
calculateModelAttributes
.
setAccessible
(
true
);
calculateModelAttributes
.
invoke
(
introspectedTable
);
Method
calculateXmlAttributes
=
IntrospectedTable
.
class
.
getDeclaredMethod
(
"calculateXmlAttributes"
);
calculateXmlAttributes
.
setAccessible
(
true
);
calculateXmlAttributes
.
invoke
(
introspectedTable
);
BeanUtils
.
invoke
(
introspectedTable
,
"calculateJavaClientAttributes"
);
BeanUtils
.
invoke
(
introspectedTable
,
"calculateModelAttributes"
);
BeanUtils
.
invoke
(
introspectedTable
,
"calculateXmlAttributes"
);
// 注意!! 如果配置了ExampleTargetPlugin插件,要修正Example 位置
PluginConfiguration
configuration
=
PluginTools
.
getPluginConfiguration
(
context
,
ExampleTargetPlugin
.
class
);
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/JavaElementGeneratorTools.java
View file @
331634de
...
...
@@ -163,28 +163,4 @@ public class JavaElementGeneratorTools {
}
return
type
;
}
/**
* 克隆方法
* @param method
*/
public
static
Method
cloneMethod
(
Method
method
)
{
Method
result
=
new
Method
();
result
.
setConstructor
(
method
.
isConstructor
());
result
.
setFinal
(
method
.
isFinal
());
result
.
setName
(
method
.
getName
());
result
.
setNative
(
method
.
isNative
());
result
.
setReturnType
(
method
.
getReturnType
());
result
.
setSynchronized
(
method
.
isSynchronized
());
result
.
setStatic
(
method
.
isStatic
());
result
.
setVisibility
(
method
.
getVisibility
());
for
(
Parameter
parameter
:
method
.
getParameters
())
{
result
.
addParameter
(
parameter
);
}
for
(
String
docLine
:
method
.
getJavaDocLines
()){
result
.
addJavaDocLine
(
docLine
);
}
result
.
addBodyLines
(
method
.
getBodyLines
());
return
result
;
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/PluginTools.java
View file @
331634de
...
...
@@ -22,7 +22,6 @@ import org.mybatis.generator.config.PluginConfiguration;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.lang.reflect.Field
;
import
java.util.ArrayList
;
import
java.util.List
;
...
...
@@ -83,9 +82,7 @@ public class PluginTools {
public
static
List
<
PluginConfiguration
>
getConfigPlugins
(
Context
ctx
)
{
try
{
// 利用反射获取pluginConfigurations属性
Field
field
=
Context
.
class
.
getDeclaredField
(
"pluginConfigurations"
);
field
.
setAccessible
(
true
);
return
(
List
<
PluginConfiguration
>)
field
.
get
(
ctx
);
return
(
List
<
PluginConfiguration
>)
BeanUtils
.
getProperty
(
ctx
,
"pluginConfigurations"
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"插件检查反射异常"
,
e
);
}
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java
View file @
331634de
...
...
@@ -134,44 +134,6 @@ public class XmlElementGeneratorTools {
}
}
/**
* 移除属性
* @param element
* @param name
*/
public
static
void
removeAttribute
(
XmlElement
element
,
String
name
)
{
Iterator
<
Attribute
>
iterator
=
element
.
getAttributes
().
iterator
();
while
(
iterator
.
hasNext
())
{
Attribute
attribute
=
iterator
.
next
();
if
(
attribute
.
getName
().
equals
(
name
))
{
iterator
.
remove
();
}
}
}
/**
* 替换属性
* @param element
* @param attribute
*/
public
static
void
replaceAttribute
(
XmlElement
element
,
Attribute
attribute
)
{
removeAttribute
(
element
,
attribute
.
getName
());
element
.
addAttribute
(
attribute
);
}
/**
* xmlElement 替换
* @param srcEle
* @param destEle
*/
public
static
void
replaceXmlElement
(
XmlElement
srcEle
,
XmlElement
destEle
)
{
srcEle
.
setName
(
destEle
.
getName
());
srcEle
.
getAttributes
().
clear
();
srcEle
.
getAttributes
().
addAll
(
destEle
.
getAttributes
());
srcEle
.
getElements
().
clear
();
srcEle
.
getElements
().
addAll
(
destEle
.
getElements
());
}
/**
* 生成keys Ele
* @param columns
...
...
@@ -517,26 +479,6 @@ public class XmlElementGeneratorTools {
}
}
/**
* 查找指定xml节点下指定节点名称的元素
* @param xmlElement
* @param name
* @return
*/
public
static
List
<
XmlElement
>
findXmlElements
(
XmlElement
xmlElement
,
String
name
)
{
List
<
XmlElement
>
list
=
new
ArrayList
<>();
List
<
Element
>
elements
=
xmlElement
.
getElements
();
for
(
Element
ele
:
elements
)
{
if
(
ele
instanceof
XmlElement
)
{
XmlElement
xmlElement1
=
(
XmlElement
)
ele
;
if
(
name
.
equalsIgnoreCase
(
xmlElement1
.
getName
()))
{
list
.
add
(
xmlElement1
);
}
}
}
return
list
;
}
/**
* 生成 xxxByPrimaryKey 的where 语句
* @param element
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementTools.java
0 → 100644
View file @
331634de
/*
* 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.
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
;
import
org.mybatis.generator.api.dom.xml.Attribute
;
import
org.mybatis.generator.api.dom.xml.Element
;
import
org.mybatis.generator.api.dom.xml.TextElement
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
java.util.ArrayList
;
import
java.util.Iterator
;
import
java.util.List
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2018/5/2 17:52
* ---------------------------------------------------------------------------
*/
public
class
XmlElementTools
{
/**
* 获取属性
* @param element
* @param name
*/
public
static
Attribute
getAttribute
(
XmlElement
element
,
String
name
)
{
Iterator
<
Attribute
>
iterator
=
element
.
getAttributes
().
iterator
();
while
(
iterator
.
hasNext
())
{
Attribute
attribute
=
iterator
.
next
();
if
(
attribute
.
getName
().
equals
(
name
))
{
return
attribute
;
}
}
return
null
;
}
/**
* 移除属性
* @param element
* @param name
*/
public
static
void
removeAttribute
(
XmlElement
element
,
String
name
)
{
Iterator
<
Attribute
>
iterator
=
element
.
getAttributes
().
iterator
();
while
(
iterator
.
hasNext
())
{
Attribute
attribute
=
iterator
.
next
();
if
(
attribute
.
getName
().
equals
(
name
))
{
iterator
.
remove
();
}
}
}
/**
* 替换属性
* @param element
* @param attribute
*/
public
static
void
replaceAttribute
(
XmlElement
element
,
Attribute
attribute
)
{
removeAttribute
(
element
,
attribute
.
getName
());
element
.
addAttribute
(
attribute
);
}
/**
* xmlElement 替换
* @param srcEle
* @param destEle
*/
public
static
void
replaceXmlElement
(
XmlElement
srcEle
,
XmlElement
destEle
)
{
srcEle
.
setName
(
destEle
.
getName
());
srcEle
.
getAttributes
().
clear
();
srcEle
.
getAttributes
().
addAll
(
destEle
.
getAttributes
());
srcEle
.
getElements
().
clear
();
srcEle
.
getElements
().
addAll
(
destEle
.
getElements
());
}
/**
* 查找指定xml节点下指定节点名称的元素
* @param xmlElement
* @param name
* @return
*/
public
static
List
<
XmlElement
>
findXmlElements
(
XmlElement
xmlElement
,
String
name
)
{
List
<
XmlElement
>
list
=
new
ArrayList
<>();
List
<
Element
>
elements
=
xmlElement
.
getElements
();
for
(
Element
ele
:
elements
)
{
if
(
ele
instanceof
XmlElement
)
{
XmlElement
xmlElement1
=
(
XmlElement
)
ele
;
if
(
name
.
equalsIgnoreCase
(
xmlElement1
.
getName
()))
{
list
.
add
(
xmlElement1
);
}
}
}
return
list
;
}
/**
* 查询指定xml下所有text xml 节点
* @param xmlElement
* @return
*/
public
static
List
<
TextElement
>
findAllTextElements
(
XmlElement
xmlElement
){
List
<
TextElement
>
textElements
=
new
ArrayList
<>();
for
(
Element
element
:
xmlElement
.
getElements
()){
if
(
element
instanceof
XmlElement
){
textElements
.
addAll
(
findAllTextElements
((
XmlElement
)
element
));
}
else
if
(
element
instanceof
TextElement
){
textElements
.
add
((
TextElement
)
element
);
}
}
return
textElements
;
}
/**
* 拷贝
* @param element
* @return
*/
public
static
XmlElement
clone
(
XmlElement
element
)
{
XmlElement
destEle
=
new
XmlElement
(
element
.
getName
());
for
(
Attribute
attribute
:
element
.
getAttributes
())
{
destEle
.
addAttribute
(
XmlElementTools
.
clone
(
attribute
));
}
for
(
Element
ele
:
element
.
getElements
())
{
if
(
ele
instanceof
XmlElement
)
{
destEle
.
addElement
(
XmlElementTools
.
clone
((
XmlElement
)
ele
));
}
else
if
(
ele
instanceof
TextElement
)
{
destEle
.
addElement
(
XmlElementTools
.
clone
((
TextElement
)
ele
));
}
}
return
destEle
;
}
/**
* 拷贝
* @param attribute
* @return
*/
public
static
Attribute
clone
(
Attribute
attribute
)
{
return
new
Attribute
(
attribute
.
getName
(),
attribute
.
getValue
());
}
/**
* 拷贝
* @param textElement
* @return
*/
public
static
TextElement
clone
(
TextElement
textElement
)
{
return
new
TextElement
(
textElement
.
getContent
());
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/InnerTypeFullyQualifiedJavaType.java
View file @
331634de
...
...
@@ -16,6 +16,7 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
.
enhanced
;
import
com.itfsw.mybatis.generator.plugins.utils.BeanUtils
;
import
org.mybatis.generator.api.dom.java.FullyQualifiedJavaType
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
...
...
@@ -40,10 +41,9 @@ public class InnerTypeFullyQualifiedJavaType extends FullyQualifiedJavaType {
try
{
// 修正package
java
.
lang
.
reflect
.
Field
packageName
=
this
.
getClass
().
getSuperclass
().
getDeclaredField
(
"packageName"
);
packageName
.
setAccessible
(
true
);
String
oldPackageName
=
getPackageName
();
packageName
.
set
(
this
,
oldPackageName
.
substring
(
0
,
oldPackageName
.
lastIndexOf
(
"."
)));
BeanUtils
.
setProperty
(
this
,
"packageName"
,
oldPackageName
.
substring
(
0
,
oldPackageName
.
lastIndexOf
(
"."
)));
outerType
=
oldPackageName
.
substring
(
oldPackageName
.
lastIndexOf
(
"."
)
+
1
);
}
catch
(
Exception
e
){
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java
View file @
331634de
...
...
@@ -17,6 +17,7 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
.
hook
;
import
com.itfsw.mybatis.generator.plugins.utils.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.BeanUtils
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.Plugin
;
...
...
@@ -80,9 +81,7 @@ public class HookAggregator implements IUpsertPluginHook, IModelBuilderPluginHoo
List
list
=
new
ArrayList
();
// 反射获取插件列表,不能用单例去弄,不然因为类释放的问题而导致测试用例出问题
try
{
java
.
lang
.
reflect
.
Field
field
=
this
.
context
.
getPlugins
().
getClass
().
getDeclaredField
(
"plugins"
);
field
.
setAccessible
(
true
);
List
<
Plugin
>
plugins
=
(
List
<
Plugin
>)
field
.
get
(
this
.
context
.
getPlugins
());
List
<
Plugin
>
plugins
=
(
List
<
Plugin
>)
BeanUtils
.
getProperty
(
this
.
context
.
getPlugins
(),
"plugins"
);
for
(
Plugin
plugin
:
plugins
)
{
if
(
clazz
.
isInstance
(
plugin
))
{
list
.
add
(
plugin
);
...
...
src/test/java/com/itfsw/mybatis/generator/plugins/OptimisticLockerPluginTest.java
0 → 100644
View file @
331634de
/*
* 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.
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.tools.*
;
import
org.apache.ibatis.session.SqlSession
;
import
org.junit.Assert
;
import
org.junit.BeforeClass
;
import
org.junit.Test
;
import
java.io.IOException
;
import
java.sql.SQLException
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2018/5/2 17:05
* ---------------------------------------------------------------------------
*/
public
class
OptimisticLockerPluginTest
{
/**
* 初始化数据库
*/
@BeforeClass
public
static
void
init
()
throws
SQLException
,
IOException
,
ClassNotFoundException
{
DBHelper
.
createDB
(
"scripts/OptimisticLockerPlugin/init.sql"
);
}
/**
* 测试 updateWithVersionByExampleSelective
*/
@Test
public
void
testUpdateWithVersionByExampleSelective
()
throws
Exception
{
MyBatisGeneratorTool
tool
=
MyBatisGeneratorTool
.
create
(
"scripts/OptimisticLockerPlugin/mybatis-generator.xml"
);
tool
.
generate
(
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
orCriteria
=
new
ObjectUtil
(
tbExample
.
invoke
(
"or"
));
orCriteria
.
invoke
(
"andField1EqualTo"
,
"ts1"
);
// sql
String
sql
=
SqlHelper
.
getFormatMapperSql
(
tbMapper
.
getObject
(),
"deleteWithVersionByExample"
,
1L
,
tbExample
.
getObject
());
Assert
.
assertEquals
(
sql
,
"update tb SET field_1 = 'null' , inc_f2 = 5 WHERE ( id = '1' )"
);
}
});
}
}
\ No newline at end of file
src/test/resources/scripts/OptimisticLockerPlugin/init.sql
0 → 100644
View file @
331634de
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50617
Source Host : localhost:3306
Source Database : mybatis-generator-plugin
Target Server Type : MYSQL
Target Server Version : 50617
File Encoding : 65001
Date: 2017-07-05 17:21:41
*/
SET
FOREIGN_KEY_CHECKS
=
0
;
-- ----------------------------
-- Table structure for tb
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb`
;
CREATE
TABLE
`tb`
(
`id`
bigint
(
20
)
NOT
NULL
COMMENT
'注释1'
,
`field1`
varchar
(
255
)
DEFAULT
NULL
COMMENT
'注释2'
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
DEFAULT
'0'
,
`inc_f3`
bigint
(
20
)
DEFAULT
'0'
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
AUTO_INCREMENT
=
5
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb
-- ----------------------------
INSERT
INTO
`tb`
VALUES
(
'1'
,
'fd1'
,
'0'
,
'0'
,
'0'
);
INSERT
INTO
`tb`
VALUES
(
'2'
,
'fd2'
,
'1'
,
'2'
,
'3'
);
INSERT
INTO
`tb`
VALUES
(
'3'
,
null
,
'3'
,
'2'
,
'1'
);
INSERT
INTO
`tb`
VALUES
(
'4'
,
'fd3'
,
'1'
,
'1'
,
'1'
);
-- ----------------------------
-- Table structure for tb_blobs
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb_blobs`
;
CREATE
TABLE
`tb_blobs`
(
`id`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
COMMENT
'注释1'
,
`field1`
varchar
(
255
)
DEFAULT
NULL
,
`field2`
longtext
COMMENT
'注释2'
,
`field3`
longtext
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f3`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
AUTO_INCREMENT
=
5
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb_blobs
-- ----------------------------
INSERT
INTO
`tb_blobs`
VALUES
(
'1'
,
'fd1'
,
null
,
null
,
'1'
,
'2'
,
'3'
);
INSERT
INTO
`tb_blobs`
VALUES
(
'2'
,
null
,
'fd2'
,
null
,
'3'
,
'2'
,
'1'
);
INSERT
INTO
`tb_blobs`
VALUES
(
'3'
,
null
,
null
,
'fd3'
,
'1'
,
'1'
,
'1'
);
INSERT
INTO
`tb_blobs`
VALUES
(
'4'
,
'fd4'
,
'fd5'
,
'fd6'
,
'0'
,
'0'
,
'0'
);
-- ----------------------------
-- Table structure for tb_keys
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb_keys`
;
CREATE
TABLE
`tb_keys`
(
`key1`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
COMMENT
'注释1'
,
`key2`
varchar
(
255
)
NOT
NULL
,
`field1`
varchar
(
255
)
DEFAULT
NULL
COMMENT
'注释2'
,
`field2`
int
(
11
)
DEFAULT
NULL
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f3`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
PRIMARY
KEY
(
`key1`
,
`key2`
)
)
ENGINE
=
InnoDB
AUTO_INCREMENT
=
5
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb_keys
-- ----------------------------
INSERT
INTO
`tb_keys`
VALUES
(
'1'
,
'k1'
,
'fd1'
,
null
,
'1'
,
'2'
,
'3'
);
INSERT
INTO
`tb_keys`
VALUES
(
'2'
,
'k2'
,
null
,
'2'
,
'3'
,
'2'
,
'1'
);
INSERT
INTO
`tb_keys`
VALUES
(
'3'
,
'k3'
,
null
,
null
,
'1'
,
'1'
,
'1'
);
-- ----------------------------
-- Table structure for tb_single_blob
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb_single_blob`
;
CREATE
TABLE
`tb_single_blob`
(
`id`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
COMMENT
'注释1'
,
`field1`
longtext
COMMENT
'注释2'
,
`field2`
int
(
11
)
DEFAULT
NULL
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f2`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`inc_f3`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
AUTO_INCREMENT
=
4
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb_single_blob
-- ----------------------------
INSERT
INTO
`tb_single_blob`
VALUES
(
'1'
,
'fd1'
,
'0'
,
'1'
,
'2'
,
'3'
);
INSERT
INTO
`tb_single_blob`
VALUES
(
'2'
,
null
,
null
,
'3'
,
'2'
,
'1'
);
INSERT
INTO
`tb_single_blob`
VALUES
(
'3'
,
null
,
null
,
'1'
,
'1'
,
'1'
);
-- ----------------------------
-- Table structure for tb_key_word
-- ----------------------------
DROP
TABLE
IF
EXISTS
`tb_key_word`
;
CREATE
TABLE
`tb_key_word`
(
`id`
bigint
(
20
)
NOT
NULL
AUTO_INCREMENT
COMMENT
'注释1'
,
`field2`
int
(
11
)
DEFAULT
NULL
,
`inc_f1`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
`update`
bigint
(
20
)
NOT
NULL
DEFAULT
'0'
,
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
AUTO_INCREMENT
=
4
DEFAULT
CHARSET
=
utf8
;
-- ----------------------------
-- Records of tb_key_word
-- ----------------------------
INSERT
INTO
`tb_key_word`
VALUES
(
'1'
,
'0'
,
'0'
,
'1'
);
\ No newline at end of file
src/test/resources/scripts/OptimisticLockerPlugin/mybatis-generator.xml
0 → 100644
View file @
331634de
<?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"
/>
<!--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"
/>
<generatedKey
column=
"id"
sqlStatement=
"MySql"
identity=
"true"
/>
</table>
<table
tableName=
"tb_keys"
>
<property
name=
"versionColumn"
value=
"inc_f1"
/>
</table>
<table
tableName=
"tb_blobs"
>
<property
name=
"versionColumn"
value=
"inc_f1"
/>
</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