com.lq.mybaits.task.CreateServiceTask Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of springboot-dagger-x Show documentation
Show all versions of springboot-dagger-x Show documentation
Fast construction of springboot microservices
The newest version!
package com.lq.mybaits.task;
import com.lq.SpringBootCli;
import com.lq.entity.TableFiledEntity;
import com.lq.entity.TableInfo;
import com.lq.glob.task.BaseTask;
import com.lq.util.FileUtil;
import com.lq.util.StringUtil;
import java.io.File;
import java.util.List;
public final class CreateServiceTask extends BaseTask {
private List tableInfos;
public CreateServiceTask(SpringBootCli springBootCli, List tableInfos) {
super(springBootCli);
this.tableInfos = tableInfos;
}
@Override
public Boolean execute() throws Exception {
if (checkDir()) {
for (TableInfo tableInfo : tableInfos) {
String service = tableInfo2Service(tableInfo);
createFile(tableInfo.getTransformTableInfo().getTableName() + "Service.java", service);
String serviceImpl = tableInfo2ServiceImpl(tableInfo);
File serviceImplDir = new File(springBootCli.getRootPackagePath() + getPackageName() + File.separator + "impl" + File.separator);
if (!serviceImplDir.exists()) {
serviceImplDir.mkdir();
}
File file = new File(springBootCli.getRootPackagePath() + getPackageName() + File.separator + "impl" + File.separator + tableInfo.getTransformTableInfo().getTableName() + "ServiceImpl.java");
if (!file.exists() || springBootCli.isForceCover()) {
FileUtil.createWriteFile(file, serviceImpl);
}
}
}
return true;
}
private String tableInfo2Service(TableInfo tableInfo) {
TableInfo transformTableInfo = tableInfo.getTransformTableInfo();
StringBuilder sb = new StringBuilder();
sb.append("package ").append(springBootCli.getPackageName()).append(".").append(getPackageName())
.append(";\n\nimport ").append(springBootCli.getPackageName()).append(".entity.")
.append(transformTableInfo.getTableName()).append(";\nimport java.util.List;\n\npublic interface ")
.append(transformTableInfo.getTableName()).append("Service {\n\n");
List filedEntities = transformTableInfo.getFiledEntities();
filedEntities.stream()
.filter(tableFiledEntity -> tableFiledEntity.getKey().equals("PRI"))
.findFirst()
.ifPresent(priKey -> {
sb.append("\tboolean insert")
.append(transformTableInfo.getTableName())
.append("(")
.append(transformTableInfo.getTableName())
.append(" ")
.append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
.append(");\n\n\t");
sb.append("List<").append(transformTableInfo.getTableName()).append("> queryAll")
.append(transformTableInfo.getTableName()).append("(");
for (int x = 1; x < filedEntities.size(); x++) {
TableFiledEntity tableFiledEntity = filedEntities.get(x);
if (tableFiledEntity.getType().equals("String")&&tableFiledEntity.getFieldLimitSize() 0) {
sb.append("String ").append(tableFiledEntity.getName()).append(",");
}
}
sb.append("Integer page,Integer pageSize);\n\n\t")
.append("Integer queryAll").append(transformTableInfo.getTableName()).append("Count(");
for (int x = 1; x < filedEntities.size(); x++) {
TableFiledEntity tableFiledEntity = filedEntities.get(x);
if (tableFiledEntity.getType().equals("String")&&tableFiledEntity.getFieldLimitSize() 0) {
sb.append("String ").append(tableFiledEntity.getName()).append(",");
}
}
for (int x = 1; x < filedEntities.size(); x++) {
if (filedEntities.get(x).getType().equals("String")) {
sb.deleteCharAt(sb.lastIndexOf(","));
break;
}
}
sb.append(");\n\n\t");
sb.append(transformTableInfo.getTableName())
.append(" query")
.append(transformTableInfo.getTableName())
.append("By")
.append(StringUtil.firstToUpperCase(priKey.getName()))
.append("(")
.append(priKey.getType())
.append(" ")
.append(priKey.getName())
.append(");\n\n")
.append("\tint delete")
.append(transformTableInfo.getTableName())
.append("By")
.append(StringUtil.firstToUpperCase(priKey.getName()))
.append("(")
.append(priKey.getType())
.append(" ")
.append(priKey.getName())
.append(");\n\n")
.append("\tint delete")
.append(transformTableInfo.getTableName())
.append("By")
.append(StringUtil.firstToUpperCase(priKey.getName()))
.append("s(List<")
.append(priKey.getType())
.append("> ")
.append(priKey.getName())
.append("s);\n\n")
.append("\tboolean update")
.append(transformTableInfo.getTableName())
.append("(")
.append(transformTableInfo.getTableName())
.append(" ")
.append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
.append(");\n\n");
}
);
sb.append("\n}");
return sb.toString();
}
private String tableInfo2ServiceImpl(TableInfo tableInfo) {
TableInfo transformTableInfo = tableInfo.getTransformTableInfo();
StringBuilder sb = new StringBuilder();
sb.append("package ").append(springBootCli.getPackageName()).append(".").append(getPackageName())
.append(".impl;\n\nimport ").append(springBootCli.getPackageName()).append(".entity.")
.append(transformTableInfo.getTableName())
.append(";\n\nimport ")
.append(springBootCli.getPackageName()).append(".service.")
.append(transformTableInfo.getTableName()).append("Service");
if (springBootCli.isUseRedis()) {
sb.append(";\nimport org.springframework.data.redis.core.RedisTemplate");
}
sb.append(";\nimport java.util.List;\nimport javax.annotation.Resource;\nimport org.springframework.stereotype.Service;\nimport ")
.append(springBootCli.getPackageName()).append(".mapper.").append(transformTableInfo.getTableName())
.append("Mapper;\n\n@Service\n")
.append("public class ").append(transformTableInfo.getTableName()).append("ServiceImpl implements ")
.append(transformTableInfo.getTableName())
.append("Service{\n\n\t@Resource\n\tprivate ").append(transformTableInfo.getTableName())
.append("Mapper ").append(StringUtil.firstToLowerCase(transformTableInfo.getTableName())).append("Mapper;\n");
List filedEntities = transformTableInfo.getFiledEntities();
if (springBootCli.isUseRedis()) {
TableFiledEntity pri = filedEntities.stream()
.filter(tableFiledEntity -> tableFiledEntity.getKey().equals("PRI"))
.findFirst().orElseGet(TableFiledEntity::new);
if (pri.getType() != null) {
sb.append("\n\t@Resource\n\tprivate RedisTemplate<").append(pri.getType()).append(", ");
} else {
sb.append("\n\t@Resource\n\tprivate RedisTemplate