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
a4980ab0
Commit
a4980ab0
authored
Jun 21, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化修正
parent
b3e4792e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
54 deletions
+37
-54
src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
...sw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
+37
-54
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/SelectiveEnhancedPlugin.java
View file @
a4980ab0
...
...
@@ -18,13 +18,13 @@ 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
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.*
;
import
org.mybatis.generator.api.dom.xml.*
;
import
org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
...
...
@@ -136,61 +136,61 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
// ====================================== 1. insertSelective ======================================
if
(
"insertSelective"
.
equals
(
id
))
{
List
<
XmlElement
>
eles
=
this
.
findEle
(
xmlElement
,
"trim"
);
List
<
XmlElement
>
eles
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"trim"
);
for
(
XmlElement
ele
:
eles
)
{
this
.
replaceEle
(
ele
,
"_parameter."
);
this
.
replaceEle
(
ele
,
"_parameter."
,
introspectedTable
);
}
}
// ====================================== 2. updateByExampleSelective ======================================
if
(
"updateByExampleSelective"
.
equals
(
id
))
{
List
<
XmlElement
>
eles
=
this
.
findEle
(
xmlElement
,
"set"
);
List
<
XmlElement
>
eles
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"set"
);
for
(
XmlElement
ele
:
eles
)
{
this
.
replaceEle
(
ele
,
"record."
);
this
.
replaceEle
(
ele
,
"record."
,
introspectedTable
);
}
}
// ====================================== 3. updateByPrimaryKeySelective ======================================
if
(
"updateByPrimaryKeySelective"
.
equals
(
id
))
{
List
<
XmlElement
>
eles
=
this
.
findEle
(
xmlElement
,
"set"
);
List
<
XmlElement
>
eles
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"set"
);
for
(
XmlElement
ele
:
eles
)
{
this
.
replaceEle
(
ele
,
"_parameter."
);
this
.
replaceEle
(
ele
,
"_parameter."
,
introspectedTable
);
}
}
// ====================================== 4. upsertSelective ======================================
if
(
"upsertSelective"
.
equals
(
id
))
{
List
<
XmlElement
>
eles
=
this
.
findEle
(
xmlElement
,
"trim"
);
List
<
XmlElement
>
eles
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"trim"
);
for
(
XmlElement
ele
:
eles
)
{
this
.
replaceEle
(
ele
,
"_parameter."
);
this
.
replaceEle
(
ele
,
"_parameter."
,
introspectedTable
);
}
}
// ====================================== 5. upsertByExampleSelective ======================================
if
(
"upsertByExampleSelective"
.
equals
(
id
))
{
List
<
XmlElement
>
eles
=
this
.
findEle
(
xmlElement
,
"trim"
);
this
.
replaceEle
(
eles
.
get
(
0
),
"record."
);
List
<
XmlElement
>
eles
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"trim"
);
this
.
replaceEle
(
eles
.
get
(
0
),
"record."
,
introspectedTable
);
// upsertByExampleSelective的第二个trim比较特殊,需另行处理
this
.
replaceEleForUpsertByExampleSelective
(
eles
.
get
(
1
),
"record."
,
introspectedTable
,
!
introspectedTable
.
getRules
().
generateRecordWithBLOBsClass
());
List
<
XmlElement
>
eles1
=
this
.
findEle
(
xmlElement
,
"set"
);
List
<
XmlElement
>
eles1
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"set"
);
for
(
XmlElement
ele
:
eles1
)
{
this
.
replaceEle
(
ele
,
"record."
);
this
.
replaceEle
(
ele
,
"record."
,
introspectedTable
);
}
}
// ====================================== 6. upsertSelectiveWithBLOBs ======================================
if
(
"upsertSelectiveWithBLOBs"
.
equals
(
id
))
{
List
<
XmlElement
>
eles
=
this
.
findEle
(
xmlElement
,
"trim"
);
List
<
XmlElement
>
eles
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"trim"
);
for
(
XmlElement
ele
:
eles
)
{
this
.
replaceEle
(
ele
,
"_parameter."
);
this
.
replaceEle
(
ele
,
"_parameter."
,
introspectedTable
);
}
}
// ====================================== 7. upsertByExampleSelectiveWithBLOBs ======================================
if
(
"upsertByExampleSelectiveWithBLOBs"
.
equals
(
id
))
{
List
<
XmlElement
>
eles
=
this
.
findEle
(
xmlElement
,
"trim"
);
this
.
replaceEle
(
eles
.
get
(
0
),
"record."
);
List
<
XmlElement
>
eles
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"trim"
);
this
.
replaceEle
(
eles
.
get
(
0
),
"record."
,
introspectedTable
);
// upsertByExampleSelective的第二个trim比较特殊,需另行处理
this
.
replaceEleForUpsertByExampleSelective
(
eles
.
get
(
1
),
"record."
,
introspectedTable
,
true
);
List
<
XmlElement
>
eles1
=
this
.
findEle
(
xmlElement
,
"set"
);
List
<
XmlElement
>
eles1
=
XmlElementGeneratorTools
.
findXmlElements
(
xmlElement
,
"set"
);
for
(
XmlElement
ele
:
eles1
)
{
this
.
replaceEle
(
ele
,
"record."
);
this
.
replaceEle
(
ele
,
"record."
,
introspectedTable
);
}
}
}
...
...
@@ -198,32 +198,13 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
return
true
;
}
/**
* 查找当前节点下的指定节点
* @param element
* @param eleName
* @return
*/
private
List
<
XmlElement
>
findEle
(
XmlElement
element
,
String
eleName
)
{
List
<
XmlElement
>
list
=
new
ArrayList
<>();
List
<
Element
>
elements
=
element
.
getElements
();
for
(
Element
ele
:
elements
)
{
if
(
ele
instanceof
XmlElement
)
{
XmlElement
xmlElement
=
(
XmlElement
)
ele
;
if
(
eleName
.
equalsIgnoreCase
(
xmlElement
.
getName
()))
{
list
.
add
(
xmlElement
);
}
}
}
return
list
;
}
/**
* 替换节点if信息
* @param element
* @param prefix
* @param introspectedTable
*/
private
void
replaceEle
(
XmlElement
element
,
String
prefix
)
{
private
void
replaceEle
(
XmlElement
element
,
String
prefix
,
IntrospectedTable
introspectedTable
)
{
// choose
XmlElement
chooseEle
=
new
XmlElement
(
"choose"
);
// when
...
...
@@ -234,34 +215,36 @@ public class SelectiveEnhancedPlugin extends BasePlugin {
if
(
ele
instanceof
XmlElement
){
// if的text节点
XmlElement
xmlElement
=
(
XmlElement
)
ele
;
TextElement
textElement
=
(
TextElement
)
xmlElement
.
getElements
().
get
(
0
);
// 找出field 名称
String
text
=
textElement
.
getContent
().
trim
();
String
text
=
((
TextElement
)
xmlElement
.
getElements
().
get
(
0
)).
getContent
();
String
field
=
""
;
if
(
text
.
matches
(
".*\\s*=\\s*#\\{.*\\},?"
))
{
Pattern
pattern
=
Pattern
.
compile
(
"(.*?)\\s*=\\s*#\\{.*},?"
);
Matcher
matcher
=
pattern
.
matcher
(
text
);
if
(
matcher
.
find
()){
field
=
matcher
.
group
(
1
);
}
}
else
if
(
text
.
matches
(
"#\\{.*\\},?"
))
{
if
(
text
.
matches
(
"#\\{.*\\},?"
))
{
Pattern
pattern
=
Pattern
.
compile
(
"#\\{(.*?),.*\\},?"
);
Matcher
matcher
=
pattern
.
matcher
(
text
);
if
(
matcher
.
find
()){
field
=
matcher
.
group
(
1
);
}
}
else
{
field
=
text
.
replaceAll
(
","
,
""
);
String
columnName
;
if
(
text
.
matches
(
".*\\s*=.*"
)){
columnName
=
text
.
split
(
"="
)[
0
];
}
else
{
columnName
=
text
.
replaceAll
(
","
,
""
);
}
// bug fixed: 修正使用autoDelimitKeywords过滤关键词造成的field前后加了特殊字符的问题
columnName
=
columnName
.
trim
().
replaceAll
(
"`"
,
""
).
replaceAll
(
"\""
,
""
).
replaceAll
(
"'"
,
""
);
IntrospectedColumn
column
=
introspectedTable
.
getColumn
(
columnName
);
field
=
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
column
);
}
XmlElement
ifEle
=
new
XmlElement
(
"if"
);
// bug fixed: 修正使用autoDelimitKeywords过滤关键词造成的field前后加了特殊字符的问题
field
=
field
.
replaceAll
(
"`"
,
""
).
replaceAll
(
"\""
,
""
).
replaceAll
(
"'"
,
""
);
ifEle
.
addAttribute
(
new
Attribute
(
"test"
,
prefix
+
"isSelective(\'"
+
field
+
"\')"
));
ifEle
.
addElement
(
textElement
);
for
(
Element
ifChild
:
xmlElement
.
getElements
()){
ifEle
.
addElement
(
ifChild
);
}
whenEle
.
addElement
(
ifEle
);
}
else
{
whenEle
.
addElement
(
ele
);
...
...
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