Skip to content

Commit cb32432

Browse files
sj13824803000abel533
authored andcommitted
!15 扩展代码生成工具
Merge pull request !15 from wugh/master
2 parents cfbe617 + 06524bb commit cb32432

File tree

5 files changed

+105
-8
lines changed

5 files changed

+105
-8
lines changed

generator/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
<version>1.16.22</version>
6262
<scope>provided</scope>
6363
</dependency>
64+
<dependency>
65+
<groupId>io.swagger</groupId>
66+
<artifactId>swagger-annotations</artifactId>
67+
<version>1.5.20</version>
68+
<scope>test</scope>
69+
</dependency>
6470
<dependency>
6571
<groupId>org.hsqldb</groupId>
6672
<artifactId>sqltool</artifactId>

generator/src/main/java/tk/mybatis/mapper/generator/MapperCommentGenerator.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ public class MapperCommentGenerator implements CommentGenerator {
4141
//开始的分隔符,例如mysql为`,sqlserver为[
4242
private String beginningDelimiter = "";
4343
//结束的分隔符,例如mysql为`,sqlserver为]
44-
private String endingDelimiter = "";
44+
private String endingDelimiter = "";
4545
//强制生成注解
4646
private boolean forceAnnotation;
47+
//是否生成swagger注解
48+
private boolean needsSwagger;
4749

4850
public MapperCommentGenerator() {
4951
super();
@@ -88,6 +90,10 @@ public void addConfigurationProperties(Properties properties) {
8890
if (StringUtility.stringHasValue(forceAnnotation)) {
8991
this.forceAnnotation = "TRUE".equalsIgnoreCase(forceAnnotation);
9092
}
93+
String needsSwagger = properties.getProperty("needsSwagger");
94+
if (StringUtility.stringHasValue(needsSwagger)) {
95+
this.needsSwagger = "TRUE".equalsIgnoreCase(needsSwagger);
96+
}
9197
}
9298

9399
public String getDelimiterName(String name) {
@@ -182,6 +188,16 @@ public void addFieldComment(Field field, IntrospectedTable introspectedTable, In
182188
String sql = MessageFormat.format(introspectedTable.getTableConfiguration().getGeneratedKey().getRuntimeSqlStatement(), tableName, tableName.toUpperCase());
183189
field.addAnnotation("@GeneratedValue(strategy = GenerationType.IDENTITY, generator = \"" + sql + "\")");
184190
}
191+
// region swagger注解
192+
if (this.needsSwagger) {
193+
String remarks = introspectedColumn.getRemarks();
194+
if (remarks == null) {
195+
remarks = "";
196+
}
197+
String swaggerAnnotation = "@ApiModelProperty(value = \"%s\" da )";
198+
field.addAnnotation("@ApiModelProperty(\"" + remarks.replaceAll("\r", "").replaceAll("\n", "") + "\")");
199+
}
200+
// endregion
185201
}
186202

187203
/**

generator/src/main/java/tk/mybatis/mapper/generator/MapperPlugin.java

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
import org.mybatis.generator.config.Context;
3232
import org.mybatis.generator.internal.util.StringUtility;
3333

34-
import java.util.HashSet;
35-
import java.util.Properties;
36-
import java.util.Set;
34+
import java.util.*;
3735

3836
/**
3937
* 通用Mapper生成器插件
@@ -69,6 +67,10 @@ public class MapperPlugin extends FalseMethodPlugin {
6967
private boolean needsEqualsAndHashCode = false;
7068
//是否生成字段名常量
7169
private boolean generateColumnConsts = false;
70+
//是否生成默认的属性的静态方法
71+
private boolean generateDefaultInstanceMethod = false;
72+
//是否生成swagger注解,包括 @ApiModel和@ApiModelProperty
73+
private boolean needsSwagger = false;
7274

7375
public String getDelimiterName(String name) {
7476
StringBuilder nameBuilder = new StringBuilder();
@@ -145,21 +147,34 @@ private void processEntityClass(TopLevelClass topLevelClass, IntrospectedTable i
145147
topLevelClass.addAnnotation("@EqualsAndHashCode");
146148
}
147149
//lombok扩展结束
150+
// region swagger扩展
151+
if (this.needsSwagger) {
152+
//导包
153+
topLevelClass.addImportedType("io.swagger.annotations.ApiModel");
154+
topLevelClass.addImportedType("io.swagger.annotations.ApiModelProperty");
155+
//增加注解(去除注释中的转换符)
156+
String remarks = introspectedTable.getRemarks();
157+
if (remarks == null) {
158+
remarks = "";
159+
}
160+
topLevelClass.addAnnotation("@ApiModel(\"" + remarks.replaceAll("\r", "").replaceAll("\n", "") + "\")");
161+
}
162+
// endregion swagger扩展
148163
String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();
149164
//如果包含空格,或者需要分隔符,需要完善
150165
if (StringUtility.stringContainsSpace(tableName)) {
151166
tableName = context.getBeginningDelimiter()
152-
+ tableName
153-
+ context.getEndingDelimiter();
167+
+ tableName
168+
+ context.getEndingDelimiter();
154169
}
155170
//是否忽略大小写,对于区分大小写的数据库,会有用
156171
if (caseSensitive && !topLevelClass.getType().getShortName().equals(tableName)) {
157172
topLevelClass.addAnnotation("@Table(name = \"" + getDelimiterName(tableName) + "\")");
158173
} else if (!topLevelClass.getType().getShortName().equalsIgnoreCase(tableName)) {
159174
topLevelClass.addAnnotation("@Table(name = \"" + getDelimiterName(tableName) + "\")");
160175
} else if (StringUtility.stringHasValue(schema)
161-
|| StringUtility.stringHasValue(beginningDelimiter)
162-
|| StringUtility.stringHasValue(endingDelimiter)) {
176+
|| StringUtility.stringHasValue(beginningDelimiter)
177+
|| StringUtility.stringHasValue(endingDelimiter)) {
163178
topLevelClass.addAnnotation("@Table(name = \"" + getDelimiterName(tableName) + "\")");
164179
} else if (forceAnnotation) {
165180
topLevelClass.addAnnotation("@Table(name = \"" + getDelimiterName(tableName) + "\")");
@@ -175,8 +190,56 @@ private void processEntityClass(TopLevelClass topLevelClass, IntrospectedTable i
175190
field.setInitializationString("\"" + introspectedColumn.getJavaProperty() + "\"");
176191
context.getCommentGenerator().addClassComment(topLevelClass, introspectedTable);
177192
topLevelClass.addField(field);
193+
//增加字段名常量,用于pageHelper
194+
Field columnField = new Field();
195+
columnField.setVisibility(JavaVisibility.PUBLIC);
196+
columnField.setStatic(true);
197+
columnField.setFinal(true);
198+
columnField.setName("DB_" + introspectedColumn.getActualColumnName().toUpperCase()); //$NON-NLS-1$
199+
columnField.setType(new FullyQualifiedJavaType(String.class.getName())); //$NON-NLS-1$
200+
columnField.setInitializationString("\"" + introspectedColumn.getActualColumnName() + "\"");
201+
topLevelClass.addField(columnField);
178202
}
179203
}
204+
if (generateDefaultInstanceMethod) {
205+
Method defaultMethod = new Method();
206+
defaultMethod.setStatic(true);
207+
defaultMethod.setName("defaultInstance");
208+
defaultMethod.setVisibility(JavaVisibility.PUBLIC);
209+
defaultMethod.setReturnType(topLevelClass.getType());
210+
defaultMethod.addBodyLine(String.format("%s instance = new %s();", topLevelClass.getType().getShortName(), topLevelClass.getType().getShortName()));
211+
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
212+
String shortName = introspectedColumn.getFullyQualifiedJavaType().getShortName();
213+
List<String> supportType = Arrays.asList("Byte", "Short", "Character", "Integer", "Long", "Float", "Double", "String", "BigDecimal", "BigInteger");
214+
if (!supportType.contains(shortName)) {
215+
continue;
216+
}
217+
if (introspectedColumn.getDefaultValue() != null) {
218+
String defaultValue = introspectedColumn.getDefaultValue();
219+
//去除前后'',如 '123456' -> 123456
220+
if (defaultValue.startsWith("'") && defaultValue.endsWith("'")) {
221+
if (defaultValue.length() == 2) {
222+
defaultValue = "";
223+
} else {
224+
defaultValue = defaultValue.substring(1, defaultValue.length() - 1);
225+
}
226+
}
227+
//暂不支持时间类型默认值识别,不同数据库表达式不同
228+
if ("Boolean".equals(shortName)) {
229+
if ("0".equals(defaultValue)) {
230+
defaultValue = "false";
231+
} else if ("1".equals(defaultValue)) {
232+
defaultValue = "true";
233+
}
234+
}
235+
//通过 new 方法转换
236+
defaultMethod.addBodyLine(String.format("instance.%s = new %s(\"%s\");", introspectedColumn.getJavaProperty(), shortName, defaultValue));
237+
}
238+
239+
}
240+
defaultMethod.addBodyLine("return instance;");
241+
topLevelClass.addMethod(defaultMethod);
242+
}
180243
}
181244

182245
/**
@@ -254,6 +317,8 @@ public void setContext(Context context) {
254317
}
255318
//支持oracle获取注释#114
256319
context.getJdbcConnectionConfiguration().addProperty("remarksReporting", "true");
320+
//支持mysql获取注释
321+
context.getJdbcConnectionConfiguration().addProperty("useInformationSchema", "true");
257322
}
258323

259324
@Override
@@ -283,15 +348,22 @@ public void setProperties(Properties properties) {
283348
this.needsEqualsAndHashCode = !this.needsData && lombok.contains("EqualsAndHashCode");
284349
this.needsAccessors = lombok.contains("Accessors");
285350
}
351+
//swagger扩展
352+
String swagger = getProperty("swagger", "false");
353+
if ("true".equalsIgnoreCase(swagger)) {
354+
this.needsSwagger = true;
355+
}
286356
if (useMapperCommentGenerator) {
287357
commentCfg.addProperty("beginningDelimiter", this.beginningDelimiter);
288358
commentCfg.addProperty("endingDelimiter", this.endingDelimiter);
289359
String forceAnnotation = getProperty("forceAnnotation");
290360
if (StringUtility.stringHasValue(forceAnnotation)) {
291361
commentCfg.addProperty("forceAnnotation", forceAnnotation);
292362
}
363+
commentCfg.addProperty("needsSwagger", this.needsSwagger + "");
293364
}
294365
this.generateColumnConsts = getPropertyAsBoolean("generateColumnConsts");
366+
this.generateDefaultInstanceMethod = getPropertyAsBoolean("generateDefaultInstanceMethod");
295367
}
296368

297369
protected String getProperty(String key) {

generator/src/test/resources/CreateDB.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ create table country (
44
id integer NOT NULL PRIMARY KEY,
55
countryname varchar(32),
66
countrycode VARCHAR(2) DEFAULT 'HH',
7+
decimal_num decimal (10,5) DEFAULT 0.1,
78
version INTEGER DEFAULT 1 NOT NULL
89
);
910

generator/src/test/resources/generatorConfig.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838
<property name="caseSensitive" value="true"/>
3939
<property name="forceAnnotation" value="true"/>
4040
<property name="generateColumnConsts" value="true"/>
41+
<property name="generateDefaultInstanceMethod" value="true"/>
4142
<property name="beginningDelimiter" value="`"/>
4243
<property name="endingDelimiter" value="`"/>
4344
<!--使用 Data 时,Getter,Setter 无效-->
4445
<property name="lombok" value="Getter,Setter,Data,ToString,Accessors,EqualsAndHashCode"/>
46+
<property name="swagger" value="true"/>
4547
</plugin>
4648

4749
<!--通用代码生成器插件-->

0 commit comments

Comments
 (0)