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
02e0367d
Commit
02e0367d
authored
Jul 03, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新插件SelectSelectivePlugin实现
parent
7b081648
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
425 additions
and
21 deletions
+425
-21
src/main/java/com/itfsw/mybatis/generator/plugins/LimitPlugin.java
...java/com/itfsw/mybatis/generator/plugins/LimitPlugin.java
+25
-15
src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
...m/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
+2
-2
src/main/java/com/itfsw/mybatis/generator/plugins/SelectOneByExamplePlugin.java
...w/mybatis/generator/plugins/SelectOneByExamplePlugin.java
+5
-4
src/main/java/com/itfsw/mybatis/generator/plugins/SelectSelectivePlugin.java
...tfsw/mybatis/generator/plugins/SelectSelectivePlugin.java
+239
-0
src/main/java/com/itfsw/mybatis/generator/plugins/utils/FormatTools.java
...om/itfsw/mybatis/generator/plugins/utils/FormatTools.java
+154
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/LimitPlugin.java
View file @
02e0367d
...
@@ -169,6 +169,31 @@ public class LimitPlugin extends BasePlugin {
...
@@ -169,6 +169,31 @@ public class LimitPlugin extends BasePlugin {
*/
*/
@Override
@Override
public
boolean
sqlMapSelectByExampleWithoutBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
public
boolean
sqlMapSelectByExampleWithoutBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
this
.
generateLimitElement
(
element
,
introspectedTable
);
return
true
;
}
/**
* SQL Map Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param element
* @param introspectedTable
* @return
*/
@Override
public
boolean
sqlMapSelectByExampleWithBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
this
.
generateLimitElement
(
element
,
introspectedTable
);
return
true
;
}
/**
* 生成limit节点
*
* @param element
* @param introspectedTable
*/
public
void
generateLimitElement
(
XmlElement
element
,
IntrospectedTable
introspectedTable
){
XmlElement
ifLimitNotNullElement
=
new
XmlElement
(
"if"
);
XmlElement
ifLimitNotNullElement
=
new
XmlElement
(
"if"
);
ifLimitNotNullElement
.
addAttribute
(
new
Attribute
(
"test"
,
"rows != null"
));
ifLimitNotNullElement
.
addAttribute
(
new
Attribute
(
"test"
,
"rows != null"
));
...
@@ -185,20 +210,5 @@ public class LimitPlugin extends BasePlugin {
...
@@ -185,20 +210,5 @@ public class LimitPlugin extends BasePlugin {
element
.
addElement
(
ifLimitNotNullElement
);
element
.
addElement
(
ifLimitNotNullElement
);
logger
.
debug
(
"itfsw(MySQL分页插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()+
"selectByExample方法增加分页条件。"
);
logger
.
debug
(
"itfsw(MySQL分页插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()+
"selectByExample方法增加分页条件。"
);
return
true
;
}
/**
* SQL Map Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param element
* @param introspectedTable
* @return
*/
@Override
public
boolean
sqlMapSelectByExampleWithBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
return
this
.
sqlMapSelectByExampleWithoutBLOBsElementGenerated
(
element
,
introspectedTable
);
}
}
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
View file @
02e0367d
...
@@ -17,9 +17,9 @@
...
@@ -17,9 +17,9 @@
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.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.FormatTools
;
import
com.itfsw.mybatis.generator.plugins.utils.IncrementsPluginTools
;
import
com.itfsw.mybatis.generator.plugins.utils.IncrementsPluginTools
;
import
com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools
;
import
com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools
;
import
com.itfsw.mybatis.generator.plugins.utils.enhanced.JavaElementEnhanced
;
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.*
;
...
@@ -200,7 +200,7 @@ public class ModelBuilderPlugin extends BasePlugin {
...
@@ -200,7 +200,7 @@ public class ModelBuilderPlugin extends BasePlugin {
mIncrements
.
addBodyLine
(
"obj."
+
setterMethod
.
getName
()
+
"("
+
field
.
getName
()
+
");"
);
mIncrements
.
addBodyLine
(
"obj."
+
setterMethod
.
getName
()
+
"("
+
field
.
getName
()
+
");"
);
mIncrements
.
addBodyLine
(
"return this;"
);
mIncrements
.
addBodyLine
(
"return this;"
);
JavaElementEnhanced
.
addMethodWithBestPosition
(
innerClass
,
mIncrements
);
FormatTools
.
addMethodWithBestPosition
(
innerClass
,
mIncrements
);
}
}
}
}
}
}
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/SelectOneByExamplePlugin.java
View file @
02e0367d
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
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.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.FormatTools
;
import
com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools
;
import
com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools
;
import
com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools
;
import
com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedColumn
;
...
@@ -132,7 +133,7 @@ public class SelectOneByExamplePlugin extends BasePlugin {
...
@@ -132,7 +133,7 @@ public class SelectOneByExamplePlugin extends BasePlugin {
commentGenerator
.
addGeneralMethodComment
(
method
,
introspectedTable
);
commentGenerator
.
addGeneralMethodComment
(
method
,
introspectedTable
);
// interface 增加方法
// interface 增加方法
interfaze
.
addMethod
(
method
);
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
method
);
logger
.
debug
(
"itfsw(查询单条数据插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加selectOneByExample方法。"
);
logger
.
debug
(
"itfsw(查询单条数据插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加selectOneByExample方法。"
);
// 方法生成 selectOneByExampleWithBLOBs !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准
// 方法生成 selectOneByExampleWithBLOBs !!! 注意这里的行为不以有没有生成Model 的 WithBLOBs类为基准
...
@@ -147,7 +148,7 @@ public class SelectOneByExamplePlugin extends BasePlugin {
...
@@ -147,7 +148,7 @@ public class SelectOneByExamplePlugin extends BasePlugin {
commentGenerator
.
addGeneralMethodComment
(
method1
,
introspectedTable
);
commentGenerator
.
addGeneralMethodComment
(
method1
,
introspectedTable
);
// interface 增加方法
// interface 增加方法
interfaze
.
addMethod
(
method1
);
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
method1
);
logger
.
debug
(
"itfsw(查询单条数据插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加selectOneByExampleWithBLOBs方法。"
);
logger
.
debug
(
"itfsw(查询单条数据插件):"
+
interfaze
.
getType
().
getShortName
()
+
"增加selectOneByExampleWithBLOBs方法。"
);
}
}
...
@@ -200,7 +201,7 @@ public class SelectOneByExamplePlugin extends BasePlugin {
...
@@ -200,7 +201,7 @@ public class SelectOneByExamplePlugin extends BasePlugin {
// 只查询一条
// 只查询一条
selectOneElement
.
addElement
(
new
TextElement
(
"limit 1"
));
selectOneElement
.
addElement
(
new
TextElement
(
"limit 1"
));
// 添加到根节点
// 添加到根节点
document
.
getRootElement
().
addElement
(
selectOneElement
);
FormatTools
.
addElementWithBestPosition
(
document
.
getRootElement
(),
selectOneElement
);
logger
.
debug
(
"itfsw(查询单条数据插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加selectOneByExample方法。"
);
logger
.
debug
(
"itfsw(查询单条数据插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加selectOneByExample方法。"
);
// ------------------------------------ selectOneByExampleWithBLOBs ----------------------------------
// ------------------------------------ selectOneByExampleWithBLOBs ----------------------------------
...
@@ -247,7 +248,7 @@ public class SelectOneByExamplePlugin extends BasePlugin {
...
@@ -247,7 +248,7 @@ public class SelectOneByExamplePlugin extends BasePlugin {
selectOneWithBLOBsElement
.
addElement
(
new
TextElement
(
"limit 1"
));
selectOneWithBLOBsElement
.
addElement
(
new
TextElement
(
"limit 1"
));
// 添加到根节点
// 添加到根节点
document
.
getRootElement
().
addElement
(
selectOneWithBLOBsElement
);
FormatTools
.
addElementWithBestPosition
(
document
.
getRootElement
(),
selectOneWithBLOBsElement
);
logger
.
debug
(
"itfsw(查询单条数据插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加selectOneByExampleWithBLOBs方法。"
);
logger
.
debug
(
"itfsw(查询单条数据插件):"
+
introspectedTable
.
getMyBatis3XmlMapperFileName
()
+
"增加selectOneByExampleWithBLOBs方法。"
);
}
}
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/SelectSelectivePlugin.java
0 → 100644
View file @
02e0367d
/*
* Copyright (c) 2017.
*
* 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.utils.*
;
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.Attribute
;
import
org.mybatis.generator.api.dom.xml.Document
;
import
org.mybatis.generator.api.dom.xml.TextElement
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities
;
import
java.util.List
;
import
static
org
.
mybatis
.
generator
.
internal
.
util
.
StringUtility
.
stringHasValue
;
/**
* ---------------------------------------------------------------------------
*
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/6/29 13:34
* ---------------------------------------------------------------------------
*/
public
class
SelectSelectivePlugin
extends
BasePlugin
{
public
static
final
String
METHOD_SELECT_BY_EXAMPLE_SELECTIVE
=
"selectByExampleSelective"
;
public
static
final
String
METHOD_SELECT_BY_PRIMARY_KEY_SELECTIVE
=
"selectByPrimaryKeySelective"
;
public
static
final
String
METHOD_SELECT_ONE_BY_EXAMPLE_SELECTIVE
=
"selectOneByExampleSelective"
;
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param warnings
* @return
*/
@Override
public
boolean
validate
(
List
<
String
>
warnings
)
{
// 插件使用前提是使用了ModelColumnPlugin插件
if
(!
PluginTools
.
checkDependencyPlugin
(
getContext
(),
ModelColumnPlugin
.
class
))
{
warnings
.
add
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"插件需配合com.itfsw.mybatis.generator.plugins.ModelColumnPlugin插件使用!"
);
return
false
;
}
return
super
.
validate
(
warnings
);
}
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param interfaze
* @param topLevelClass
* @param introspectedTable
* @return
*/
@Override
public
boolean
clientGenerated
(
Interface
interfaze
,
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
// 找出全字段对应的Model
FullyQualifiedJavaType
fullFieldModel
=
introspectedTable
.
getRules
().
calculateAllFieldsClass
();
// 返回list类型
FullyQualifiedJavaType
listType
=
FullyQualifiedJavaType
.
getNewListInstance
();
listType
.
addTypeArgument
(
fullFieldModel
);
// column枚举
FullyQualifiedJavaType
selectiveType
=
new
FullyQualifiedJavaType
(
fullFieldModel
.
getShortName
()
+
"."
+
ModelColumnPlugin
.
ENUM_NAME
);
// 1. selectByExampleSelective 方法
Method
mSelectByExampleSelective
=
JavaElementGeneratorTools
.
generateMethod
(
METHOD_SELECT_BY_EXAMPLE_SELECTIVE
,
JavaVisibility
.
DEFAULT
,
listType
,
new
Parameter
(
new
FullyQualifiedJavaType
(
introspectedTable
.
getExampleType
()),
"example"
,
"@Param(\"example\")"
),
new
Parameter
(
selectiveType
,
"selective"
,
"@Param(\"selective\")"
,
true
)
);
commentGenerator
.
addGeneralMethodComment
(
mSelectByExampleSelective
,
introspectedTable
);
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
mSelectByExampleSelective
);
// 2. selectByPrimaryKeySelective
Method
mSelectByPrimaryKeySelective
=
JavaElementGeneratorTools
.
generateMethod
(
METHOD_SELECT_BY_PRIMARY_KEY_SELECTIVE
,
JavaVisibility
.
DEFAULT
,
fullFieldModel
);
if
(
introspectedTable
.
getRules
().
generatePrimaryKeyClass
())
{
FullyQualifiedJavaType
type
=
new
FullyQualifiedJavaType
(
introspectedTable
.
getPrimaryKeyType
());
mSelectByPrimaryKeySelective
.
addParameter
(
new
Parameter
(
type
,
"key"
,
"@Param(\"record\")"
));
}
else
{
// no primary key class - fields are in the base class
// if more than one PK field, then we need to annotate the
// parameters
// for MyBatis3
List
<
IntrospectedColumn
>
introspectedColumns
=
introspectedTable
.
getPrimaryKeyColumns
();
for
(
IntrospectedColumn
introspectedColumn
:
introspectedColumns
)
{
FullyQualifiedJavaType
type
=
introspectedColumn
.
getFullyQualifiedJavaType
();
Parameter
parameter
=
new
Parameter
(
type
,
introspectedColumn
.
getJavaProperty
());
parameter
.
addAnnotation
(
"@Param(\""
+
introspectedColumn
.
getJavaProperty
()
+
"\")"
);
mSelectByPrimaryKeySelective
.
addParameter
(
parameter
);
}
}
mSelectByPrimaryKeySelective
.
addParameter
(
new
Parameter
(
selectiveType
,
"selective"
,
"@Param(\"selective\")"
,
true
));
commentGenerator
.
addGeneralMethodComment
(
mSelectByPrimaryKeySelective
,
introspectedTable
);
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
mSelectByPrimaryKeySelective
);
// 3. selectOneByExampleSelective
if
(
PluginTools
.
getPluginConfiguration
(
context
,
SelectOneByExamplePlugin
.
class
)
!=
null
)
{
Method
mSelectOneByExampleSelective
=
JavaElementGeneratorTools
.
generateMethod
(
METHOD_SELECT_ONE_BY_EXAMPLE_SELECTIVE
,
JavaVisibility
.
DEFAULT
,
fullFieldModel
,
new
Parameter
(
new
FullyQualifiedJavaType
(
introspectedTable
.
getExampleType
()),
"example"
,
"@Param(\"example\")"
),
new
Parameter
(
selectiveType
,
"selective"
,
"@Param(\"selective\")"
,
true
)
);
commentGenerator
.
addGeneralMethodComment
(
mSelectOneByExampleSelective
,
introspectedTable
);
FormatTools
.
addMethodWithBestPosition
(
interfaze
,
mSelectOneByExampleSelective
);
}
return
true
;
}
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param document
* @param introspectedTable
* @return
*/
@Override
public
boolean
sqlMapDocumentGenerated
(
Document
document
,
IntrospectedTable
introspectedTable
)
{
// 生成返回字段节点
XmlElement
columnsEle
=
new
XmlElement
(
"foreach"
);
columnsEle
.
addAttribute
(
new
Attribute
(
"collection"
,
"selective"
));
columnsEle
.
addAttribute
(
new
Attribute
(
"item"
,
"column"
));
columnsEle
.
addAttribute
(
new
Attribute
(
"separator"
,
","
));
columnsEle
.
addElement
(
new
TextElement
(
"${column.value}"
));
// 1. selectByExampleSelective 方法
XmlElement
selectByExampleSelectiveEle
=
new
XmlElement
(
"select"
);
commentGenerator
.
addComment
(
selectByExampleSelectiveEle
);
selectByExampleSelectiveEle
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_SELECT_BY_EXAMPLE_SELECTIVE
));
selectByExampleSelectiveEle
.
addAttribute
(
new
Attribute
(
"resultType"
,
introspectedTable
.
getRules
().
calculateAllFieldsClass
().
getFullyQualifiedName
()));
selectByExampleSelectiveEle
.
addAttribute
(
new
Attribute
(
"parameterType"
,
"map"
));
selectByExampleSelectiveEle
.
addElement
(
new
TextElement
(
"select"
));
if
(
stringHasValue
(
introspectedTable
.
getSelectByExampleQueryId
()))
{
selectByExampleSelectiveEle
.
addElement
(
new
TextElement
(
"'"
+
introspectedTable
.
getSelectByExampleQueryId
()
+
"' as QUERYID,"
));
}
selectByExampleSelectiveEle
.
addElement
(
columnsEle
);
selectByExampleSelectiveEle
.
addElement
(
new
TextElement
(
"from "
+
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
()));
selectByExampleSelectiveEle
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
XmlElement
ifElement
=
new
XmlElement
(
"if"
);
ifElement
.
addAttribute
(
new
Attribute
(
"test"
,
"example.orderByClause != null"
));
ifElement
.
addElement
(
new
TextElement
(
"order by ${example.orderByClause}"
));
selectByExampleSelectiveEle
.
addElement
(
ifElement
);
FormatTools
.
addElementWithBestPosition
(
document
.
getRootElement
(),
selectByExampleSelectiveEle
);
// 2. selectByPrimaryKeySelective
XmlElement
selectByPrimaryKeySelectiveEle
=
new
XmlElement
(
"select"
);
commentGenerator
.
addComment
(
selectByPrimaryKeySelectiveEle
);
selectByPrimaryKeySelectiveEle
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_SELECT_BY_PRIMARY_KEY_SELECTIVE
));
selectByPrimaryKeySelectiveEle
.
addAttribute
(
new
Attribute
(
"resultType"
,
introspectedTable
.
getRules
().
calculateAllFieldsClass
().
getFullyQualifiedName
()));
selectByPrimaryKeySelectiveEle
.
addAttribute
(
new
Attribute
(
"parameterType"
,
"map"
));
selectByPrimaryKeySelectiveEle
.
addElement
(
new
TextElement
(
"select"
));
if
(
stringHasValue
(
introspectedTable
.
getSelectByExampleQueryId
()))
{
selectByPrimaryKeySelectiveEle
.
addElement
(
new
TextElement
(
"'"
+
introspectedTable
.
getSelectByExampleQueryId
()
+
"' as QUERYID,"
));
}
selectByPrimaryKeySelectiveEle
.
addElement
(
columnsEle
);
selectByPrimaryKeySelectiveEle
.
addElement
(
new
TextElement
(
"from "
+
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
()));
boolean
and
=
false
;
StringBuffer
sb
=
new
StringBuffer
();
for
(
IntrospectedColumn
introspectedColumn
:
introspectedTable
.
getPrimaryKeyColumns
())
{
sb
.
setLength
(
0
);
if
(
and
)
{
sb
.
append
(
" and "
);
}
else
{
sb
.
append
(
"where "
);
and
=
true
;
}
sb
.
append
(
MyBatis3FormattingUtilities
.
getAliasedEscapedColumnName
(
introspectedColumn
));
sb
.
append
(
" = "
);
sb
.
append
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
introspectedTable
.
getRules
().
generatePrimaryKeyClass
()
?
"record."
:
null
));
selectByPrimaryKeySelectiveEle
.
addElement
(
new
TextElement
(
sb
.
toString
()));
}
FormatTools
.
addElementWithBestPosition
(
document
.
getRootElement
(),
selectByPrimaryKeySelectiveEle
);
// 3. selectOneByExampleSelective
if
(
PluginTools
.
getPluginConfiguration
(
context
,
SelectOneByExamplePlugin
.
class
)
!=
null
)
{
XmlElement
selectOneByExampleSelectiveEle
=
new
XmlElement
(
"select"
);
commentGenerator
.
addComment
(
selectOneByExampleSelectiveEle
);
selectOneByExampleSelectiveEle
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_SELECT_ONE_BY_EXAMPLE_SELECTIVE
));
selectOneByExampleSelectiveEle
.
addAttribute
(
new
Attribute
(
"resultType"
,
introspectedTable
.
getRules
().
calculateAllFieldsClass
().
getFullyQualifiedName
()));
selectOneByExampleSelectiveEle
.
addAttribute
(
new
Attribute
(
"parameterType"
,
"map"
));
selectOneByExampleSelectiveEle
.
addElement
(
new
TextElement
(
"select"
));
if
(
stringHasValue
(
introspectedTable
.
getSelectByExampleQueryId
()))
{
selectOneByExampleSelectiveEle
.
addElement
(
new
TextElement
(
"'"
+
introspectedTable
.
getSelectByExampleQueryId
()
+
"' as QUERYID,"
));
}
selectOneByExampleSelectiveEle
.
addElement
(
columnsEle
);
selectOneByExampleSelectiveEle
.
addElement
(
new
TextElement
(
"from "
+
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
()));
selectOneByExampleSelectiveEle
.
addElement
(
XmlElementGeneratorTools
.
getUpdateByExampleIncludeElement
(
introspectedTable
));
XmlElement
ifElement1
=
new
XmlElement
(
"if"
);
ifElement1
.
addAttribute
(
new
Attribute
(
"test"
,
"example.orderByClause != null"
));
ifElement1
.
addElement
(
new
TextElement
(
"order by ${example.orderByClause}"
));
selectOneByExampleSelectiveEle
.
addElement
(
ifElement1
);
// 只查询一条
selectOneByExampleSelectiveEle
.
addElement
(
new
TextElement
(
"limit 1"
));
// 添加到根节点
FormatTools
.
addElementWithBestPosition
(
document
.
getRootElement
(),
selectOneByExampleSelectiveEle
);
}
return
true
;
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/
enhanced/JavaElementEnhanced
.java
→
src/main/java/com/itfsw/mybatis/generator/plugins/utils/
FormatTools
.java
View file @
02e0367d
...
@@ -14,22 +14,26 @@
...
@@ -14,22 +14,26 @@
* limitations under the License.
* limitations under the License.
*/
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
.
enhanced
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
;
import
org.mybatis.generator.api.dom.java.InnerClass
;
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.Method
;
import
org.mybatis.generator.api.dom.xml.Attribute
;
import
org.mybatis.generator.api.dom.xml.Element
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
java.util.List
;
import
java.util.List
;
/**
/**
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
*
JavaElement 增强
*
格式化工具,优化输出
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
* @author: hewei
* @author: hewei
* @time:2017/6/
19 16:34
* @time:2017/6/
30 10:53
* ---------------------------------------------------------------------------
* ---------------------------------------------------------------------------
*/
*/
public
class
JavaElementEnhanced
{
public
class
FormatTools
{
/**
/**
* 在最佳位置添加方法
* 在最佳位置添加方法
*
*
...
@@ -42,16 +46,109 @@ public class JavaElementEnhanced {
...
@@ -42,16 +46,109 @@ public class JavaElementEnhanced {
for
(
int
i
=
0
;
i
<
methods
.
size
();
i
++){
for
(
int
i
=
0
;
i
<
methods
.
size
();
i
++){
Method
m
=
methods
.
get
(
i
);
Method
m
=
methods
.
get
(
i
);
if
(
m
.
getName
().
equals
(
method
.
getName
())){
if
(
m
.
getName
().
equals
(
method
.
getName
())){
if
(
m
.
getParameters
().
size
()
<=
method
.
getParameters
().
size
()
||
index
==
-
1
){
if
(
m
.
getParameters
().
size
()
<=
method
.
getParameters
().
size
()){
index
=
i
+
1
;
}
else
{
index
=
i
;
}
}
else
if
(
m
.
getName
().
startsWith
(
method
.
getName
())){
if
(
index
==
-
1
){
index
=
i
;
index
=
i
;
}
}
}
else
if
(
method
.
getName
().
startsWith
(
m
.
getName
())){
index
=
i
+
1
;
}
}
}
}
if
(
index
==
-
1
){
if
(
index
==
-
1
||
index
>=
methods
.
size
()
){
innerClass
.
addMethod
(
method
);
innerClass
.
addMethod
(
method
);
}
else
{
}
else
{
methods
.
add
(
index
,
method
);
methods
.
add
(
index
,
method
);
}
}
}
}
/**
* 在最佳位置添加方法
*
* @param interfacz
* @param method
*/
public
static
void
addMethodWithBestPosition
(
Interface
interfacz
,
Method
method
){
List
<
Method
>
methods
=
interfacz
.
getMethods
();
int
index
=
-
1
;
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
()){
index
=
i
+
1
;
}
else
{
index
=
i
;
}
}
else
if
(
m
.
getName
().
startsWith
(
method
.
getName
())){
if
(
index
==
-
1
){
index
=
i
;
}
}
else
if
(
method
.
getName
().
startsWith
(
m
.
getName
())){
index
=
i
+
1
;
}
}
if
(
index
==
-
1
||
index
>=
methods
.
size
()){
interfacz
.
addMethod
(
method
);
}
else
{
methods
.
add
(
index
,
method
);
}
}
/**
* 在最佳位置添加节点
*
* @param rootElement
* @param element
*/
public
static
void
addElementWithBestPosition
(
XmlElement
rootElement
,
XmlElement
element
){
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
;
}
}
}
}
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"
)){
return
attribute
.
getValue
();
}
}
return
null
;
}
}
}
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