com.lq.cli.mybatis.CreateServiceTask 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.TableInfo;
import java.util.List;
public class CreateServiceTask extends CreateTask {
private List tableInfos;
public CreateServiceTask(TaskArgs taskArgs, List tableInfos) {
super(taskArgs);
this.tableInfos = tableInfos;
setPackageName("service");
}
@Override
public Boolean call() throws Exception {
if (checkDir()) {
for (TableInfo tableInfo : tableInfos) {
String service = tableInfo2Service(tableInfo);
createFile(tableInfo.getTransformTableInfo().getTableName() + "Service.java", service);
String serviceImpl = tableInfo2ServiceImpl(tableInfo);
createFile(tableInfo.getTransformTableInfo().getTableName() + "ServiceImpl.java", serviceImpl);
}
}
return true;
}
private String tableInfo2Service(TableInfo tableInfo) {
TableInfo transformTableInfo = tableInfo.getTransformTableInfo();
StringBuilder sb = new StringBuilder();
sb.append("package ").append(taskArgs.packageName).append(".").append(getPackageName())
.append(";\n\nimport ").append(taskArgs.packageName).append(".entity.")
.append(transformTableInfo.getTableName()).append(";\n\n")
.append("public interface ").append(transformTableInfo.getTableName()).append("Service {\n\n");
sb.append("\tboolean insert")
.append(transformTableInfo.getTableName())
.append("(")
.append(transformTableInfo.getTableName())
.append(" ")
.append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
.append(");\n\n");
transformTableInfo.getFiledEntities().stream()
.filter(tableFiledEntity -> tableFiledEntity.getKey().equals("PRI"))
.findFirst()
.ifPresent(priKey -> sb.append("\t")
.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("\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(taskArgs.packageName).append(".").append(getPackageName())
.append(";\n\nimport ").append(taskArgs.packageName).append(".entity.")
.append(transformTableInfo.getTableName());
if (taskArgs.cliConfig.isUseRedis()) {
sb.append(";\nimport org.springframework.data.redis.core.RedisTemplate");
}
sb.append(";\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Service;\nimport ")
.append(taskArgs.packageName).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@Autowired\n\tprivate ").append(transformTableInfo.getTableName())
.append("Mapper ").append(StringUtil.firstToLowerCase(transformTableInfo.getTableName())).append("Mapper;\n");
if (taskArgs.cliConfig.isUseRedis()) {
sb.append("\n\t@Autowired\n\tprivate RedisTemplate