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
d0afad6f
Commit
d0afad6f
authored
Jul 28, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
官方ConstructorBased配置BUG临时修正插件
parent
9d817f09
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
104 additions
and
72 deletions
+104
-72
src/main/java/com/itfsw/mybatis/generator/plugins/ConstructorBasedBugFixPlugin.java
...batis/generator/plugins/ConstructorBasedBugFixPlugin.java
+104
-0
src/main/java/com/itfsw/mybatis/generator/plugins/SelectOneByExamplePlugin.java
...w/mybatis/generator/plugins/SelectOneByExamplePlugin.java
+0
-72
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/ConstructorBasedBugFixPlugin.java
0 → 100644
View file @
d0afad6f
/*
* 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.BasePlugin
;
import
com.itfsw.mybatis.generator.plugins.utils.FormatTools
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.JavaVisibility
;
import
org.mybatis.generator.api.dom.java.Method
;
import
org.mybatis.generator.api.dom.java.Parameter
;
import
org.mybatis.generator.api.dom.java.TopLevelClass
;
import
java.util.List
;
/**
* ---------------------------------------------------------------------------
* constructorBased 官方 bug 修正插件
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/7/28 11:05
* ---------------------------------------------------------------------------
*/
public
class
ConstructorBasedBugFixPlugin
extends
BasePlugin
{
/**
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
* @param topLevelClass
* @param introspectedTable
* @return
*/
@Override
public
boolean
modelBaseRecordClassGenerated
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
// 有种情况下ModelBaseRecordClass不会生成不包含BLOBs的构造方法,造成selectOneByExample返回结果找不到对应的构造方法异常
if
(
introspectedTable
.
hasBLOBColumns
()
&&
!
introspectedTable
.
getRules
().
generateRecordWithBLOBsClass
())
{
// 添加没有BLOBs的构造方法
Method
method
=
new
Method
();
method
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
method
.
setConstructor
(
true
);
method
.
setName
(
topLevelClass
.
getType
().
getShortName
());
commentGenerator
.
addGeneralMethodComment
(
method
,
introspectedTable
);
// 使用没有blobs的字段
List
<
IntrospectedColumn
>
constructorColumns
=
introspectedTable
.
getNonBLOBColumns
();
for
(
IntrospectedColumn
introspectedColumn
:
constructorColumns
)
{
method
.
addParameter
(
new
Parameter
(
introspectedColumn
.
getFullyQualifiedJavaType
(),
introspectedColumn
.
getJavaProperty
()));
topLevelClass
.
addImportedType
(
introspectedColumn
.
getFullyQualifiedJavaType
());
}
StringBuilder
sb
=
new
StringBuilder
();
if
(
introspectedTable
.
getRules
().
generatePrimaryKeyClass
())
{
boolean
comma
=
false
;
sb
.
append
(
"super("
);
for
(
IntrospectedColumn
introspectedColumn
:
introspectedTable
.
getPrimaryKeyColumns
())
{
if
(
comma
)
{
sb
.
append
(
", "
);
}
else
{
comma
=
true
;
}
sb
.
append
(
introspectedColumn
.
getJavaProperty
());
}
sb
.
append
(
");"
);
method
.
addBodyLine
(
sb
.
toString
());
}
List
<
IntrospectedColumn
>
introspectedColumns
;
if
(!
introspectedTable
.
getRules
().
generatePrimaryKeyClass
()
&&
introspectedTable
.
hasPrimaryKeyColumns
())
{
introspectedColumns
=
introspectedTable
.
getNonBLOBColumns
();
}
else
{
introspectedColumns
=
introspectedTable
.
getBaseColumns
();
}
for
(
IntrospectedColumn
introspectedColumn
:
introspectedColumns
)
{
sb
.
setLength
(
0
);
sb
.
append
(
"this."
);
sb
.
append
(
introspectedColumn
.
getJavaProperty
());
sb
.
append
(
" = "
);
sb
.
append
(
introspectedColumn
.
getJavaProperty
());
sb
.
append
(
';'
);
method
.
addBodyLine
(
sb
.
toString
());
}
FormatTools
.
addMethodWithBestPosition
(
topLevelClass
,
method
);
}
return
true
;
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/SelectOneByExamplePlugin.java
View file @
d0afad6f
...
...
@@ -20,7 +20,6 @@ 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.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.Attribute
;
...
...
@@ -28,9 +27,6 @@ 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
java.util.ArrayList
;
import
java.util.List
;
import
static
org
.
mybatis
.
generator
.
internal
.
util
.
StringUtility
.
stringHasValue
;
/**
...
...
@@ -45,74 +41,6 @@ public class SelectOneByExamplePlugin extends BasePlugin {
public
static
final
String
METHOD_SELECT_ONE_BY_EXAMPLE
=
"selectOneByExample"
;
// 方法名
public
static
final
String
METHOD_SELECT_ONE_BY_EXAMPLE_WITH_BLOBS
=
"selectOneByExampleWithBLOBs"
;
// 方法名
@Override
public
boolean
modelBaseRecordClassGenerated
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
// 有种情况下ModelBaseRecordClass不会生成不包含BLOBs的构造方法,造成selectOneByExample返回结果找不到对应的构造方法异常
if
(
introspectedTable
.
hasBLOBColumns
()
&&
!
introspectedTable
.
getRules
().
generateRecordWithBLOBsClass
())
{
// 添加没有BLOBs的构造方法
Method
method
=
new
Method
();
method
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
method
.
setConstructor
(
true
);
method
.
setName
(
topLevelClass
.
getType
().
getShortName
());
commentGenerator
.
addGeneralMethodComment
(
method
,
introspectedTable
);
// 使用没有blobs的字段
List
<
IntrospectedColumn
>
constructorColumns
=
introspectedTable
.
getNonBLOBColumns
();
for
(
IntrospectedColumn
introspectedColumn
:
constructorColumns
)
{
method
.
addParameter
(
new
Parameter
(
introspectedColumn
.
getFullyQualifiedJavaType
(),
introspectedColumn
.
getJavaProperty
()));
topLevelClass
.
addImportedType
(
introspectedColumn
.
getFullyQualifiedJavaType
());
}
StringBuilder
sb
=
new
StringBuilder
();
if
(
introspectedTable
.
getRules
().
generatePrimaryKeyClass
())
{
boolean
comma
=
false
;
sb
.
append
(
"super("
);
for
(
IntrospectedColumn
introspectedColumn
:
introspectedTable
.
getPrimaryKeyColumns
())
{
if
(
comma
)
{
sb
.
append
(
", "
);
}
else
{
comma
=
true
;
}
sb
.
append
(
introspectedColumn
.
getJavaProperty
());
}
sb
.
append
(
");"
);
method
.
addBodyLine
(
sb
.
toString
());
}
List
<
IntrospectedColumn
>
introspectedColumns
;
if
(!
introspectedTable
.
getRules
().
generatePrimaryKeyClass
()
&&
introspectedTable
.
hasPrimaryKeyColumns
())
{
introspectedColumns
=
introspectedTable
.
getNonBLOBColumns
();
}
else
{
introspectedColumns
=
introspectedTable
.
getBaseColumns
();
}
for
(
IntrospectedColumn
introspectedColumn
:
introspectedColumns
)
{
sb
.
setLength
(
0
);
sb
.
append
(
"this."
);
sb
.
append
(
introspectedColumn
.
getJavaProperty
());
sb
.
append
(
" = "
);
sb
.
append
(
introspectedColumn
.
getJavaProperty
());
sb
.
append
(
';'
);
method
.
addBodyLine
(
sb
.
toString
());
}
// 找到原有构造方法位置,保持构造方法放在一起美观
ArrayList
<
Method
>
methods
=
(
ArrayList
<
Method
>)
topLevelClass
.
getMethods
();
int
index
=
0
;
for
(
Method
m
:
methods
)
{
index
++;
if
(
m
.
getName
().
equals
(
topLevelClass
.
getType
().
getShortName
()))
{
break
;
}
}
methods
.
add
(
index
,
method
);
}
return
true
;
}
/**
* Java Client Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
...
...
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