闲余时间写了个类来自动生成实体类。希望后期能自动获得数据库字段和数据类型。
代码:
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Scanner; public class EntityFactory { /** * 实体类生成工具 输入/输出 路径 将\改成\\ outClass{Classname(int @@@,....)}//Classname为类名 * int 为数据类型 @@@变量名 遵守方法命令规范 */ private String outpath;// 生成的类所在的目录 private String sentence;// 设置语句 遵守格式 outClass{Classname(int @@@,....)} public EntityFactory(String outpath, String sentence) { super(); this.outpath = outpath; this.sentence = sentence; } public EntityFactory() { } public String getSentence() { return sentence; } public void setSentence(String sentence) { this.sentence = sentence; } public String getOUTPATH() { return outpath; } public void setOUTPATH(String oUTPATH) { outpath = oUTPATH; } public File makeFile(String name, String type) { /** * 生成的文件 名字和后缀名 */ String path = outpath + name + "." + type; File outf = new File(path); if (outf.exists()) { outf.delete(); } else { try { outf.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return outf; } public String getClassName(String str) { /** * 获得类名 */ String className = null; if (str.contains("outClass")) { int start = str.indexOf("{"); int sp = str.indexOf("("); className = str.substring(start + 1, sp); } return className; } public ArrayListsplitString(String str) { /** * 取出关键字 类型 变量名 int @@@ */ ArrayList list = new ArrayList (); if (str.contains("outClass")) { int sp = str.indexOf("("); int end = str.lastIndexOf(")"); String content = str.substring(sp + 1, end); String[] info = content.split(","); for (String s : info) { String[] detail = s.split(" "); list.add(detail); } } return list; } public String initial(String str) { /** * 首字母大写 */ String s = str.substring(0, 1); return s.toUpperCase() + str.substring(1); } public void writeFile() { /** * bw.write("**实体类自动生成"); 写入文件 */ BufferedWriter bw = null; try { File file = makeFile(getClassName(sentence), "java"); bw = new BufferedWriter(new FileWriter(file)); ArrayList list = splitString(sentence); bw.write("public class " + getClassName(sentence) + "{"); bw.newLine(); bw.write("/*****"); bw.newLine(); bw.newLine(); bw.write("**@time" + new SimpleDateFormat("yyyy-MM-dd").format(new Date())); bw.newLine(); bw.write("****/"); bw.newLine(); for (int i = 0; i < list.size(); i++) { /** * 属性private */ String[] detail = list.get(i); bw.write("private\t" + detail[0] + "\t" + detail[1] + " ;"); bw.newLine(); bw.newLine(); } bw.newLine(); /** 无参数构造方法 **/ bw.write("public\t" + getClassName(sentence) + "() {}"); bw.newLine(); bw.newLine(); /** * // 有参数构造方法 */ bw.write("public\t" + getClassName(sentence) + "("); bw.newLine(); for (int i = 0; i < list.size(); i++) { String[] detail = list.get(i); if (i != list.size() - 1) { bw.write(detail[0] + " " + detail[1] + ","); } else { bw.write(detail[0] + " " + detail[1]); } } bw.write(")"); bw.write("{"); for (int i = 0; i < list.size(); i++) { String[] detail = list.get(i); bw.write("this." + detail[1] + "=" + detail[1] + ";"); bw.newLine(); } bw.write("\t}"); bw.newLine(); /** * // get set方法 */ for (int i = 0; i < list.size(); i++) { String[] detail = list.get(i); bw.newLine(); bw.write("\tpublic\t" + detail[0] + "\t" + "get" + initial(detail[1]) + " () {"); bw.newLine(); bw.write("\t" + "return" + "\t" + detail[1] + ";"); bw.newLine(); bw.write("\t}"); bw.newLine(); bw.write("\tpublic\t" + "void\t" + "set" + initial(detail[1]) + "(" + detail[0] + " " + detail[1] + ") {"); bw.newLine(); bw.write("\t" + "this." + detail[1] + "=" + detail[1] + ";"); bw.newLine(); bw.write("\t}"); bw.newLine(); } bw.newLine(); bw.newLine(); /** * // toString 方法 */ bw.write("public\tString\ttoString () {"); bw.newLine(); bw.write("\t" + "return" + "\t" + "\"" + getClassName(sentence) + "["); for (int i = 0; i < list.size(); i++) { String[] detail = list.get(i); if (i == list.size() - 1) { bw.write(detail[1] + "=" + "\"" + "+" + detail[1] + "+" + "\"" + "]" + "\"" + ";"); } else { bw.write(detail[1] + "=" + "\"" + "+" + detail[1] + "+" + "\"" + ","); } } bw.newLine(); bw.write("\t}"); bw.newLine(); bw.newLine(); bw.write("}"); bw.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public boolean checkClass() { boolean boo = true; if (getClassName(sentence) == null) { System.out.println("缺少outClass关键字"); boo = false; } return boo; } public void produceClass() { Scanner input = new Scanner(System.in); System.out.println("请输入目标文件夹(格式D:\\\\)"); String pa = input.nextLine(); // ef.setOUTPATH("D:\\"); setOUTPATH(pa); System.out .println("请outClass命令 outClass{Classname(int @@@,....)}//Classname为类名* int 为数据类型 @@@变量名"); String classkey = input.nextLine(); // ef.setSentence("outClass{Test(int num1,int num2,String b,String name,String password)}"); setSentence(classkey); boolean boo = checkClass(); if (boo) { writeFile(); System.out.println("已经生成" + getClassName(sentence) + ".java" + "\t路径" + outpath); } else { System.out.println("生成失败!!"); } } public static void main(String[] args) { EntityFactory ef = new EntityFactory(); ef.produceClass(); } }