package com.zhcj.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class AutoGenerateSD {
static String CalssName = "User";// FIXME 填写新建的Model类名称
static String EntityName = CalssName.substring(0, 1).toLowerCase() + CalssName.substring(1, CalssName.length());
// 主入口
public static void main(String[] args) throws Exception {
GenerateFile();// 生成文件
// removeFile();// 删除文件
}
@SuppressWarnings("unused")
private static void removeFile() {
String rootPath = System.getProperty("user.dir") + "/src/main/java/com/zhcj/";
// 删除接口dao
delFile(rootPath + "/dao", CalssName + "Dao.java");
System.out.println("删除Dao完成");
// 删除显示类daoImpl
delFile(rootPath + "/dao/impl", CalssName + "DaoImpl.java");
System.out.println("删除DaoImpl完成");
// 删除接口service
delFile(rootPath + "/service", CalssName + "Service.java");
System.out.println("删除Service完成");
// 删除显示类serviceImpl
delFile(rootPath + "/service/impl", CalssName + "ServiceImpl.java");
System.out.println("删除ServiceImpl完成");
}
@SuppressWarnings("unused")
private static void GenerateFile() {
try {
String rootPath = System.getProperty("user.dir") + "/src/main/java/com/zhcj/";
System.out.println("rootPath:"+rootPath);
// 创建接口dao
createDaoFile(rootPath + "/dao", CalssName + "Dao.java");
System.out.println("创建Dao完成");
// 创建显示类daoImpl
createDaoImplFile(rootPath + "/dao/impl", CalssName + "DaoImpl.java");
System.out.println("创建DaoImpl完成");
// 创建接口service
createServiceFile(rootPath + "/service", CalssName + "Service.java");
System.out.println("创建Service完成");
// 创建显示类serviceImpl
createServiceImplFile(rootPath + "/service/impl", CalssName + "ServiceImpl.java");
System.out.println("创建ServiceImpl完成");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void createServiceImplFile(String path, String filename) throws IOException {
File file = createFile(path + "/" + filename);
FileOutputStream out = new FileOutputStream(file, true);
StringBuffer sb = new StringBuffer();
sb.append("package com.zhcj.service.impl;\n");
sb.append("import org.springframework.beans.factory.annotation.Autowired;\n");
sb.append("import org.springframework.stereotype.Service;\n");
sb.append("import com.zhcj.dao.BaseDao;\n");
sb.append("import com.zhcj.dao." + CalssName + "Dao;\n");
sb.append("import com.zhcj.model." + CalssName + ";\n");
sb.append("import com.zhcj.service." + CalssName + "Service;\n");
sb.append("@Service(\"" + EntityName + "Service\")\n");
sb.append("public class " + CalssName + "ServiceImpl extends BaseServiceImpl<" + CalssName + "> implements "
+ CalssName + "Service{\n");
sb.append("@Autowired\n");
sb.append("private " + CalssName + "Dao " + EntityName + "Dao;\n");
sb.append("@Override\n");
sb.append("public BaseDao<" + CalssName + "> getBaseDao() {\n");
sb.append("return this." + EntityName + "Dao;\n\n}}");
out.write(sb.toString().getBytes("utf-8"));
out.close();
}
private static void createServiceFile(String path, String filename) throws IOException {
File file = createFile(path + "/" + filename);
FileOutputStream out = new FileOutputStream(file, true);
StringBuffer sb = new StringBuffer();
sb.append("package com.zhcj.service;\n");
sb.append("import com.zhcj.model." + CalssName + ";\n");
sb.append("public interface " + CalssName + "Service extends BaseService<" + CalssName + ">{\n\n}");
out.write(sb.toString().getBytes("utf-8"));
out.close();
}
public static File createFile(String fileName) throws IOException {
File file = new File(fileName);
if (!file.exists())
file.createNewFile();
return file;
}
private static void createDaoImplFile(String path, String filename) throws IOException {
File file = createFile(path + "/" + filename);
FileOutputStream out = new FileOutputStream(file, true);
StringBuffer sb = new StringBuffer();
// sb.append("这是第" + i + "行:前面介绍的各种方法都不关用,为什么总是奇怪的问题 ");
sb.append("package com.zhcj.dao.impl;\n");
sb.append("import org.springframework.stereotype.Component;\n");
sb.append("import com.zhcj.dao." + CalssName + "Dao;\n");
sb.append("import com.zhcj.model." + CalssName + ";\n");
sb.append("@Component(\"" + EntityName + "Dao\")\n");
sb.append("public class " + CalssName + "DaoImpl extends BaseDaoImpl<" + CalssName + "> implements " + CalssName
+ "Dao{\n\n}");
out.write(sb.toString().getBytes("utf-8"));
out.close();
}
public static void createDaoFile(String path, String filename) throws IOException {
File file = createFile(path + "/" + filename);
FileOutputStream out = new FileOutputStream(file, true);
StringBuffer sb = new StringBuffer();
sb.append("package com.zhcj.dao;\n");
sb.append("import com.zhcj.model." + CalssName + ";\n");
sb.append("public interface " + CalssName + "Dao extends BaseDao<" + CalssName + ">{\n\n}");
out.write(sb.toString().getBytes("utf-8"));
out.close();
}
public static void delFile(String path, String filename) {
File file = new File(path + "/" + filename);
if (file.exists() && file.isFile())
file.delete();
}
}
简简单单,放在test包里,根据实体类名运行就能在项目的目录里建立相关的dao,daoImpl,service,serviceImpl等文件,不需要反复写一样的机械代码,附带删除,万一写错还能一键删除,都是为了方便