org.mybatis.generator.api.plus.MybatisUtilBatchService Maven / Gradle / Ivy
package org.mybatis.generator.api.plus;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import java.io.*;
import java.util.Iterator;
import java.util.List;
/**
* @author wangfupeng
*/
public class MybatisUtilBatchService {
public static void writeBatchService(String basePackageName, String aspectPackageName, String targetProject, TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String tableName, String servicePackage, String mapperPackage, String modelPackage) {
File file = null;
OutputStream fileOutputStream = null;
BufferedWriter bufferedWriter = null;
try {
String packageNameFolder = servicePackage.replaceAll("\\.", "/");
targetProject = targetProject.replaceAll("\\./", "");
File folder = new File(targetProject + "/" + packageNameFolder + "/batch");
folder.mkdirs();
file = new File(targetProject + "/" + packageNameFolder + "/batch/Batch" + tableName + "Service.java");
//如果文件不存在,则创建文件,如果已存在,则覆盖
file.createNewFile();
fileOutputStream = new FileOutputStream(file);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
bufferedWriter.write("package " + servicePackage + ".batch;\n");
bufferedWriter.write("" +
"import " + mapperPackage+".batch.Batch" + tableName + "Mapper;\n" +
"import " + modelPackage +"."+ tableName + ";\n" +
"import com.gitee.fufu669.aspect.CacheMockFetchLater;\n" +
"import org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.stereotype.Service;\n" +
"import org.springframework.context.annotation.Lazy;\n" +
"\n" +
"import java.util.List;\n" +
"\n" +
"/*" + introspectedTable.getRemarks() + "的集体插入服务*/\n" +
"@Lazy\n" +
"@Service\n" +
"public class Batch" + tableName + "Service {\n" +
" \n" +
" @Autowired\n" +
" Batch" + tableName + "Mapper batch" + tableName + "Mapper;\n" +
" \n");
bufferedWriter.write("" +
" public Integer batchAdd(List<" + tableName + "> " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "s){\n");
List introspectedColumns2 = introspectedTable.getAllColumns();
Iterator iterator2 = introspectedColumns2.iterator();
bufferedWriter.write(" //批量插入\n");
bufferedWriter.write(" return batch" + tableName + "Mapper.batchAdd(" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "s);\n }\n");
bufferedWriter.write("}");
bufferedWriter.write("\n");
System.out.println("write to file success : " + file.getPath());
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtilCommon.closeBufferWriterAndFileOutputStream(fileOutputStream, bufferedWriter);
}
}
}