步骤:通过DataBaseMetaData一次获取想要的各个表和相关的列信息。
再分析各个列的类型,生成文件。
---------------------------------------------------------------------------------------------------
非常简单,只需要读一次数据库,同样的原理可以来实现DAO层。
配合几个利用元数据和反射来处理DTO和DAO,即可实现简单的ORM.
正在实验中...
---------------------------------------------------------------------------------------------------
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;
/**
* @author KimmKing
*/
public class test {
private static final String T = "\t";
private static final String CRLF = "\r\n";
static String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
static String url = "jdbc:odbc:qsoft";
static String user = "sa";
static String password = "q11";
static HashMap map = new HashMap();
static HashMap type = new HashMap();
static {
type.put("4", "int");
type.put("12", "String");
type.put("93", "Date");
type.put("-1", "String");
}
static String path = "d:\\DTO";
public static void main(String[] args) {
// testODBC();
findTableAndColumn();
Set set = map.entrySet();
for (Iterator iter = set.iterator(); iter.hasNext();) {
Entry element = (Entry) iter.next();
TableBean table = (TableBean) element.getValue();
createDto(table);
}
}
private static void createDto(TableBean table) {
StringBuffer sb = new StringBuffer();
StringBuffer tostring = new StringBuffer();
sb.append("package com.qsoft.dto;");
sb.append(CRLF);
sb.append(CRLF);
sb.append("/**");
sb.append(CRLF);
sb.append(" * @author KimmKing");
sb.append(CRLF);
sb.append(" * create time:" + new Date().toLocaleString());
sb.append(CRLF);
sb.append("*/");
sb.append(CRLF);
sb.append(CRLF);
sb.append("import java.util.Date;");
sb.append(CRLF);
sb.append(CRLF);
String tablename = table.getName();
String classname = convertT2C(tablename);
List list = table.getColumns();
// tostring.append(classname);
// tostring.append("[");
sb.append("public class " + classname + " implements IDto {");
sb.append(CRLF);
for (Iterator iter = list.iterator(); iter.hasNext();) {
ColumnBean column = (ColumnBean) iter.next();
String columnname = column.getName().toLowerCase();
String stype = type.get(column.getType() + "").toString();
sb.append(T);
sb.append("private " + stype + " " + columnname + ";");
sb.append(T);
sb.append("//Length:");
sb.append(column.getLength());
sb.append(CRLF);
sb.append(CRLF);
// getter
sb.append(T);
sb.append("public " + stype + " " + getGetter(columnname) + "() {");
sb.append(CRLF);
sb.append(T);
sb.append(T);
sb.append("return " + columnname + ";");
sb.append(CRLF);
sb.append(T);
sb.append("}");
sb.append(CRLF);
sb.append(CRLF);
// setter
sb.append(T);
sb.append("public void " + getSetter(columnname) + "(" + stype
+ " _" + columnname + ") {");
sb.append(CRLF);
sb.append(T);
sb.append(T);
sb.append("this." + columnname + "=_" + columnname + ";");
sb.append(CRLF);
sb.append(T);
sb.append("}");
sb.append(CRLF);
sb.append(CRLF);
sb.append(CRLF);
//
tostring.append("\"," + columnname + "=\" + " + columnname + " + ");
}
String pv = tostring.toString();
pv = pv.substring(2);
// toString
sb.append(T);
sb.append("public String toString() {");
sb.append(CRLF);
sb.append(T);
sb.append(T);
sb.append("return \"" + classname + "[" + pv + "\"]\";");
sb.append(CRLF);
sb.append(T);
sb.append("}");
sb.append(CRLF);
sb.append(CRLF);
sb.append("}");
sb.append(CRLF);
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
File file = new File(path + "\\" + classname + ".java");
try {
FileWriter writer = new FileWriter(file);
writer.write(sb.toString());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static String convertT2C(String tablename) {
String classname = tablename.substring(1).toUpperCase();
String first = classname.substring(0,1).toUpperCase();
classname = first + classname.substring(1).toLowerCase() + "Dto";
return classname;
}
private static String getSetter(String propertyName) {
String first = propertyName.substring(0, 1).toUpperCase();
String methodName = "set" + first
+ propertyName.substring(1).toLowerCase();
return methodName;
}
private static String getGetter(String propertyName) {
String first = propertyName.substring(0, 1).toUpperCase();
String methodName = "get" + first
+ propertyName.substring(1).toLowerCase();
return methodName;
}
private static void findTableAndColumn() {
try {
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url, user, password);
DatabaseMetaData meta = conn.getMetaData();
ResultSet rs = meta.getColumns(null, null, "c%", null);
while (rs.next()) {
String tablename = rs.getString(3);
TableBean table = (TableBean) map.get(tablename);
if (table == null) {
table = new TableBean();
table.setName(tablename);
map.put(tablename, table);
}
ColumnBean column = new ColumnBean();
column.setName(rs.getString(4));
column.setType(rs.getInt(5));
column.setLength(rs.getInt(7));
table.getColumns().add(column);
}
rs.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private static void testmeta() {
// String sql = "select * from cuser ";
try {
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url, user, password);
DatabaseMetaData meta = conn.getMetaData();
ResultSet rs = meta.getColumns(null, null, "c%", null);
while (rs.next()) {
print(rs);
System.out.println();
}
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private static void print(ResultSet rs) {
try {
int count = rs.getMetaData().getColumnCount();
for (int i = 0; i < count; i++) {
System.out.print(rs.getString(1 + i));
System.out.print("\t");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void testODBC() {
String sql = "select * from cuser ";
try {
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString(2));
}
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private static void print(List list) {
for (Iterator iter = list.iterator(); iter.hasNext();) {
System.out.println(iter.next());
}
}
}
/**
* @author KimmKing
*
*/
public class ColumnBean {
private String name;
private int type;
private int length;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
import java.util.ArrayList;
import java.util.List;
/**
* @author KimmKing
*
*/
public class TableBean {
private String name;
private List columns = new ArrayList();
public List getColumns() {
return columns;
}
public void setColumns(List columns) {
this.columns = columns;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1629786