com.lq.jpa.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.jpa.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 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) {
TableFiledEntity pri = tableInfo.getTransformTableInfo().getFiledEntities().stream()
.filter(tableFiledEntity -> tableFiledEntity.getKey().equals("PRI"))
.findFirst().orElse(null);
if (pri != null) {
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 serviceImplFile = new File(springBootCli.getRootPackagePath() + getPackageName() + File.separator + "impl" + File.separator + tableInfo.getTransformTableInfo().getTableName() + "ServiceImpl.java");
if (!serviceImplFile.exists() || springBootCli.isForceCover()) {
FileUtil.createWriteFile(serviceImplFile, serviceImpl);
}
String repository = tableInfo2Repository(tableInfo);
File repositoryImplDir = new File(springBootCli.getRootPackagePath() + getPackageName() + File.separator + "repository" + File.separator);
if (!repositoryImplDir.exists()) {
repositoryImplDir.mkdir();
}
File repositoryFile = new File(springBootCli.getRootPackagePath() + getPackageName() + File.separator + "repository" + File.separator + tableInfo.getTransformTableInfo().getTableName() + "Repository.java");
if (!repositoryFile.exists() || springBootCli.isForceCover()) {
FileUtil.createWriteFile(repositoryFile, repository);
}
}
}
}
return true;
}
private String tableInfo2Repository(TableInfo tableInfo) {
TableInfo transformTableInfo = tableInfo.getTransformTableInfo();
StringBuilder sb = new StringBuilder();
transformTableInfo.getFiledEntities().stream()
.filter(tableFiledEntity -> tableFiledEntity.getKey().equals("PRI"))
.findFirst()
.ifPresent(priKey -> {
sb.append("package ").append(springBootCli.getPackageName()).append(".").append(getPackageName())
.append(".repository;\n\nimport ").append(springBootCli.getPackageName()).append(".entity.")
.append(transformTableInfo.getTableName())
.append(";\nimport org.springframework.data.jpa.repository.JpaRepository;\nimport org.springframework.data.jpa.repository.JpaSpecificationExecutor;\n\npublic interface ")
.append(transformTableInfo.getTableName()).append("Repository extends JpaRepository<")
.append(transformTableInfo.getTableName()).append(",").append(priKey.getType())
.append(">, JpaSpecificationExecutor<").append(transformTableInfo.getTableName()).append("> {\n\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 java.util.ArrayList;\nimport java.util.stream.Collectors;\nimport javax.annotation.Resource;\nimport org.springframework.stereotype.Service;\nimport javax.persistence.criteria.Predicate;\nimport org.springframework.data.domain.Page;\nimport org.springframework.data.domain.PageRequest;\nimport org.springframework.data.jpa.domain.Specification;\n\nimport ")
.append(springBootCli.getPackageName()).append(".service.repository.").append(transformTableInfo.getTableName())
.append("Repository;\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("Repository ").append(StringUtil.firstToLowerCase(transformTableInfo.getTableName())).append("Repository;\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