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
09065650
Commit
09065650
authored
Apr 28, 2018
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重构代码插件依赖关系
parent
c33616c4
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
164 additions
and
73 deletions
+164
-73
src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
...sw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
+26
-4
src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
...ava/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
+2
-2
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java
...tis/generator/plugins/utils/XmlElementGeneratorTools.java
+136
-67
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
View file @
09065650
...
@@ -339,7 +339,17 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
...
@@ -339,7 +339,17 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
*/
*/
@Override
@Override
public
boolean
sqlMapUpsertByExampleSelectiveElementGenerated
(
XmlElement
element
,
List
<
IntrospectedColumn
>
columns
,
XmlElement
insertColumnsEle
,
XmlElement
insertValuesEle
,
XmlElement
setsEle
,
IntrospectedTable
introspectedTable
)
{
public
boolean
sqlMapUpsertByExampleSelectiveElementGenerated
(
XmlElement
element
,
List
<
IntrospectedColumn
>
columns
,
XmlElement
insertColumnsEle
,
XmlElement
insertValuesEle
,
XmlElement
setsEle
,
IntrospectedTable
introspectedTable
)
{
return
false
;
// 替换insert column
XmlElementGeneratorTools
.
replaceXmlElement
(
insertColumnsEle
,
this
.
generateInsertColumnSelective
(
columns
));
// 替换insert values
XmlElementGeneratorTools
.
replaceXmlElement
(
insertValuesEle
,
this
.
generateInsertValuesSelective
(
columns
,
false
));
// 替换update set
XmlElementGeneratorTools
.
replaceXmlElement
(
setsEle
,
this
.
generateSetsSelective
(
columns
));
return
true
;
}
}
// ====================================================== 一些私有节点生成方法 =========================================================
// ====================================================== 一些私有节点生成方法 =========================================================
...
@@ -384,6 +394,16 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
...
@@ -384,6 +394,16 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
* @return
* @return
*/
*/
private
XmlElement
generateInsertValuesSelective
(
List
<
IntrospectedColumn
>
columns
)
{
private
XmlElement
generateInsertValuesSelective
(
List
<
IntrospectedColumn
>
columns
)
{
return
generateInsertValuesSelective
(
columns
,
true
);
}
/**
* insert column selective
* @param columns
* @param bracket
* @return
*/
private
XmlElement
generateInsertValuesSelective
(
List
<
IntrospectedColumn
>
columns
,
boolean
bracket
)
{
XmlElement
insertValuesChooseEle
=
new
XmlElement
(
"choose"
);
XmlElement
insertValuesChooseEle
=
new
XmlElement
(
"choose"
);
XmlElement
valuesWhenEle
=
new
XmlElement
(
"when"
);
XmlElement
valuesWhenEle
=
new
XmlElement
(
"when"
);
...
@@ -393,15 +413,17 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
...
@@ -393,15 +413,17 @@ public class SelectiveEnhancedPlugin extends BasePlugin implements IUpsertPlugin
XmlElement
valuesForeachEle
=
new
XmlElement
(
"foreach"
);
XmlElement
valuesForeachEle
=
new
XmlElement
(
"foreach"
);
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"open"
,
"("
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
if
(
bracket
)
{
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"open"
,
"("
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"close"
,
")"
));
valuesForeachEle
.
addAttribute
(
new
Attribute
(
"close"
,
")"
));
}
valuesForeachEle
.
addElement
(
new
TextElement
(
"#{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
valuesForeachEle
.
addElement
(
new
TextElement
(
"#{record.${column.javaProperty},jdbcType=${column.jdbcType}}"
));
valuesWhenEle
.
addElement
(
valuesForeachEle
);
valuesWhenEle
.
addElement
(
valuesForeachEle
);
XmlElement
valuesOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
XmlElement
valuesOtherwiseEle
=
new
XmlElement
(
"otherwise"
);
insertValuesChooseEle
.
addElement
(
valuesOtherwiseEle
);
insertValuesChooseEle
.
addElement
(
valuesOtherwiseEle
);
valuesOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
columns
,
"record."
));
valuesOtherwiseEle
.
addElement
(
XmlElementGeneratorTools
.
generateValuesSelective
(
columns
,
"record."
,
bracket
));
return
insertValuesChooseEle
;
return
insertValuesChooseEle
;
}
}
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
View file @
09065650
...
@@ -217,7 +217,7 @@ public class UpsertPlugin extends BasePlugin {
...
@@ -217,7 +217,7 @@ public class UpsertPlugin extends BasePlugin {
insertEle
.
addAttribute
(
new
Attribute
(
"parameterType"
,
introspectedTable
.
getRules
().
calculateAllFieldsClass
().
getFullyQualifiedName
()));
insertEle
.
addAttribute
(
new
Attribute
(
"parameterType"
,
introspectedTable
.
getRules
().
calculateAllFieldsClass
().
getFullyQualifiedName
()));
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
XmlElementGeneratorTools
.
useGeneratedKeys
(
insertEle
,
introspectedTable
);
XmlElementGeneratorTools
.
useGeneratedKeys
(
insertEle
,
introspectedTable
,
"record."
);
// insert
// insert
insertEle
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
insertEle
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
...
@@ -349,7 +349,7 @@ public class UpsertPlugin extends BasePlugin {
...
@@ -349,7 +349,7 @@ public class UpsertPlugin extends BasePlugin {
// insert
// insert
updateEle
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
updateEle
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()));
for
(
Element
element
:
XmlElementGeneratorTools
.
generateKeys
(
columns
))
{
for
(
Element
element
:
XmlElementGeneratorTools
.
generateKeys
(
columns
,
"record."
))
{
updateEle
.
addElement
(
element
);
updateEle
.
addElement
(
element
);
}
}
this
.
generateExistsClause
(
introspectedTable
,
updateEle
,
XmlElementGeneratorTools
.
generateValues
(
columns
,
"record."
,
false
));
this
.
generateExistsClause
(
introspectedTable
,
updateEle
,
XmlElementGeneratorTools
.
generateValues
(
columns
,
"record."
,
false
));
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java
View file @
09065650
...
@@ -27,6 +27,7 @@ import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
...
@@ -27,6 +27,7 @@ import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import
org.mybatis.generator.config.GeneratedKey
;
import
org.mybatis.generator.config.GeneratedKey
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Iterator
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.List
;
...
@@ -151,7 +152,7 @@ public class XmlElementGeneratorTools {
...
@@ -151,7 +152,7 @@ public class XmlElementGeneratorTools {
* @param element
* @param element
* @param name
* @param name
*/
*/
public
static
void
removeAttribute
(
XmlElement
element
,
String
name
){
public
static
void
removeAttribute
(
XmlElement
element
,
String
name
)
{
Iterator
<
Attribute
>
iterator
=
element
.
getAttributes
().
iterator
();
Iterator
<
Attribute
>
iterator
=
element
.
getAttributes
().
iterator
();
while
(
iterator
.
hasNext
())
{
while
(
iterator
.
hasNext
())
{
Attribute
attribute
=
iterator
.
next
();
Attribute
attribute
=
iterator
.
next
();
...
@@ -166,7 +167,7 @@ public class XmlElementGeneratorTools {
...
@@ -166,7 +167,7 @@ public class XmlElementGeneratorTools {
* @param element
* @param element
* @param attribute
* @param attribute
*/
*/
public
static
void
replaceAttribute
(
XmlElement
element
,
Attribute
attribute
){
public
static
void
replaceAttribute
(
XmlElement
element
,
Attribute
attribute
)
{
removeAttribute
(
element
,
attribute
.
getName
());
removeAttribute
(
element
,
attribute
.
getName
());
element
.
addAttribute
(
attribute
);
element
.
addAttribute
(
attribute
);
}
}
...
@@ -176,7 +177,7 @@ public class XmlElementGeneratorTools {
...
@@ -176,7 +177,7 @@ public class XmlElementGeneratorTools {
* @param srcEle
* @param srcEle
* @param destEle
* @param destEle
*/
*/
public
static
void
replaceXmlElement
(
XmlElement
srcEle
,
XmlElement
destEle
){
public
static
void
replaceXmlElement
(
XmlElement
srcEle
,
XmlElement
destEle
)
{
srcEle
.
setName
(
destEle
.
getName
());
srcEle
.
setName
(
destEle
.
getName
());
srcEle
.
getAttributes
().
clear
();
srcEle
.
getAttributes
().
clear
();
srcEle
.
getAttributes
().
addAll
(
destEle
.
getAttributes
());
srcEle
.
getAttributes
().
addAll
(
destEle
.
getAttributes
());
...
@@ -190,17 +191,28 @@ public class XmlElementGeneratorTools {
...
@@ -190,17 +191,28 @@ public class XmlElementGeneratorTools {
* @return
* @return
*/
*/
public
static
List
<
Element
>
generateKeys
(
List
<
IntrospectedColumn
>
columns
)
{
public
static
List
<
Element
>
generateKeys
(
List
<
IntrospectedColumn
>
columns
)
{
return
generateKeys
(
columns
,
true
);
return
generateKeys
(
columns
,
null
);
}
}
/**
/**
* 生成keys Ele
* 生成keys Ele
* @param columns
* @param columns
* @param prefix
* @return
*/
public
static
List
<
Element
>
generateKeys
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
)
{
return
generateKeys
(
columns
,
prefix
,
true
);
}
/**
* 生成keys Ele
* @param columns
* @param prefix
* @param bracket
* @param bracket
* @return
* @return
*/
*/
public
static
List
<
Element
>
generateKeys
(
List
<
IntrospectedColumn
>
columns
,
boolean
bracket
)
{
public
static
List
<
Element
>
generateKeys
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
,
boolean
bracket
)
{
return
generateCommColumns
(
columns
,
null
,
bracket
,
1
);
return
generateCommColumns
(
columns
,
prefix
,
bracket
,
1
);
}
}
/**
/**
...
@@ -361,8 +373,39 @@ public class XmlElementGeneratorTools {
...
@@ -361,8 +373,39 @@ public class XmlElementGeneratorTools {
* @param type 1:key,2:value,3:set
* @param type 1:key,2:value,3:set
* @return
* @return
*/
*/
p
ublic
static
List
<
Element
>
generateCommColumns
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
,
boolean
bracket
,
int
type
)
{
p
rivate
static
List
<
Element
>
generateCommColumns
(
List
<
IntrospectedColumn
>
columns
,
String
prefix
,
boolean
bracket
,
int
type
)
{
List
<
Element
>
list
=
new
ArrayList
<>();
List
<
Element
>
list
=
new
ArrayList
<>();
if
(
hasIdentityAndGeneratedAlwaysColumns
(
columns
))
{
XmlElement
eleTrim
=
new
XmlElement
(
"trim"
);
if
(
bracket
)
{
eleTrim
.
addAttribute
(
new
Attribute
(
"prefix"
,
"("
));
eleTrim
.
addAttribute
(
new
Attribute
(
"suffix"
,
")"
));
eleTrim
.
addAttribute
(
new
Attribute
(
"suffixOverrides"
,
","
));
}
else
{
eleTrim
.
addAttribute
(
new
Attribute
(
"suffixOverrides"
,
","
));
}
for
(
IntrospectedColumn
introspectedColumn
:
columns
)
{
if
(
introspectedColumn
.
isGeneratedAlways
()
||
introspectedColumn
.
isIdentity
())
{
generateSelectiveToTrimEle
(
eleTrim
,
introspectedColumn
,
prefix
,
type
);
}
else
{
switch
(
type
)
{
case
3
:
eleTrim
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
" = "
+
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
)
+
","
));
break
;
case
2
:
eleTrim
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
)
+
","
));
break
;
case
1
:
eleTrim
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
","
));
break
;
}
}
}
return
Arrays
.
asList
(
eleTrim
);
}
else
{
StringBuilder
sb
=
new
StringBuilder
(
bracket
?
"("
:
""
);
StringBuilder
sb
=
new
StringBuilder
(
bracket
?
"("
:
""
);
Iterator
<
IntrospectedColumn
>
columnIterator
=
columns
.
iterator
();
Iterator
<
IntrospectedColumn
>
columnIterator
=
columns
.
iterator
();
while
(
columnIterator
.
hasNext
())
{
while
(
columnIterator
.
hasNext
())
{
...
@@ -404,6 +447,7 @@ public class XmlElementGeneratorTools {
...
@@ -404,6 +447,7 @@ public class XmlElementGeneratorTools {
return
list
;
return
list
;
}
}
}
/**
/**
* 通用遍历columns
* 通用遍历columns
...
@@ -424,6 +468,20 @@ public class XmlElementGeneratorTools {
...
@@ -424,6 +468,20 @@ public class XmlElementGeneratorTools {
}
}
for
(
IntrospectedColumn
introspectedColumn
:
columns
)
{
for
(
IntrospectedColumn
introspectedColumn
:
columns
)
{
generateSelectiveToTrimEle
(
eleTrim
,
introspectedColumn
,
prefix
,
type
);
}
return
eleTrim
;
}
/**
* 生成选择列到trim 节点
* @param trimEle
* @param introspectedColumn
* @param prefix
* @param type 1:key,2:value,3:set
*/
private
static
void
generateSelectiveToTrimEle
(
XmlElement
trimEle
,
IntrospectedColumn
introspectedColumn
,
String
prefix
,
int
type
)
{
if
(
type
!=
3
&&
(
introspectedColumn
.
isSequenceColumn
()
||
introspectedColumn
.
getFullyQualifiedJavaType
().
isPrimitive
()))
{
if
(
type
!=
3
&&
(
introspectedColumn
.
isSequenceColumn
()
||
introspectedColumn
.
getFullyQualifiedJavaType
().
isPrimitive
()))
{
// if it is a sequence column, it is not optional
// if it is a sequence column, it is not optional
// This is required for MyBatis3 because MyBatis3 parses
// This is required for MyBatis3 because MyBatis3 parses
...
@@ -432,10 +490,10 @@ public class XmlElementGeneratorTools {
...
@@ -432,10 +490,10 @@ public class XmlElementGeneratorTools {
// if it is primitive, we cannot do a null check
// if it is primitive, we cannot do a null check
switch
(
type
)
{
switch
(
type
)
{
case
2
:
case
2
:
eleTrim
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
)
+
","
));
trimEle
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
prefix
)
+
","
));
break
;
break
;
case
1
:
case
1
:
eleTrim
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
","
));
trimEle
.
addElement
(
new
TextElement
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
)
+
","
));
break
;
break
;
}
}
}
else
{
}
else
{
...
@@ -454,13 +512,10 @@ public class XmlElementGeneratorTools {
...
@@ -454,13 +512,10 @@ public class XmlElementGeneratorTools {
break
;
break
;
}
}
eleTrim
.
addElement
(
eleIf
);
trimEle
.
addElement
(
eleIf
);
}
}
}
}
return
eleTrim
;
}
/**
/**
* 查找指定xml节点下指定节点名称的元素
* 查找指定xml节点下指定节点名称的元素
* @param xmlElement
* @param xmlElement
...
@@ -516,4 +571,18 @@ public class XmlElementGeneratorTools {
...
@@ -516,4 +571,18 @@ public class XmlElementGeneratorTools {
element
.
addElement
(
new
TextElement
(
sb
.
toString
()));
element
.
addElement
(
new
TextElement
(
sb
.
toString
()));
}
}
}
}
/**
* 是否存在自增或者生成的column
* @param columns
* @return
*/
private
static
boolean
hasIdentityAndGeneratedAlwaysColumns
(
List
<
IntrospectedColumn
>
columns
)
{
for
(
IntrospectedColumn
ic
:
columns
)
{
if
(
ic
.
isGeneratedAlways
()
||
ic
.
isIdentity
())
{
return
true
;
}
}
return
false
;
}
}
}
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