package com.batch.tabletojava import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.util.ArrayList; import java.util.List; public class Table2JavaBean { private static final String TAB = " "; private static final String LINE = " "; public static void main(String[] args) { try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = null; String jdbcString = "jdbc:oracle:thin:@localhost:1521:xe"; conn = DriverManager.getConnection(jdbcString, "oracle", "oracle"); List<String> tableList = new ArrayList<String>(); tableList = getTableList(conn); for(String s: tableList){ makeJavaBean(conn, s); System.out.println(s); } }catch(Exception e){ e.printStackTrace(); } } private static void makeJavaBean( Connection conn ,String tableID) { FileOutputStream fo = null; DataOutputStream ds = null; File file = new File("c:\tableBean\" + tableID + ".java"); String sql = "select * from " + tableID + " where 1<>1"; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); ResultSetMetaData md = rs.getMetaData(); String tableName = getTableName(conn, tableID); int columnCount = md.getColumnCount(); tableID = tableID.substring(0, 1).toUpperCase() + tableID.substring(1); StringBuffer buff = new StringBuffer(); buff.append("/*----------------------------------------------------------------------------------------"); buff.append(LINE); buff.append("Program Name | " + tableID + ".java"); buff.append(LINE); buff.append("Language | Java"); buff.append(LINE); buff.append("Name | " + tableName + "类"); buff.append(LINE); buff.append("Preparation person | XX.XXX (XXXXX) 2013/12/20"); buff.append(LINE); buff.append("Note |"); buff.append(LINE); buff.append("------------------------------------------------------------------------------------------"); buff.append(LINE); buff.append("2013-12-20 | Original Source Release by XX.XXX (XXXX)"); buff.append(LINE); buff.append("------------------------------------------------------------------------------------------*/"); buff.append(LINE); buff.append("package com.batch.common;"); buff.append(LINE); buff.append(" "); buff.append(LINE); buff.append("/**"); buff.append(LINE); buff.append(" * " + tableName + "类."); buff.append(LINE); buff.append(" * "); buff.append(LINE); buff.append(" * @author XX.XXX"); buff.append(LINE); buff.append(" */"); buff.append(LINE); buff.append("public class " + tableID + " extends WWUMTCOMMON {"); buff.append(LINE); for (int i = 1; i <= columnCount; i++) { if("ZZ".equals(md.getColumnName(i).substring(0,2))){ continue; } // 取得字段注释 String columnComment = getColumnComment(conn, tableID, md.getColumnName(i)); String columnID = makeColumnID(md.getColumnName(i)); buff.append(TAB).append("/** "+columnComment+". */"); buff.append(LINE); buff.append(TAB); buff.append("private " + Table2JavaBean.toType(md.getColumnTypeName(i)) + " " + columnID.substring(0,1).toLowerCase() + columnID.substring(1)+ ";"); buff.append(LINE); } for (int i = 1; i <= columnCount; i++) { if("ZZ".equals(md.getColumnName(i).substring(0,2))){ continue; } // 取得字段注释 String columnComment = getColumnComment(conn, tableID, md.getColumnName(i)); buff.append(TAB); String pojiType = Table2JavaBean .toType(md.getColumnTypeName(i)); String columnName = makeColumnID(md.getColumnName(i)); columnName = columnName.substring(0, 1).toLowerCase() + columnName.substring(1); String getName = null; String setName = null; if (columnName.length() > 1) { // get buff.append(LINE).append(TAB).append("/** "); buff.append(LINE).append(TAB).append(" * 获取" +columnComment+"。"); buff.append(LINE).append(TAB).append(" * @return "+columnName +" " + columnComment); buff.append(LINE).append(TAB).append(" */"); getName = "public " + pojiType + " get" + columnName.substring(0, 1).toUpperCase() + columnName.substring(1) + "() {"; buff.append(LINE).append(TAB).append(getName); buff.append(LINE).append(TAB).append(TAB); buff.append("return " + columnName + ";"); buff.append(LINE).append(TAB).append("}"); buff.append(LINE); // set setName = "public void set" + columnName.substring(0, 1).toUpperCase() + columnName.substring(1) + "(" + pojiType + " " + columnName + ") {"; buff.append(LINE).append(TAB).append("/** "); buff.append(LINE).append(TAB).append(" * 设定"+ columnComment+"。 "); buff.append(LINE).append(TAB).append(" * @param "+columnName +" " + columnComment); buff.append(LINE).append(TAB).append(" */"); buff.append(LINE).append(TAB).append(setName); buff.append(LINE).append(TAB).append(TAB); buff.append("this." + columnName + " = " + columnName + ";"); buff.append(LINE).append(TAB).append("}"); buff.append(LINE); } } buff.append("}").append(LINE); fo = new FileOutputStream(file); ds = new DataOutputStream(fo); ds.write(buff.toString().getBytes()); }catch(Exception e){ e.printStackTrace(); } finally{ try{ if(ds != null){ ds.flush(); ds.close(); } if(fo != null){ fo.close(); } if(rs != null){ rs.close(); } if(ps != null){ ps.close(); } }catch (Exception e){ e.printStackTrace(); } } } private static String makeColumnID(String columnID) { String[] strs = columnID.toLowerCase().split("_"); StringBuffer sb = new StringBuffer(); for(int i = 0; i<strs.length; i++){ try{ sb.append(strs[i].substring(0, 1).toUpperCase() + strs[i].substring(1).toLowerCase()); }catch(Exception e){ e.printStackTrace(); System.out.println(columnID); } } return sb.toString(); } private static String getColumnComment(Connection conn, String tableID, String columnID){ String columnName = ""; // USER_TAB_COLUMNS String sql = "select COMMENTS from USER_COL_COMMENTS where TABLE_NAME = ? and COLUMN_NAME = ?"; PreparedStatement ps = null; ResultSet rs = null; try{ ps = conn.prepareStatement(sql); ps.setString(1, tableID); ps.setString(2, columnID); rs = ps.executeQuery(); while (rs.next()) { columnName = rs.getString("COMMENTS"); } }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(rs != null){ rs.close(); } if(ps != null){ ps.close(); } }catch(Exception e){ e.printStackTrace(); } } return columnName; } private static List<String> getTableList(Connection conn ){ try{}catch(Exception e){} List<String> tableList = new ArrayList<String>(); // USER_TAB_COLUMNS String sql = "select distinct TABLE_NAME from USER_TAB_COLUMNS"; // System.out.println(sql); PreparedStatement ps = null; ResultSet rs = null; try{ ps = conn.prepareStatement(sql); rs = ps.executeQuery(); String tableID = ""; while (rs.next()) { tableID = rs.getString("TABLE_NAME"); // System.out.println(tableID); tableList.add(tableID); } }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(rs != null){ rs.close(); } if(ps != null){ ps.close(); } }catch(Exception e){ e.printStackTrace(); } } return tableList; } private static String getTableName(Connection conn ,String tableID) throws Exception{ String tableName = ""; // USER_TAB_COLUMNS String sql = "select COMMENTS from USER_TAB_COMMENTS where TABLE_NAME = ?"; PreparedStatement ps = null; ResultSet rs = null; ps = conn.prepareStatement(sql); ps.setString(1, tableID); rs = ps.executeQuery(); while (rs.next()) { tableName = rs.getString("COMMENTS"); rs.close(); return tableName; } return tableName; } public static String toType(String str){ String result = null; if(str.startsWith("VARCHAR2") || str.startsWith("CHAR")){ result = "String"; }else if(str.startsWith("NUMBER")){ result = "int"; }else if(str.startsWith("DATE")){ result = "Date"; } return result; } }
网上查到很多关于java做成数据库表javabean的例子,但是都没有自动生成注释。
在网上查到的例子的基础上,改写了一下。发帖备份。