扩充EnumTypeStatusPlugin支持的数据类型范围
This commit is contained in:
@@ -20,11 +20,13 @@ import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.FormatTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.IntrospectedTableTools;
|
||||
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
|
||||
import org.mybatis.generator.api.CommentGenerator;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
import org.mybatis.generator.internal.util.StringUtility;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -48,7 +50,7 @@ public class EnumTypeStatusPlugin extends BasePlugin {
|
||||
public final static String REMARKS_PATTERN = ".*\\s*\\[\\s*(\\w+\\s*\\(\\s*[\\u4e00-\\u9fa5_a-zA-Z0-9]+\\s*\\)\\s*:\\s*[\\u4e00-\\u9fa5_a-zA-Z0-9]+\\s*\\,?\\s*)+\\s*\\]\\s*.*";
|
||||
public final static String NEED_PATTERN = "\\[\\s*((\\w+\\s*\\(\\s*[\\u4e00-\\u9fa5_a-zA-Z0-9]+\\s*\\)\\s*:\\s*[\\u4e00-\\u9fa5_a-zA-Z0-9]+\\s*\\,?\\s*)+)\\s*\\]";
|
||||
public final static String ITEM_PATTERN = "(\\w+)\\s*\\(\\s*([\\u4e00-\\u9fa5_a-zA-Z0-9]+)\\s*\\)\\s*:\\s*([\\u4e00-\\u9fa5_a-zA-Z0-9]+)";
|
||||
private Map<IntrospectedColumn, List<EnumItemInfo>> enumColumns;
|
||||
private Map<String, EnumInfo> enumColumns;
|
||||
|
||||
/**
|
||||
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
|
||||
@@ -83,30 +85,35 @@ public class EnumTypeStatusPlugin extends BasePlugin {
|
||||
} else if (!(Short.class.getTypeName().equals(javaType)
|
||||
|| Integer.class.getTypeName().equals(javaType)
|
||||
|| Long.class.getTypeName().equals(javaType)
|
||||
|| Boolean.class.getTypeName().equals(javaType)
|
||||
|| Double.class.getTypeName().equals(javaType)
|
||||
|| Float.class.getTypeName().equals(javaType)
|
||||
|| BigDecimal.class.getTypeName().equals(javaType)
|
||||
|| Byte.class.getTypeName().equals(javaType)
|
||||
|| String.class.getTypeName().equals(javaType))) {
|
||||
warnings.add("itfsw:插件" + EnumTypeStatusPlugin.class.getTypeName() + "找到column为" + enumColumnsStr.trim() + "对应Java类型不是Short、Integer、Long、String!");
|
||||
warnings.add("itfsw:插件" + EnumTypeStatusPlugin.class.getTypeName() + "找到column为" + enumColumnsStr.trim() + "对应Java类型不在支持范围内!");
|
||||
} else {
|
||||
// 提取信息
|
||||
Pattern pattern = Pattern.compile(NEED_PATTERN);
|
||||
Matcher matcher = pattern.matcher(remarks);
|
||||
List<EnumItemInfo> itemInfos = new ArrayList<>();
|
||||
EnumInfo enumInfo = new EnumInfo(column);
|
||||
if (matcher.find() && matcher.groupCount() == 2) {
|
||||
String enumInfoStr = matcher.group(1);
|
||||
// 根据逗号切分
|
||||
String[] enumInfoStrs = enumInfoStr.split(",");
|
||||
|
||||
// 提取每个节点信息
|
||||
for (String enumInfo : enumInfoStrs) {
|
||||
for (String enumInfoItemStr : enumInfoStrs) {
|
||||
pattern = Pattern.compile(ITEM_PATTERN);
|
||||
matcher = pattern.matcher(enumInfo.trim());
|
||||
matcher = pattern.matcher(enumInfoItemStr.trim());
|
||||
if (matcher.find() && matcher.groupCount() == 3) {
|
||||
itemInfos.add(new EnumItemInfo(column, matcher.group(1), matcher.group(3), matcher.group(2)));
|
||||
enumInfo.addItem(matcher.group(1), matcher.group(3), matcher.group(2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (itemInfos.size() > 0) {
|
||||
this.enumColumns.put(column, itemInfos);
|
||||
if (enumInfo.hasItems()) {
|
||||
this.enumColumns.put(column.getJavaProperty(), enumInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -132,63 +139,13 @@ public class EnumTypeStatusPlugin extends BasePlugin {
|
||||
* @param introspectedTable
|
||||
*/
|
||||
private void generateModelEnum(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
this.enumColumns.forEach((introspectedColumn, enumItemInfos) -> {
|
||||
// 枚举跟随字段走
|
||||
for (Field field : topLevelClass.getFields()) {
|
||||
if (introspectedColumn.getJavaProperty().equals(field.getName())) {
|
||||
String enumName = this.upperCaseFirst(field.getName());
|
||||
|
||||
InnerEnum innerEnum = new InnerEnum(new FullyQualifiedJavaType(enumName));
|
||||
commentGenerator.addEnumComment(innerEnum, introspectedTable);
|
||||
innerEnum.setVisibility(JavaVisibility.PUBLIC);
|
||||
innerEnum.setStatic(true);
|
||||
|
||||
// 生成枚举
|
||||
for (EnumItemInfo item : enumItemInfos) {
|
||||
innerEnum.addEnumConstant(item.getName() + "(" + item.getValue() + ", " + item.getComment() + ")");
|
||||
}
|
||||
|
||||
// 生成属性和构造函数
|
||||
Field fValue = new Field("value", field.getType());
|
||||
fValue.setVisibility(JavaVisibility.PRIVATE);
|
||||
fValue.setFinal(true);
|
||||
commentGenerator.addFieldComment(fValue, introspectedTable);
|
||||
innerEnum.addField(fValue);
|
||||
|
||||
Field fName = new Field("name", FullyQualifiedJavaType.getStringInstance());
|
||||
fName.setVisibility(JavaVisibility.PRIVATE);
|
||||
fName.setFinal(true);
|
||||
commentGenerator.addFieldComment(fName, introspectedTable);
|
||||
innerEnum.addField(fName);
|
||||
|
||||
Method mInc = new Method(enumName);
|
||||
mInc.setConstructor(true);
|
||||
mInc.addBodyLine("this.value = value;");
|
||||
mInc.addBodyLine("this.name = name;");
|
||||
mInc.addParameter(new Parameter(fValue.getType(), "value"));
|
||||
mInc.addParameter(new Parameter(fName.getType(), "name"));
|
||||
commentGenerator.addGeneralMethodComment(mInc, introspectedTable);
|
||||
FormatTools.addMethodWithBestPosition(innerEnum, mInc);
|
||||
|
||||
// 获取value的方法
|
||||
Method mValue = JavaElementGeneratorTools.generateGetterMethod(fValue);
|
||||
commentGenerator.addGeneralMethodComment(mValue, introspectedTable);
|
||||
FormatTools.addMethodWithBestPosition(innerEnum, mValue);
|
||||
|
||||
Method mValue1 = JavaElementGeneratorTools.generateGetterMethod(fValue);
|
||||
mValue1.setName("value");
|
||||
commentGenerator.addGeneralMethodComment(mValue1, introspectedTable);
|
||||
FormatTools.addMethodWithBestPosition(innerEnum, mValue1);
|
||||
|
||||
// 获取name的方法
|
||||
Method mName = JavaElementGeneratorTools.generateGetterMethod(fName);
|
||||
commentGenerator.addGeneralMethodComment(mName, introspectedTable);
|
||||
FormatTools.addMethodWithBestPosition(innerEnum, mName);
|
||||
|
||||
topLevelClass.addInnerEnum(innerEnum);
|
||||
}
|
||||
// 枚举跟随字段走
|
||||
for (Field field : topLevelClass.getFields()) {
|
||||
if (this.enumColumns.get(field.getName()) != null) {
|
||||
InnerEnum innerEnum = this.enumColumns.get(field.getName()).generateEnum(commentGenerator, introspectedTable);
|
||||
topLevelClass.addInnerEnum(innerEnum);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,66 +161,131 @@ public class EnumTypeStatusPlugin extends BasePlugin {
|
||||
return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
|
||||
}
|
||||
|
||||
/**
|
||||
* 首字母大写
|
||||
* @param s
|
||||
* @return
|
||||
*/
|
||||
private String upperCaseFirst(String s) {
|
||||
if (Character.isUpperCase(s.charAt(0))) {
|
||||
return s;
|
||||
} else {
|
||||
return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class EnumItemInfo {
|
||||
public static class EnumInfo {
|
||||
private List<EnumItemInfo> items = new ArrayList<>();
|
||||
private IntrospectedColumn column;
|
||||
private String name;
|
||||
private String comment;
|
||||
private String value;
|
||||
|
||||
public EnumItemInfo(IntrospectedColumn column, String name, String comment, String value) {
|
||||
public EnumInfo(IntrospectedColumn column) {
|
||||
this.column = column;
|
||||
this.name = name;
|
||||
this.comment = comment;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>comment</tt>.
|
||||
* @return property value of comment
|
||||
* @author hewei
|
||||
* 添加Enum Item
|
||||
* @param name
|
||||
* @param comment
|
||||
* @param value
|
||||
*/
|
||||
public String getComment() {
|
||||
return "\"" + comment.trim() + "\"";
|
||||
public void addItem(String name, String comment, String value) {
|
||||
items.add(new EnumItemInfo(this.column, name, comment, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>name</tt>.
|
||||
* @return property value of name
|
||||
* @author hewei
|
||||
* 判断是否有节点
|
||||
* @return
|
||||
*/
|
||||
public String getName() {
|
||||
return name.trim().toUpperCase();
|
||||
public boolean hasItems() {
|
||||
return items.size() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>value</tt>.
|
||||
* @return property value of value
|
||||
* @author hewei
|
||||
*/
|
||||
public String getValue() {
|
||||
String javaType = this.column.getFullyQualifiedJavaType().getFullyQualifiedName();
|
||||
if (Short.class.getTypeName().equals(javaType)) {
|
||||
return "(short)" + value;
|
||||
} else if (Long.class.getTypeName().equals(javaType)) {
|
||||
return value + "L";
|
||||
} else if (String.class.getTypeName().equals(javaType)) {
|
||||
return "\"" + value + "\"";
|
||||
public InnerEnum generateEnum(CommentGenerator commentGenerator, IntrospectedTable introspectedTable) {
|
||||
String enumName = FormatTools.upFirstChar(column.getJavaProperty());
|
||||
|
||||
InnerEnum innerEnum = new InnerEnum(new FullyQualifiedJavaType(enumName));
|
||||
commentGenerator.addEnumComment(innerEnum, introspectedTable);
|
||||
innerEnum.setVisibility(JavaVisibility.PUBLIC);
|
||||
innerEnum.setStatic(true);
|
||||
|
||||
// 生成枚举
|
||||
for (EnumItemInfo item : this.items) {
|
||||
innerEnum.addEnumConstant(item.getName() + "(" + item.getValue() + ", " + item.getComment() + ")");
|
||||
}
|
||||
|
||||
// 生成属性和构造函数
|
||||
Field fValue = new Field("value", column.getFullyQualifiedJavaType());
|
||||
fValue.setVisibility(JavaVisibility.PRIVATE);
|
||||
fValue.setFinal(true);
|
||||
commentGenerator.addFieldComment(fValue, introspectedTable);
|
||||
innerEnum.addField(fValue);
|
||||
|
||||
Field fName = new Field("name", FullyQualifiedJavaType.getStringInstance());
|
||||
fName.setVisibility(JavaVisibility.PRIVATE);
|
||||
fName.setFinal(true);
|
||||
commentGenerator.addFieldComment(fName, introspectedTable);
|
||||
innerEnum.addField(fName);
|
||||
|
||||
Method mInc = new Method(enumName);
|
||||
mInc.setConstructor(true);
|
||||
mInc.addBodyLine("this.value = value;");
|
||||
mInc.addBodyLine("this.name = name;");
|
||||
mInc.addParameter(new Parameter(fValue.getType(), "value"));
|
||||
mInc.addParameter(new Parameter(fName.getType(), "name"));
|
||||
commentGenerator.addGeneralMethodComment(mInc, introspectedTable);
|
||||
FormatTools.addMethodWithBestPosition(innerEnum, mInc);
|
||||
|
||||
// 获取value的方法
|
||||
Method mValue = JavaElementGeneratorTools.generateGetterMethod(fValue);
|
||||
commentGenerator.addGeneralMethodComment(mValue, introspectedTable);
|
||||
FormatTools.addMethodWithBestPosition(innerEnum, mValue);
|
||||
|
||||
Method mValue1 = JavaElementGeneratorTools.generateGetterMethod(fValue);
|
||||
mValue1.setName("value");
|
||||
commentGenerator.addGeneralMethodComment(mValue1, introspectedTable);
|
||||
FormatTools.addMethodWithBestPosition(innerEnum, mValue1);
|
||||
|
||||
// 获取name的方法
|
||||
Method mName = JavaElementGeneratorTools.generateGetterMethod(fName);
|
||||
commentGenerator.addGeneralMethodComment(mName, introspectedTable);
|
||||
FormatTools.addMethodWithBestPosition(innerEnum, mName);
|
||||
|
||||
return innerEnum;
|
||||
}
|
||||
|
||||
|
||||
private class EnumItemInfo {
|
||||
private IntrospectedColumn column;
|
||||
private String name;
|
||||
private String comment;
|
||||
private String value;
|
||||
|
||||
public EnumItemInfo(IntrospectedColumn column, String name, String comment, String value) {
|
||||
this.column = column;
|
||||
this.name = name.trim();
|
||||
this.comment = comment.trim();
|
||||
this.value = value.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>comment</tt>.
|
||||
* @return property value of comment
|
||||
* @author hewei
|
||||
*/
|
||||
public String getComment() {
|
||||
return "\"" + comment + "\"";
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>name</tt>.
|
||||
* @return property value of name
|
||||
* @author hewei
|
||||
*/
|
||||
public String getName() {
|
||||
return name.toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>value</tt>.
|
||||
* @return property value of value
|
||||
* @author hewei
|
||||
*/
|
||||
public String getValue() {
|
||||
String javaType = this.column.getFullyQualifiedJavaType().getShortName();
|
||||
if ("NULL".equalsIgnoreCase(value)) {
|
||||
return "null";
|
||||
} else {
|
||||
return "new " + javaType + "(\"" + value + "\")";
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ public class EnumTypeStatusPluginTest {
|
||||
tool = MyBatisGeneratorTool.create("scripts/EnumTypeStatusPlugin/mybatis-generator-with-unsupport-type.xml");
|
||||
tool.generate();
|
||||
|
||||
Assert.assertEquals(tool.getWarnings().get(0), "itfsw:插件" + EnumTypeStatusPlugin.class.getTypeName() + "找到column为field2对应Java类型不是Short、Integer、Long、String!");
|
||||
Assert.assertEquals(tool.getWarnings().get(0), "itfsw:插件" + EnumTypeStatusPlugin.class.getTypeName() + "找到column为field2对应Java类型不在支持范围内!");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user