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
33c2bf94
Commit
33c2bf94
authored
Jan 06, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
项目初始提交
parent
6c7b5711
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
991 additions
and
0 deletions
+991
-0
.gitignore
.gitignore
+2
-0
pom.xml
pom.xml
+162
-0
src/main/java/com/itfsw/mybatis/generator/plugins/CriteriaBuilderPlugin.java
...tfsw/mybatis/generator/plugins/CriteriaBuilderPlugin.java
+105
-0
src/main/java/com/itfsw/mybatis/generator/plugins/LimitPlugin.java
...java/com/itfsw/mybatis/generator/plugins/LimitPlugin.java
+195
-0
src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
...m/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
+130
-0
src/main/java/com/itfsw/mybatis/generator/plugins/SelectOneByExamplePlugin.java
...w/mybatis/generator/plugins/SelectOneByExamplePlugin.java
+179
-0
src/main/java/com/itfsw/mybatis/generator/plugins/utils/CommentTools.java
...m/itfsw/mybatis/generator/plugins/utils/CommentTools.java
+118
-0
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java
...tis/generator/plugins/utils/XmlElementGeneratorTools.java
+100
-0
No files found.
.gitignore
View file @
33c2bf94
...
@@ -10,3 +10,5 @@
...
@@ -10,3 +10,5 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
hs_err_pid*
.idea/
mybatis-generator-plugin.iml
pom.xml
0 → 100644
View file @
33c2bf94
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.itfsw
</groupId>
<artifactId>
mybatis-generator-plugin
</artifactId>
<version>
1.0.0
</version>
<packaging>
jar
</packaging>
<name>
mybatis-generator-plugin
</name>
<description>
Mybatis Generator Plugins
</description>
<url>
https://github.com/itfsw/mybatis-generator-plugin
</url>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ license ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<licenses>
<license>
<name>
The Apache License, Version 2.0
</name>
<url>
http://www.apache.org/licenses/LICENSE-2.0.txt
</url>
</license>
</licenses>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++项目properties配置++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<properties>
<!-- java version -->
<java.version>
1.8
</java.version>
<!-- 构建编码 -->
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++依赖管理++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<dependencies>
<!-- mybatis generator -->
<dependency>
<groupId>
org.mybatis.generator
</groupId>
<artifactId>
mybatis-generator-core
</artifactId>
<version>
1.3.5
</version>
<scope>
provided
</scope>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
slf4j-api
</artifactId>
<version>
1.7.22
</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
3.8.1
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++profiles++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<profiles>
<profile>
<id>
release
</id>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-source-plugin
</artifactId>
<version>
3.0.1
</version>
<executions>
<execution>
<id>
attach-sources
</id>
<goals>
<goal>
jar
</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-javadoc-plugin
</artifactId>
<version>
2.10.4
</version>
<configuration>
<charset>
${project.build.sourceEncoding}
</charset>
<encoding>
${project.build.sourceEncoding}
</encoding>
<docencoding>
${project.build.sourceEncoding}
</docencoding>
</configuration>
<executions>
<execution>
<id>
attach-javadocs
</id>
<goals>
<goal>
jar
</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- gpg 签名 -->
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-gpg-plugin
</artifactId>
<version>
1.6
</version>
<executions>
<execution>
<id>
sign-artifacts
</id>
<phase>
verify
</phase>
<goals>
<goal>
sign
</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++build++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<build>
<!-- 配置java版本 不配置的话默认父类配置的是1.6-->
<pluginManagement>
<plugins>
<!-- 由于历史原因 maven-compiler只支持编译Java 1.3 所以要自己手动配置其支持1.5 不然跑test的时候不能使用
@Test 注解!! -->
<plugin>
<artifactId>
maven-compiler-plugin
</artifactId>
<configuration>
<source>
${java.version}
</source>
<target>
${java.version}
</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++developers++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<developers>
<developer>
<name>
hewei
</name>
<email>
hewei@itfsw.com
</email>
<organization>
itfsw
</organization>
<organizationUrl>
http://blog.itfsw.com
</organizationUrl>
</developer>
</developers>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++scm++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<scm>
<connection>
scm:git:https://github.com/itfsw/mybatis-generator-plugin.git
</connection>
<developerConnection>
scm:git:https://github.com/itfsw/mybatis-generator-plugin.git
</developerConnection>
<url>
https://github.com/itfsw/mybatis-generator-plugin
</url>
<tag>
v1.0.0
</tag>
</scm>
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++distribution++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<distributionManagement>
<snapshotRepository>
<id>
sonatype
</id>
<url>
https://oss.sonatype.org/content/repositories/snapshots
</url>
</snapshotRepository>
<repository>
<id>
sonatype
</id>
<url>
https://oss.sonatype.org/service/local/staging/deploy/maven2/
</url>
</repository>
</distributionManagement>
</project>
src/main/java/com/itfsw/mybatis/generator/plugins/CriteriaBuilderPlugin.java
0 → 100644
View file @
33c2bf94
/*
* Copyright (c) 2014.
*
* 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.CommentTools
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.PluginAdapter
;
import
org.mybatis.generator.api.dom.java.*
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.List
;
/**
* ---------------------------------------------------------------------------
* 增加Criteria Builder方法
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2016/12/28 14:56
* ---------------------------------------------------------------------------
*/
public
class
CriteriaBuilderPlugin
extends
PluginAdapter
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
CriteriaBuilderPlugin
.
class
);
/**
* {@inheritDoc}
*/
public
boolean
validate
(
List
<
String
>
warnings
)
{
return
true
;
}
/**
* ModelExample Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param topLevelClass
* @param introspectedTable
* @return
* @author hewei
*/
@Override
public
boolean
modelExampleClassGenerated
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
List
<
InnerClass
>
innerClasses
=
topLevelClass
.
getInnerClasses
();
for
(
InnerClass
innerClass
:
innerClasses
)
{
if
(
"Criteria"
.
equals
(
innerClass
.
getType
().
getShortName
()))
{
addFactoryMethodToCriteria
(
topLevelClass
,
innerClass
,
introspectedTable
);
}
}
List
<
Method
>
methods
=
topLevelClass
.
getMethods
();
for
(
Method
method
:
methods
)
{
if
(!
"createCriteriaInternal"
.
equals
(
method
.
getName
()))
continue
;
method
.
getBodyLines
().
set
(
0
,
"Criteria criteria = new Criteria(this);"
);
logger
.
info
(
"hw:CriteriaBuilder修改Example的createCriteriaInternal方法,修改构造Criteria时传入Example对象"
);
}
return
true
;
}
/**
* 添加工厂方法
*
* @param topLevelClass
* @param innerClass
* @param introspectedTable
* @author hewei
*/
private
void
addFactoryMethodToCriteria
(
TopLevelClass
topLevelClass
,
InnerClass
innerClass
,
IntrospectedTable
introspectedTable
)
{
Field
f
=
new
Field
(
"example"
,
topLevelClass
.
getType
());
f
.
setVisibility
(
JavaVisibility
.
PRIVATE
);
innerClass
.
addField
(
f
);
// overwrite constructor
List
<
Method
>
methods
=
innerClass
.
getMethods
();
for
(
Method
method
:
methods
)
{
if
(
method
.
isConstructor
())
{
method
.
addParameter
(
new
Parameter
(
topLevelClass
.
getType
(),
"example"
));
method
.
addBodyLine
(
"this.example = example;"
);
logger
.
info
(
"hw:CriteriaBuilder修改Criteria的构造方法,增加example参数"
);
}
}
// add factory method "example"
Method
method
=
new
Method
(
"example"
);
method
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
method
.
setReturnType
(
topLevelClass
.
getType
());
method
.
addBodyLine
(
"return this.example;"
);
CommentTools
.
addGeneralMethodComment
(
method
,
introspectedTable
);
innerClass
.
addMethod
(
method
);
logger
.
info
(
"hw:CriteriaBuilder增加工厂方法example"
);
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/LimitPlugin.java
0 → 100644
View file @
33c2bf94
/*
* Copyright (c) 2014.
*
* 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.CommentTools
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.PluginAdapter
;
import
org.mybatis.generator.api.dom.java.*
;
import
org.mybatis.generator.api.dom.xml.Attribute
;
import
org.mybatis.generator.api.dom.xml.TextElement
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.List
;
/**
* ---------------------------------------------------------------------------
* 增加分页方法
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2016/12/29 18:14
* ---------------------------------------------------------------------------
*/
public
class
LimitPlugin
extends
PluginAdapter
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
LimitPlugin
.
class
);
/**
* {@inheritDoc}
*/
public
boolean
validate
(
List
<
String
>
warnings
)
{
return
true
;
}
/**
* ModelExample Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param topLevelClass
* @param introspectedTable
* @return
* @author hewei
*/
@Override
public
boolean
modelExampleClassGenerated
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
PrimitiveTypeWrapper
integerWrapper
=
FullyQualifiedJavaType
.
getIntInstance
().
getPrimitiveTypeWrapper
();
// 添加offset和rows字段
Field
offset
=
new
Field
();
offset
.
setName
(
"offset"
);
offset
.
setVisibility
(
JavaVisibility
.
PROTECTED
);
offset
.
setType
(
integerWrapper
);
CommentTools
.
addFieldComment
(
offset
,
introspectedTable
);
topLevelClass
.
addField
(
offset
);
Field
rows
=
new
Field
();
rows
.
setName
(
"rows"
);
rows
.
setVisibility
(
JavaVisibility
.
PROTECTED
);
rows
.
setType
(
integerWrapper
);
CommentTools
.
addFieldComment
(
rows
,
introspectedTable
);
topLevelClass
.
addField
(
rows
);
logger
.
info
(
"hw:分页插件增加Example的offset和rows字段"
);
// 增加getter && setter 方法
Method
setOffset
=
new
Method
();
setOffset
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
setOffset
.
setName
(
"setOffset"
);
setOffset
.
addParameter
(
new
Parameter
(
integerWrapper
,
"offset"
));
setOffset
.
addBodyLine
(
"this.offset = offset;"
);
CommentTools
.
addGeneralMethodComment
(
setOffset
,
introspectedTable
);
topLevelClass
.
addMethod
(
setOffset
);
Method
getOffset
=
new
Method
();
getOffset
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
getOffset
.
setReturnType
(
integerWrapper
);
getOffset
.
setName
(
"getOffset"
);
getOffset
.
addBodyLine
(
"return offset;"
);
CommentTools
.
addGeneralMethodComment
(
getOffset
,
introspectedTable
);
topLevelClass
.
addMethod
(
getOffset
);
Method
setRows
=
new
Method
();
setRows
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
setRows
.
setName
(
"setRows"
);
setRows
.
addParameter
(
new
Parameter
(
integerWrapper
,
"rows"
));
setRows
.
addBodyLine
(
"this.rows = rows;"
);
CommentTools
.
addGeneralMethodComment
(
setRows
,
introspectedTable
);
topLevelClass
.
addMethod
(
setRows
);
Method
getRows
=
new
Method
();
getRows
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
getRows
.
setReturnType
(
integerWrapper
);
getRows
.
setName
(
"getRows"
);
getRows
.
addBodyLine
(
"return rows;"
);
CommentTools
.
addGeneralMethodComment
(
getRows
,
introspectedTable
);
topLevelClass
.
addMethod
(
getRows
);
logger
.
info
(
"hw:分页插件增加Example的offset和rows的getter和setter"
);
// 提供几个快捷方法
Method
setLimit
=
new
Method
();
setLimit
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
setLimit
.
setName
(
"limit"
);
setLimit
.
addParameter
(
new
Parameter
(
integerWrapper
,
"rows"
));
setLimit
.
addBodyLine
(
"this.rows = rows;"
);
CommentTools
.
addGeneralMethodComment
(
setLimit
,
introspectedTable
);
topLevelClass
.
addMethod
(
setLimit
);
Method
setLimit2
=
new
Method
();
setLimit2
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
setLimit2
.
setName
(
"limit"
);
setLimit2
.
addParameter
(
new
Parameter
(
integerWrapper
,
"offset"
));
setLimit2
.
addParameter
(
new
Parameter
(
integerWrapper
,
"rows"
));
setLimit2
.
addBodyLine
(
"this.offset = offset;"
);
setLimit2
.
addBodyLine
(
"this.rows = rows;"
);
CommentTools
.
addGeneralMethodComment
(
setLimit2
,
introspectedTable
);
topLevelClass
.
addMethod
(
setLimit2
);
logger
.
info
(
"hw:分页插件增加Example的limit方法"
);
Method
setPage
=
new
Method
();
setPage
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
setPage
.
setName
(
"page"
);
setPage
.
addParameter
(
new
Parameter
(
integerWrapper
,
"page"
));
setPage
.
addParameter
(
new
Parameter
(
integerWrapper
,
"pageSize"
));
setPage
.
addBodyLine
(
"this.offset = page * pageSize;"
);
setPage
.
addBodyLine
(
"this.rows = pageSize;"
);
CommentTools
.
addGeneralMethodComment
(
setPage
,
introspectedTable
);
topLevelClass
.
addMethod
(
setPage
);
logger
.
info
(
"hw:分页插件增加Example的page方法"
);
// !!! clear 方法增加 offset 和 rows的清理
List
<
Method
>
methodList
=
topLevelClass
.
getMethods
();
for
(
Method
method:
methodList
){
if
(
method
.
getName
().
equals
(
"clear"
)){
method
.
addBodyLine
(
"rows = null;"
);
method
.
addBodyLine
(
"offset = null;"
);
logger
.
info
(
"hw:分页插件修正Example的clear方法,增加rows和offset字段的清空"
);
}
}
return
true
;
}
/**
* SQL Map Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param element
* @param introspectedTable
* @return
* @author hewei
*/
@Override
public
boolean
sqlMapSelectByExampleWithoutBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
XmlElement
ifLimitNotNullElement
=
new
XmlElement
(
"if"
);
ifLimitNotNullElement
.
addAttribute
(
new
Attribute
(
"test"
,
"rows != null"
));
XmlElement
ifOffsetNotNullElement
=
new
XmlElement
(
"if"
);
ifOffsetNotNullElement
.
addAttribute
(
new
Attribute
(
"test"
,
"offset != null"
));
ifOffsetNotNullElement
.
addElement
(
new
TextElement
(
"limit ${offset}, ${rows}"
));
ifLimitNotNullElement
.
addElement
(
ifOffsetNotNullElement
);
XmlElement
ifOffsetNullElement
=
new
XmlElement
(
"if"
);
ifOffsetNullElement
.
addAttribute
(
new
Attribute
(
"test"
,
"offset == null"
));
ifOffsetNullElement
.
addElement
(
new
TextElement
(
"limit ${rows}"
));
ifLimitNotNullElement
.
addElement
(
ifOffsetNullElement
);
element
.
addElement
(
ifLimitNotNullElement
);
return
true
;
}
/**
* SQL Map Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param element
* @param introspectedTable
* @return
* @author hewei
*/
@Override
public
boolean
sqlMapSelectByExampleWithBLOBsElementGenerated
(
XmlElement
element
,
IntrospectedTable
introspectedTable
)
{
return
this
.
sqlMapSelectByExampleWithoutBLOBsElementGenerated
(
element
,
introspectedTable
);
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java
0 → 100644
View file @
33c2bf94
/*
* Copyright (c) 2014.
*
* 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.CommentTools
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.PluginAdapter
;
import
org.mybatis.generator.api.dom.java.*
;
import
org.mybatis.generator.internal.util.JavaBeansUtil
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.List
;
/**
* ---------------------------------------------------------------------------
* 增加Model Builder方法
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2016/12/28 14:56
* ---------------------------------------------------------------------------
*/
public
class
ModelBuilderPlugin
extends
PluginAdapter
{
public
static
final
String
BUILDER_CLASS_NAME
=
"Builder"
;
// Builder 类名
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ModelBuilderPlugin
.
class
);
/**
* {@inheritDoc}
*/
public
boolean
validate
(
List
<
String
>
warnings
)
{
return
true
;
}
/**
* Model Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param topLevelClass
* @param introspectedTable
* @return
* @author hewei
*/
@Override
public
boolean
modelBaseRecordClassGenerated
(
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
List
<
Field
>
fields
=
topLevelClass
.
getFields
();
// 生成内部Builder类
InnerClass
innerClass
=
new
InnerClass
(
BUILDER_CLASS_NAME
);
innerClass
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
innerClass
.
setStatic
(
true
);
CommentTools
.
addClassComment
(
innerClass
,
introspectedTable
);
logger
.
info
(
"hw:生成内部Builder类"
);
// 构建内部obj变量
Field
f
=
new
Field
(
"obj"
,
topLevelClass
.
getType
());
f
.
setVisibility
(
JavaVisibility
.
PRIVATE
);
innerClass
.
addField
(
f
);
// 构造构造方法
Method
constructor
=
new
Method
(
BUILDER_CLASS_NAME
);
constructor
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
constructor
.
setConstructor
(
true
);
constructor
.
addBodyLine
(
new
StringBuilder
(
"this.obj = new "
)
.
append
(
topLevelClass
.
getType
().
getShortName
()).
append
(
"();"
).
toString
());
innerClass
.
addMethod
(
constructor
);
logger
.
info
(
"hw:生成内部Builder类构造方法"
);
// !!可能Model存在复合主键情况,字段要加上这些
if
(
topLevelClass
.
getSuperClass
()
!=
null
&&
topLevelClass
.
getSuperClass
().
compareTo
(
new
FullyQualifiedJavaType
(
introspectedTable
.
getPrimaryKeyType
()))
==
0
){
for
(
IntrospectedColumn
introspectedColumn
:
introspectedTable
.
getPrimaryKeyColumns
())
{
Field
field
=
JavaBeansUtil
.
getJavaBeansField
(
introspectedColumn
,
context
,
introspectedTable
);
Method
setterMethod
=
JavaBeansUtil
.
getJavaBeansSetter
(
introspectedColumn
,
context
,
introspectedTable
);
Method
method
=
new
Method
(
field
.
getName
());
method
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
method
.
setReturnType
(
innerClass
.
getType
());
method
.
addParameter
(
new
Parameter
(
field
.
getType
(),
field
.
getName
()));
method
.
addBodyLine
(
new
StringBuilder
().
append
(
"obj."
).
append
(
setterMethod
.
getName
())
.
append
(
"("
).
append
(
field
.
getName
()).
append
(
");"
).
toString
());
method
.
addBodyLine
(
new
StringBuilder
().
append
(
"return this;"
).
toString
());
CommentTools
.
addGeneralMethodComment
(
method
,
introspectedTable
);
innerClass
.
addMethod
(
method
);
logger
.
info
(
"hw:生成内部Builder类的复合主键字段对应方法"
+
field
.
getName
());
}
}
// 根据Model属性生成链式赋值方法
for
(
Field
field
:
fields
)
{
if
(
field
.
isStatic
())
continue
;
Method
method
=
new
Method
(
field
.
getName
());
method
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
method
.
setReturnType
(
innerClass
.
getType
());
method
.
addParameter
(
new
Parameter
(
field
.
getType
(),
field
.
getName
()));
method
.
addBodyLine
(
new
StringBuilder
().
append
(
"obj."
).
append
(
field
.
getName
())
.
append
(
" = "
).
append
(
field
.
getName
()).
append
(
";"
).
toString
());
method
.
addBodyLine
(
new
StringBuilder
().
append
(
"return this;"
).
toString
());
CommentTools
.
addGeneralMethodComment
(
method
,
introspectedTable
);
innerClass
.
addMethod
(
method
);
logger
.
info
(
"hw:生成内部Builder类的普通字段对应方法"
+
field
.
getName
());
}
Method
build
=
new
Method
(
"build"
);
build
.
setReturnType
(
topLevelClass
.
getType
());
build
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
build
.
addBodyLine
(
"return this.obj;"
);
CommentTools
.
addGeneralMethodComment
(
build
,
introspectedTable
);
innerClass
.
addMethod
(
build
);
logger
.
info
(
"hw:生成内部Builder类的build方法"
);
topLevelClass
.
addInnerClass
(
innerClass
);
return
true
;
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/SelectOneByExamplePlugin.java
0 → 100644
View file @
33c2bf94
/*
* Copyright (c) 2014.
*
* 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.CommentTools
;
import
com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.PluginAdapter
;
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.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.List
;
import
static
org
.
mybatis
.
generator
.
internal
.
util
.
StringUtility
.
stringHasValue
;
/**
* ---------------------------------------------------------------------------
* 增加查询一条数据方法
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2016/12/28 14:56
* ---------------------------------------------------------------------------
*/
public
class
SelectOneByExamplePlugin
extends
PluginAdapter
{
public
static
final
String
METHOD_NAME
=
"selectOneByExample"
;
// 方法名
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
SelectOneByExamplePlugin
.
class
);
/**
* {@inheritDoc}
*/
@Override
public
boolean
validate
(
List
<
String
>
warnings
)
{
return
true
;
}
/**
* Java Client Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param interfaze
* @param topLevelClass
* @param introspectedTable
* @return
* @author hewei
*/
@Override
public
boolean
clientGenerated
(
Interface
interfaze
,
TopLevelClass
topLevelClass
,
IntrospectedTable
introspectedTable
)
{
logger
.
info
(
"hw:生成"
+
interfaze
.
getType
()+
"对应SelectOneByExample方法..."
);
// 方法生成
Method
method
=
new
Method
(
METHOD_NAME
);
// 方法可见性
method
.
setVisibility
(
JavaVisibility
.
PUBLIC
);
// 返回值类型
FullyQualifiedJavaType
returnType
=
introspectedTable
.
getRules
().
calculateAllFieldsClass
();
method
.
setReturnType
(
returnType
);
// 添加参数
FullyQualifiedJavaType
type
=
new
FullyQualifiedJavaType
(
introspectedTable
.
getExampleType
());
method
.
addParameter
(
new
Parameter
(
type
,
"example"
));
// 添加方法说明
CommentTools
.
addGeneralMethodComment
(
method
,
introspectedTable
);
// interface 增加方法
interfaze
.
addMethod
(
method
);
return
true
;
}
/**
* SQL Map Methods 生成
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
*
* @param document
* @param introspectedTable
* @return
*/
@Override
public
boolean
sqlMapDocumentGenerated
(
Document
document
,
IntrospectedTable
introspectedTable
)
{
logger
.
info
(
"hw:生成表"
+
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
()+
"("
+
(
introspectedTable
.
hasBLOBColumns
()
?
"有"
:
"无"
)+
"Blob类型)的mapping.xml对应SelectOneByExample方法..."
);
//数据库表名
String
tableName
=
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
();
// 生成查询语句
XmlElement
selectOneElement
=
new
XmlElement
(
"select"
);
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools
.
addComment
(
selectOneElement
);
// 添加ID
selectOneElement
.
addAttribute
(
new
Attribute
(
"id"
,
METHOD_NAME
));
// ----------------------------------------- 表中是否有blob类型字段 ---------------------------------------
if
(
introspectedTable
.
hasBLOBColumns
()){
// 添加返回类型
selectOneElement
.
addAttribute
(
new
Attribute
(
"resultMap"
,
introspectedTable
.
getResultMapWithBLOBsId
()));
// 添加参数类型
selectOneElement
.
addAttribute
(
new
Attribute
(
"parameterType"
,
introspectedTable
.
getExampleType
()));
// 添加查询SQL
selectOneElement
.
addElement
(
new
TextElement
(
"select"
));
//$NON-NLS-1$
StringBuilder
sb
=
new
StringBuilder
();
if
(
stringHasValue
(
introspectedTable
.
getSelectByExampleQueryId
()))
{
sb
.
append
(
'\''
);
sb
.
append
(
introspectedTable
.
getSelectByExampleQueryId
());
sb
.
append
(
"' as QUERYID,"
);
//$NON-NLS-1$
selectOneElement
.
addElement
(
new
TextElement
(
sb
.
toString
()));
}
selectOneElement
.
addElement
(
XmlElementGeneratorTools
.
getBaseColumnListElement
(
introspectedTable
));
selectOneElement
.
addElement
(
new
TextElement
(
","
));
//$NON-NLS-1$
selectOneElement
.
addElement
(
XmlElementGeneratorTools
.
getBlobColumnListElement
(
introspectedTable
));
sb
.
setLength
(
0
);
sb
.
append
(
"from "
);
//$NON-NLS-1$
sb
.
append
(
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
());
selectOneElement
.
addElement
(
new
TextElement
(
sb
.
toString
()));
selectOneElement
.
addElement
(
XmlElementGeneratorTools
.
getExampleIncludeElement
(
introspectedTable
));
XmlElement
ifElement
=
new
XmlElement
(
"if"
);
//$NON-NLS-1$
ifElement
.
addAttribute
(
new
Attribute
(
"test"
,
"orderByClause != null"
));
//$NON-NLS-1$ //$NON-NLS-2$
ifElement
.
addElement
(
new
TextElement
(
"order by ${orderByClause}"
));
//$NON-NLS-1$
selectOneElement
.
addElement
(
ifElement
);
// 只查询一条
selectOneElement
.
addElement
(
new
TextElement
(
"limit 1"
));
}
else
{
// 添加返回类型
selectOneElement
.
addAttribute
(
new
Attribute
(
"resultMap"
,
introspectedTable
.
getBaseResultMapId
()));
// 添加参数类型
selectOneElement
.
addAttribute
(
new
Attribute
(
"parameterType"
,
introspectedTable
.
getExampleType
()));
selectOneElement
.
addElement
(
new
TextElement
(
"select"
));
//$NON-NLS-1$
StringBuilder
sb
=
new
StringBuilder
();
if
(
stringHasValue
(
introspectedTable
.
getSelectByExampleQueryId
()))
{
sb
.
append
(
'\''
);
sb
.
append
(
introspectedTable
.
getSelectByExampleQueryId
());
sb
.
append
(
"' as QUERYID,"
);
//$NON-NLS-1$
selectOneElement
.
addElement
(
new
TextElement
(
sb
.
toString
()));
}
selectOneElement
.
addElement
(
XmlElementGeneratorTools
.
getBaseColumnListElement
(
introspectedTable
));
sb
.
setLength
(
0
);
sb
.
append
(
"from "
);
//$NON-NLS-1$
sb
.
append
(
introspectedTable
.
getAliasedFullyQualifiedTableNameAtRuntime
());
selectOneElement
.
addElement
(
new
TextElement
(
sb
.
toString
()));
selectOneElement
.
addElement
(
XmlElementGeneratorTools
.
getExampleIncludeElement
(
introspectedTable
));
XmlElement
ifElement
=
new
XmlElement
(
"if"
);
//$NON-NLS-1$
ifElement
.
addAttribute
(
new
Attribute
(
"test"
,
"orderByClause != null"
));
//$NON-NLS-1$ //$NON-NLS-2$
ifElement
.
addElement
(
new
TextElement
(
"order by ${orderByClause}"
));
//$NON-NLS-1$
selectOneElement
.
addElement
(
ifElement
);
// 只查询一条
selectOneElement
.
addElement
(
new
TextElement
(
"limit 1"
));
}
// 添加到根节点
document
.
getRootElement
().
addElement
(
selectOneElement
);
return
true
;
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/CommentTools.java
0 → 100644
View file @
33c2bf94
/*
* Copyright (c) 2014.
*
* 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.IntrospectedTable
;
import
org.mybatis.generator.api.dom.java.Field
;
import
org.mybatis.generator.api.dom.java.InnerClass
;
import
org.mybatis.generator.api.dom.java.Method
;
import
org.mybatis.generator.api.dom.xml.TextElement
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
org.mybatis.generator.config.MergeConstants
;
/**
* ---------------------------------------------------------------------------
* 插件评论生成工具
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2016/12/28 17:57
* ---------------------------------------------------------------------------
*/
public
class
CommentTools
{
/**
* 生成通用属性注解
*
* @param field
* @param introspectedTable
* @author hewei
*/
public
static
void
addFieldComment
(
Field
field
,
IntrospectedTable
introspectedTable
)
{
StringBuilder
sb
=
new
StringBuilder
();
field
.
addJavaDocLine
(
"/**"
);
//$NON-NLS-1$
field
.
addJavaDocLine
(
" * 这是Mybatis Generator拓展插件生成的属性(请勿删除)."
);
//$NON-NLS-1$
sb
.
append
(
" * This field corresponds to the database table "
);
//$NON-NLS-1$
sb
.
append
(
introspectedTable
.
getFullyQualifiedTable
());
field
.
addJavaDocLine
(
sb
.
toString
());
field
.
addJavaDocLine
(
" *"
);
field
.
addJavaDocLine
(
" * "
+
MergeConstants
.
NEW_ELEMENT_TAG
);
field
.
addJavaDocLine
(
" * @author hewei"
);
field
.
addJavaDocLine
(
" */"
);
//$NON-NLS-1$
}
/**
* 生成通用内部类注解
*
* @param innerClass 类
* @param introspectedTable 表
* @author hewei
*/
public
static
void
addClassComment
(
InnerClass
innerClass
,
IntrospectedTable
introspectedTable
)
{
StringBuilder
sb
=
new
StringBuilder
();
innerClass
.
addJavaDocLine
(
"/**"
);
//$NON-NLS-1$
innerClass
.
addJavaDocLine
(
" * 这是Mybatis Generator拓展插件生成的类(请勿删除)."
);
//$NON-NLS-1$
sb
.
append
(
" * This class corresponds to the database table "
);
//$NON-NLS-1$
sb
.
append
(
introspectedTable
.
getFullyQualifiedTable
());
innerClass
.
addJavaDocLine
(
sb
.
toString
());
innerClass
.
addJavaDocLine
(
" *"
);
innerClass
.
addJavaDocLine
(
" * "
+
MergeConstants
.
NEW_ELEMENT_TAG
);
innerClass
.
addJavaDocLine
(
" * @author hewei"
);
innerClass
.
addJavaDocLine
(
" */"
);
//$NON-NLS-1$
}
/**
* 生成通用方法注解
*
* @param method 方法
* @param introspectedTable 表
* @author hewei
*/
public
static
void
addGeneralMethodComment
(
Method
method
,
IntrospectedTable
introspectedTable
)
{
StringBuilder
sb
=
new
StringBuilder
();
method
.
addJavaDocLine
(
"/**"
);
//$NON-NLS-1$
method
.
addJavaDocLine
(
" * 这是Mybatis Generator拓展插件生成的方法(请勿删除)."
);
sb
.
append
(
" * This method corresponds to the database table "
);
sb
.
append
(
introspectedTable
.
getFullyQualifiedTable
());
method
.
addJavaDocLine
(
sb
.
toString
());
method
.
addJavaDocLine
(
" *"
);
method
.
addJavaDocLine
(
" * "
+
MergeConstants
.
NEW_ELEMENT_TAG
);
method
.
addJavaDocLine
(
" * @author hewei"
);
method
.
addJavaDocLine
(
" */"
);
//$NON-NLS-1$
}
/**
* 生成xml element 注释
*
* @param xmlElement the xml element
* @author hewei
*/
public
static
void
addComment
(
XmlElement
xmlElement
)
{
xmlElement
.
addElement
(
new
TextElement
(
"<!--"
));
//$NON-NLS-1$
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
" WARNING - "
);
//$NON-NLS-1$
sb
.
append
(
MergeConstants
.
NEW_ELEMENT_TAG
);
xmlElement
.
addElement
(
new
TextElement
(
sb
.
toString
()));
xmlElement
.
addElement
(
new
TextElement
(
" 这个节点为代码生成工具生成,请不要修改!"
));
//$NON-NLS-1$
xmlElement
.
addElement
(
new
TextElement
(
" @author hewei"
));
xmlElement
.
addElement
(
new
TextElement
(
"-->"
));
//$NON-NLS-1$
}
}
src/main/java/com/itfsw/mybatis/generator/plugins/utils/XmlElementGeneratorTools.java
0 → 100644
View file @
33c2bf94
/*
* Copyright (c) 2014.
*
* 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.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.xml.Attribute
;
import
org.mybatis.generator.api.dom.xml.TextElement
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
org.mybatis.generator.config.GeneratedKey
;
/**
* ---------------------------------------------------------------------------
* Xml 节点生成工具 参考 org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2016/12/29 16:47
* ---------------------------------------------------------------------------
*/
public
class
XmlElementGeneratorTools
{
/**
* This method should return an XmlElement for the select key used to
* automatically generate keys.
*
* @param introspectedColumn
* the column related to the select key statement
* @param generatedKey
* the generated key for the current table
* @return the selectKey element
*/
public
static
XmlElement
getSelectKey
(
IntrospectedColumn
introspectedColumn
,
GeneratedKey
generatedKey
)
{
String
identityColumnType
=
introspectedColumn
.
getFullyQualifiedJavaType
().
getFullyQualifiedName
();
XmlElement
answer
=
new
XmlElement
(
"selectKey"
);
//$NON-NLS-1$
answer
.
addAttribute
(
new
Attribute
(
"resultType"
,
identityColumnType
));
//$NON-NLS-1$
answer
.
addAttribute
(
new
Attribute
(
"keyProperty"
,
introspectedColumn
.
getJavaProperty
()));
//$NON-NLS-1$
answer
.
addAttribute
(
new
Attribute
(
"order"
,
//$NON-NLS-1$
generatedKey
.
getMyBatis3Order
()));
answer
.
addElement
(
new
TextElement
(
generatedKey
.
getRuntimeSqlStatement
()));
return
answer
;
}
public
static
XmlElement
getBaseColumnListElement
(
IntrospectedTable
introspectedTable
)
{
XmlElement
answer
=
new
XmlElement
(
"include"
);
//$NON-NLS-1$
answer
.
addAttribute
(
new
Attribute
(
"refid"
,
//$NON-NLS-1$
introspectedTable
.
getBaseColumnListId
()));
return
answer
;
}
public
static
XmlElement
getBlobColumnListElement
(
IntrospectedTable
introspectedTable
)
{
XmlElement
answer
=
new
XmlElement
(
"include"
);
//$NON-NLS-1$
answer
.
addAttribute
(
new
Attribute
(
"refid"
,
//$NON-NLS-1$
introspectedTable
.
getBlobColumnListId
()));
return
answer
;
}
public
static
XmlElement
getExampleIncludeElement
(
IntrospectedTable
introspectedTable
)
{
XmlElement
ifElement
=
new
XmlElement
(
"if"
);
//$NON-NLS-1$
ifElement
.
addAttribute
(
new
Attribute
(
"test"
,
"_parameter != null"
));
//$NON-NLS-1$ //$NON-NLS-2$
XmlElement
includeElement
=
new
XmlElement
(
"include"
);
//$NON-NLS-1$
includeElement
.
addAttribute
(
new
Attribute
(
"refid"
,
//$NON-NLS-1$
introspectedTable
.
getExampleWhereClauseId
()));
ifElement
.
addElement
(
includeElement
);
return
ifElement
;
}
public
static
XmlElement
getUpdateByExampleIncludeElement
(
IntrospectedTable
introspectedTable
)
{
XmlElement
ifElement
=
new
XmlElement
(
"if"
);
//$NON-NLS-1$
ifElement
.
addAttribute
(
new
Attribute
(
"test"
,
"_parameter != null"
));
//$NON-NLS-1$ //$NON-NLS-2$
XmlElement
includeElement
=
new
XmlElement
(
"include"
);
//$NON-NLS-1$
includeElement
.
addAttribute
(
new
Attribute
(
"refid"
,
//$NON-NLS-1$
introspectedTable
.
getMyBatis3UpdateByExampleWhereClauseId
()));
ifElement
.
addElement
(
includeElement
);
return
ifElement
;
}
}
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