com.lq.cli.mybatis.CreateJavaBeanTask Maven / Gradle / Ivy
package com.lq.cli.mybatis;
import com.lq.cli.comment.CreateTask;
import com.lq.cli.comment.StringUtil;
import com.lq.cli.comment.TaskArgs;
import com.lq.cli.jdbc.JdbcUtil;
import com.lq.cli.jdbc.TableFiledEntity;
import com.lq.cli.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> {
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());
tableFiledEntities.add(new TableFiledEntity(name,type,tableFiledEntity.getaNull(),tableFiledEntity.getKey(),tableFiledEntity.getaDefault(),tableFiledEntity.getExtra()));
}
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\n")
.append("public class ").append(transformTableInfo.getTableName()).append(" {\n\n");
transformTableInfo.getFiledEntities().forEach(tableFiledEntity -> 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");
});
sb.append("\n}");
return sb.toString();
}
private String filedType2DataType(String filedType){
String type = filedType.split(Pattern.quote("("))[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 {
return "String";
}
}
}