Skip to content

Commit 33c2bf9

Browse files
author
hewei
committed
项目初始提交
1 parent 6c7b571 commit 33c2bf9

File tree

8 files changed

+991
-0
lines changed

8 files changed

+991
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010

1111
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
1212
hs_err_pid*
13+
.idea/
14+
mybatis-generator-plugin.iml

pom.xml

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.itfsw</groupId>
6+
<artifactId>mybatis-generator-plugin</artifactId>
7+
<version>1.0.0</version>
8+
<packaging>jar</packaging>
9+
10+
<name>mybatis-generator-plugin</name>
11+
<description>Mybatis Generator Plugins</description>
12+
<url>https://github.com/itfsw/mybatis-generator-plugin</url>
13+
14+
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ license ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
15+
<licenses>
16+
<license>
17+
<name>The Apache License, Version 2.0</name>
18+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
19+
</license>
20+
</licenses>
21+
22+
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++项目properties配置++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
23+
<properties>
24+
<!-- java version -->
25+
<java.version>1.8</java.version>
26+
<!-- 构建编码 -->
27+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
28+
</properties>
29+
30+
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++依赖管理++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
31+
<dependencies>
32+
<!-- mybatis generator -->
33+
<dependency>
34+
<groupId>org.mybatis.generator</groupId>
35+
<artifactId>mybatis-generator-core</artifactId>
36+
<version>1.3.5</version>
37+
<scope>provided</scope>
38+
</dependency>
39+
<!-- 日志 -->
40+
<dependency>
41+
<groupId>org.slf4j</groupId>
42+
<artifactId>slf4j-api</artifactId>
43+
<version>1.7.22</version>
44+
</dependency>
45+
<!-- junit -->
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>3.8.1</version>
50+
<scope>test</scope>
51+
</dependency>
52+
</dependencies>
53+
54+
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++profiles++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
55+
<profiles>
56+
<profile>
57+
<id>release</id>
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-source-plugin</artifactId>
63+
<version>3.0.1</version>
64+
<executions>
65+
<execution>
66+
<id>attach-sources</id>
67+
<goals>
68+
<goal>jar</goal>
69+
</goals>
70+
</execution>
71+
</executions>
72+
</plugin>
73+
<plugin>
74+
<groupId>org.apache.maven.plugins</groupId>
75+
<artifactId>maven-javadoc-plugin</artifactId>
76+
<version>2.10.4</version>
77+
<configuration>
78+
<charset>${project.build.sourceEncoding}</charset>
79+
<encoding>${project.build.sourceEncoding}</encoding>
80+
<docencoding>${project.build.sourceEncoding}</docencoding>
81+
</configuration>
82+
<executions>
83+
<execution>
84+
<id>attach-javadocs</id>
85+
<goals>
86+
<goal>jar</goal>
87+
</goals>
88+
</execution>
89+
</executions>
90+
</plugin>
91+
<!-- gpg 签名 -->
92+
<plugin>
93+
<groupId>org.apache.maven.plugins</groupId>
94+
<artifactId>maven-gpg-plugin</artifactId>
95+
<version>1.6</version>
96+
<executions>
97+
<execution>
98+
<id>sign-artifacts</id>
99+
<phase>verify</phase>
100+
<goals>
101+
<goal>sign</goal>
102+
</goals>
103+
</execution>
104+
</executions>
105+
</plugin>
106+
</plugins>
107+
</build>
108+
</profile>
109+
</profiles>
110+
111+
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++build++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
112+
<build>
113+
<!-- 配置java版本 不配置的话默认父类配置的是1.6-->
114+
<pluginManagement>
115+
<plugins>
116+
<!-- 由于历史原因 maven-compiler只支持编译Java 1.3 所以要自己手动配置其支持1.5 不然跑test的时候不能使用
117+
@Test 注解!! -->
118+
<plugin>
119+
<artifactId>maven-compiler-plugin</artifactId>
120+
<configuration>
121+
<source>${java.version}</source>
122+
<target>${java.version}</target>
123+
</configuration>
124+
</plugin>
125+
</plugins>
126+
</pluginManagement>
127+
</build>
128+
129+
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++developers++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
130+
<developers>
131+
<developer>
132+
<name>hewei</name>
133+
<email>hewei@itfsw.com</email>
134+
<organization>itfsw</organization>
135+
<organizationUrl>http://blog.itfsw.com</organizationUrl>
136+
</developer>
137+
</developers>
138+
139+
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++scm++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
140+
<scm>
141+
<connection>
142+
scm:git:https://github.com/itfsw/mybatis-generator-plugin.git
143+
</connection>
144+
<developerConnection>
145+
scm:git:https://github.com/itfsw/mybatis-generator-plugin.git
146+
</developerConnection>
147+
<url>https://github.com/itfsw/mybatis-generator-plugin</url>
148+
<tag>v1.0.0</tag>
149+
</scm>
150+
151+
<!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++distribution++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
152+
<distributionManagement>
153+
<snapshotRepository>
154+
<id>sonatype</id>
155+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
156+
</snapshotRepository>
157+
<repository>
158+
<id>sonatype</id>
159+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
160+
</repository>
161+
</distributionManagement>
162+
</project>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (c) 2014.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.itfsw.mybatis.generator.plugins;
18+
19+
import com.itfsw.mybatis.generator.plugins.utils.CommentTools;
20+
import org.mybatis.generator.api.IntrospectedTable;
21+
import org.mybatis.generator.api.PluginAdapter;
22+
import org.mybatis.generator.api.dom.java.*;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
import java.util.List;
27+
28+
/**
29+
* ---------------------------------------------------------------------------
30+
* 增加Criteria Builder方法
31+
* ---------------------------------------------------------------------------
32+
* @author: hewei
33+
* @time:2016/12/28 14:56
34+
* ---------------------------------------------------------------------------
35+
*/
36+
public class CriteriaBuilderPlugin extends PluginAdapter {
37+
private static final Logger logger = LoggerFactory.getLogger(CriteriaBuilderPlugin.class);
38+
/**
39+
* {@inheritDoc}
40+
*/
41+
public boolean validate(List<String> warnings) {
42+
return true;
43+
}
44+
45+
/**
46+
* ModelExample Methods 生成
47+
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
48+
*
49+
* @param topLevelClass
50+
* @param introspectedTable
51+
* @return
52+
* @author hewei
53+
*/
54+
@Override
55+
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
56+
List<InnerClass> innerClasses = topLevelClass.getInnerClasses();
57+
for (InnerClass innerClass : innerClasses) {
58+
if ("Criteria".equals(innerClass.getType().getShortName())) {
59+
addFactoryMethodToCriteria(topLevelClass, innerClass, introspectedTable);
60+
}
61+
}
62+
63+
List<Method> methods = topLevelClass.getMethods();
64+
for (Method method : methods) {
65+
if (!"createCriteriaInternal".equals(method.getName()))
66+
continue;
67+
method.getBodyLines().set(0, "Criteria criteria = new Criteria(this);");
68+
logger.info("hw:CriteriaBuilder修改Example的createCriteriaInternal方法,修改构造Criteria时传入Example对象");
69+
}
70+
return true;
71+
}
72+
73+
/**
74+
* 添加工厂方法
75+
*
76+
* @param topLevelClass
77+
* @param innerClass
78+
* @param introspectedTable
79+
* @author hewei
80+
*/
81+
private void addFactoryMethodToCriteria(TopLevelClass topLevelClass, InnerClass innerClass, IntrospectedTable introspectedTable) {
82+
Field f = new Field("example", topLevelClass.getType());
83+
f.setVisibility(JavaVisibility.PRIVATE);
84+
innerClass.addField(f);
85+
86+
// overwrite constructor
87+
List<Method> methods = innerClass.getMethods();
88+
for (Method method : methods) {
89+
if (method.isConstructor()) {
90+
method.addParameter(new Parameter(topLevelClass.getType(), "example"));
91+
method.addBodyLine("this.example = example;");
92+
logger.info("hw:CriteriaBuilder修改Criteria的构造方法,增加example参数");
93+
}
94+
}
95+
96+
// add factory method "example"
97+
Method method = new Method("example");
98+
method.setVisibility(JavaVisibility.PUBLIC);
99+
method.setReturnType(topLevelClass.getType());
100+
method.addBodyLine("return this.example;");
101+
CommentTools.addGeneralMethodComment(method, introspectedTable);
102+
innerClass.addMethod(method);
103+
logger.info("hw:CriteriaBuilder增加工厂方法example");
104+
}
105+
}

0 commit comments

Comments
 (0)