日期:2014-05-16 浏览次数:20535 次
?java 数据库表反向生成相应的java类,同时完成了字段和表名中包含下划线的情况。
package cn.tongkuan.common; import java.io.File; import java.io.FileOutputStream; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class TableToJavaBean { private static final String LINE = "\r\n"; private static final String TAB = "\t"; String packages = this.getClass().getPackage().getName().replace("common", "model");; private static Map<String, String> map; static { map = new HashMap<String, String>(); map.put("VARCHAR", "String"); map.put("INTEGER", "Integer"); map.put("FLOAT", "float"); map.put("TIMESTAMP", "Date"); map.put("CHAR", "String"); map.put("DATETIME", "Date"); map.put("TIMESTAMP_IMPORT", "import java.util.Date"); map.put("DATETIME_IMPORT","import java.util.Date"); } public static String getPojoType(String dataType) { StringTokenizer st = new StringTokenizer(dataType); return map.get(st.nextToken()); } public static String getImport(String dataType) { if (map.get(dataType)==null||"".equals(map.get(dataType))) { return null; }else{ return map.get(dataType); } } public void tableToBean(Connection connection, String tableName) throws SQLException { String sql = "select * from " + tableName + " where 1 <> 1"; PreparedStatement ps = null; ResultSet rs = null; ps = connection.prepareStatement(sql); rs = ps.executeQuery(); ResultSetMetaData md = rs.getMetaData(); int columnCount = md.getColumnCount(); StringBuffer sb = new StringBuffer(); tableName = tableName.substring(0, 1).toUpperCase() + tableName.subSequence(1, tableName.length()); tableName = this.dealLine(tableName); sb.append("package " + this.packages + " ;"); sb.append(LINE); importPackage(md, columnCount, sb); sb.append(LINE); sb.append(LINE); sb.append("public class " + tableName + " {"); sb.append(LINE); defProperty(md, columnCount, sb); genSetGet(md, columnCount, sb); sb.append("}"); String paths = System.getProperty("user.dir"); String endPath = paths + "\\src\\" + (packages.replace("/", "\\")).replace(".", "\\"); buildJavaFile(endPath + "\\" + tableName + ".java", sb.toString()); } //属性生成get、 set 方法 private void genSetGet(ResultSetMetaData md, int columnCount, StringBuffer sb) throws SQLException { for (int i = 1; i <= columnCount; i++) { sb.append(TAB); String pojoType = getPojoType(md.getColumnTypeName(i)); String columnName = dealLine(md, i); String getName = null; String setName = null; if (columnName.length() > 1) { getName = "public " + pojoType + " get" + columnName.substring(0, 1).toUpperCase() + columnName.substring(1, columnName.length()) + "() {"; setName = "public void set" + columnName.substring(0, 1).toUpperCase() + columnName.substring(1, columnName.length()) + "(" + pojoType + " " + columnName + ") {"; } else { getName = "public get" + columnName.toUpperCase() + "() {"; setName = "public set" + columnName.toUpperCase() + "(" + pojoType + " " + columnName + ") {"; } sb.append(LINE).append(TAB).append(getName); sb.append(LINE).append(TAB).append(TAB); sb.append("return " + columnName + ";"); sb.append(LINE).append(TAB).append("}"); sb.append(LINE); sb.append(LINE).append(TAB).append(setName); sb.append(LINE).append(TAB).append(TAB); sb.append("this." + columnName + " = " + columnName + ";"); sb.append(LINE).append(TAB).append("}"); sb.append(LINE); } } //导入属性所需包 private void importPackage(ResultSetMetaData md, int columnCount, StringBuffer sb) throws SQLException { for (int i = 1; i <= column