运行环境 XP MYECLIPSE8。5 JDK1。6。30
根据提供domain 类, 自动生成其对应的DAO的 crud操作。
package com.wjj.domain;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.wjj.generator;
import com.wjj.io.utils.IOUtil;
public class DAOGenerator {
public static void Generate(Class clazz) throws Exception{
StringBuilder sBuilder = new StringBuilder();
sBuilder.append("package com.wjj.generated.dao;");
sBuilder.append("\n");
sBuilder.append("import " + clazz.getPackage().getName() + "."
+ clazz.getSimpleName() + ";");
sBuilder.append("\n");
sBuilder.append("public class " + clazz.getSimpleName() + "DAO");
sBuilder.append("\n");
sBuilder.append("{");
sBuilder.append("\n");
GenerateInsert(clazz, sBuilder);
GenerateDelete(clazz, sBuilder);
GenerateUpdate(clazz, sBuilder);
GenerateList(clazz, sBuilder);
sBuilder.append("}");
sBuilder.append("\n");
IOUtil.writeStringFileToDesk(sBuilder.toString().getBytes(),clazz.getSimpleName()+"DAO");
System.out.println("=====================");
System.out.println(sBuilder.toString());
}
protected static void GenerateInsert(Class clazz, StringBuilder sBuilder) {
sBuilder.append("public void Insert" + clazz.getSimpleName() + "("+clazz.getSimpleName()+ " " + clazz.getSimpleName().toLowerCase() +"){");
sBuilder.append("\n");
System.out
.println(" you can append jdbc or hibernate or ibatis ... to here");
sBuilder.append("}");
sBuilder.append("\n");
}
protected static void GenerateDelete(Class clazz, StringBuilder sBuilder) {
sBuilder.append("public void Delete" + clazz.getSimpleName() + "("+clazz.getSimpleName()+ " " + clazz.getSimpleName().toLowerCase() +"){");
sBuilder.append("\n");
System.out
.println(" you can append jdbc or hibernate or ibatis ... to here");
sBuilder.append("}");
sBuilder.append("\n");
}
protected static void GenerateUpdate(Class clazz, StringBuilder sBuilder) {
sBuilder.append("public void Update" + clazz.getSimpleName() + "("+clazz.getSimpleName()+ " " + clazz.getSimpleName().toLowerCase() +"){");
sBuilder.append("\n");
System.out
.println(" you can append jdbc or hibernate or ibatis ... to here");
sBuilder.append("}");
sBuilder.append("\n");
}
protected static void GenerateList(Class clazz, StringBuilder sBuilder) {
sBuilder.append("public void Query" + clazz.getSimpleName() + "(){");
sBuilder.append("\n");
System.out
.println(" you can append jdbc or hibernate or ibatis ... to here");
sBuilder.append("}");
sBuilder.append("\n");
}
}
package com.wjj.io.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class IOUtil {
public static void writeStringFileToDesk(byte[] data,String fileName)throws Exception{
File file = new File(System.getProperty("user.dir")+"\\src\\com\\wjj\\generated\\dao",fileName + ".java");
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
System.out.println("file.getAbsolutePath()---> " + file.getAbsolutePath());
OutputStream os = new FileOutputStream(file);
os.write(data,0,data.length);
os.close();
}
}
package com.wjj.test;
import com.wjj.generator.DAOGenerator;
public class TestMain {
public static void main(String[] args) throws Exception{
Class clazz = Class.forName("com.wjj.domain.User");
System.out.println(clazz.getSimpleName());
DAOGenerator.Generate(clazz);
}
}
本tool 最终将会实现通过一个domain 自动生成其 dao ,service, action 及前台页面功能。
因为工司不让上外网, 因为公司无法上外网, 无法将公司里面实现完的共布出来,只能在家里重新写一个了。。