All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.lq.cli.mybatis.CreateJavaBeanTask Maven / Gradle / Ivy
package com.lq.cli.mybatis;
import com.lq.cli.comment.CreateTask;
import com.lq.comment.util.StringUtil;
import com.lq.cli.comment.TaskArgs;
import com.lq.comment.jdbc.JdbcUtil;
import com.lq.comment.jdbc.TableFiledEntity;
import com.lq.comment.jdbc.TableInfo;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
public class CreateJavaBeanTask extends CreateTask> {
// List textType = Arrays.asList("char","varchar","tinytext","text","blob","mediumtext","mediumblob","longtext","longblob","enum","set",
// "date","datetime","timestamp","time","year");
private List numberType = Arrays.asList("tinyint", "smallint", "mediumint", "int", "bigint", "float", "double", "decimal");
private final JdbcUtil jdbcUtil;
public CreateJavaBeanTask(TaskArgs taskArgs) {
super(taskArgs);
jdbcUtil = new JdbcUtil(taskArgs.jdbcConfig);
setPackageName("entity");
}
@Override
public List call() throws SQLException, IOException {
if (checkDir()) {
List tableInfos = jdbcUtil.queryTableInfo();
if (tableInfos != null && tableInfos.size() > 0) {
for (TableInfo tableInfo : tableInfos) {
TableInfo transformTableInfo = transformTableInfo(tableInfo);
String javaBean = tableInfo2JavaBean(transformTableInfo);
createFile(tableInfo.getTransformTableInfo().getTableName() + ".java", javaBean);
}
}
return tableInfos;
}
return Collections.emptyList();
}
private TableInfo transformTableInfo(TableInfo tableInfo) {
String tableName = StringUtil.firstToUpperCase(StringUtil.underlineToHump(tableInfo.getTableName()));
List tableFiledEntities = new ArrayList<>();
for (TableFiledEntity tableFiledEntity : tableInfo.getFiledEntities()) {
String type = filedType2DataType(tableFiledEntity.getType());
String name = StringUtil.underlineToHump(tableFiledEntity.getName());
TableFiledEntity filedEntity = new TableFiledEntity(name, tableFiledEntity.getaNull(), tableFiledEntity.getKey(), tableFiledEntity.getaDefault(), tableFiledEntity.getExtra());
if (type.startsWith("String")) {
String[] typeSplit = type.split("_");
type = typeSplit[0];
filedEntity.setFieldLimitSize(Integer.parseInt(typeSplit[1]));
}
filedEntity.setType(type);
tableFiledEntities.add(filedEntity);
}
TableInfo transformTableInfo = new TableInfo(tableName, tableFiledEntities);
tableInfo.setTransformTableInfo(transformTableInfo);
return transformTableInfo;
}
private String tableInfo2JavaBean(TableInfo transformTableInfo) {
StringBuilder sb = new StringBuilder();
sb.append("package ").append(taskArgs.packageName).append(".").append(getPackageName())
.append(";\n\nimport org.hibernate.validator.constraints.Length;\nimport javax.validation.constraints.NotBlank;\n\n");
sb.append("public class ").append(transformTableInfo.getTableName()).append(" {\n\n");
transformTableInfo.getFiledEntities().forEach(tableFiledEntity -> {
if (!tableFiledEntity.getKey().equals("PRI") && tableFiledEntity.getaNull().equals("NO") && tableFiledEntity.getType().equals("String")) {
sb.append("\t@NotBlank(message=\"").append(tableFiledEntity.getName()).append("不能为空\")\n");
if (tableFiledEntity.getFieldLimitSize() != 0) {
sb.append("\t@Length(max = ").append(tableFiledEntity.getFieldLimitSize()).append(",message = \"")
.append(tableFiledEntity.getName()).append("长度不能超过")
.append(tableFiledEntity.getFieldLimitSize()).append("位\")\n");
}
}
sb.append("\tprivate ").append(tableFiledEntity.getType())
.append(" ").append(tableFiledEntity.getName()).append(";\n");
});
sb.append("\n");
transformTableInfo.getFiledEntities().forEach(tableFiledEntity -> {
String type = tableFiledEntity.getType();
String nameUpperCase = StringUtil.firstToUpperCase(tableFiledEntity.getName());
sb.append("\tpublic ").append(type).append(" get").append(nameUpperCase).append("(){\n")
.append("\t\treturn ").append(tableFiledEntity.getName()).append(";\n\t}\n\n");
sb.append("\tpublic void").append(" set").append(nameUpperCase).append("(").
append(type).append(" ").append(tableFiledEntity.getName()).append("){\n")
.append("\t\tthis.").append(tableFiledEntity.getName()).append(" = ")
.append(tableFiledEntity.getName()).append(";\n\t}\n\n");
});
sb.append("\n}");
return sb.toString();
}
private String filedType2DataType(String filedType) {
String[] types = filedType.split(Pattern.quote("("));
String type = types[0];
if (numberType.contains(type)) {
if (type.equals("bigint")) {
return "long";
} else if (type.endsWith("int")) {
return "int";
} else if (type.equals("decimal")) {
return "double";
} else {
return type;
}
} else {
String fieldLimitSize = "0";
if (types.length == 2) {
fieldLimitSize = types[1].replace(")", "");
}
return "String_" + fieldLimitSize;
}
}
}