org.mybatis.generator.api.plus.MybatisUtilBatchMapper Maven / Gradle / Ivy
The newest version!
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.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import java.io.*;
import java.util.Iterator;
import java.util.List;
/**
* @author wangfupeng
*/
public class MybatisUtilBatchMapper {
public static void writeBatchMapper(String basePackageName, String aspectPackageName, String targetProject, TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String tableName, String mapperPackage, String modelPackage) {
File file = null;
OutputStream fileOutputStream = null;
BufferedWriter bufferedWriter = null;
try {
String packageNameFolder = mapperPackage.replaceAll("\\.", "/");
targetProject = targetProject.replaceAll("\\./", "");
File folder = new File(targetProject + "/" + packageNameFolder + "/batch");
folder.mkdirs();
file = new File(targetProject + "/" + packageNameFolder + "/batch/Batch" + tableName + "Mapper.java");
//如果文件不存在,则创建文件,如果已存在,则覆盖
file.createNewFile();
fileOutputStream = new FileOutputStream(file);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
bufferedWriter.write("package " + mapperPackage+".batch;\n" +
"\n" +
"import " + modelPackage+"." + tableName + ";\n");
bufferedWriter.write("import org.apache.ibatis.annotations.Mapper;\n" +
"import org.apache.ibatis.annotations.Insert;\n" +
"import org.springframework.context.annotation.Lazy;\n" +
"import org.apache.ibatis.annotations.Param;\n" +
"\n" +
"import java.util.List;\n" +
"import java.util.Date;\n" +
"\n" +
"/*" + introspectedTable.getRemarks() + "的集体插入*/\n" +
"@Lazy\n" +
"@Mapper\n" +
"public interface Batch" + tableName + "Mapper {\n" +
" \n");
List introspectedColumns = introspectedTable.getAllColumns();
Iterator iterator = introspectedColumns.iterator();
while (iterator.hasNext()) {
IntrospectedColumn introspectedColumn = iterator.next();
String columnName = introspectedColumn.getJavaProperty();
}
bufferedWriter.write(" @Insert(\"\")\n");
bufferedWriter.write(" public Integer batchAdd(" +
"@Param(value=\"" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "s\") " +
"List<" + tableName + "> " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "s);\n" +
"\n");
bufferedWriter.write(" \n" + "}");
bufferedWriter.write("\n");
System.out.println("write to file success : " + file.getPath());
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtilCommon.closeBufferWriterAndFileOutputStream(fileOutputStream, bufferedWriter);
}
}
}