java讀取資料庫指定表,生成bean,獲取主鍵,自動生成get、set方法
阿新 • • 發佈:2019-02-10
開發一個程式涉及很多資料庫表,之前介紹過用
在使用mvc架構開發要將資料庫表變成bean,這也是個重複性無技術含量的工作,為了打發無聊編寫一個小工具,自動讀取資料庫並生成bean,同時生成get、set方法。
結果示例:import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class BeanUtil { private static final String TABLE_NAME = "TABLE_NAME"; private static final String COLUMN_NAME = "COLUMN_NAME"; private static final String DATA_TYPE = "DATA_TYPE"; private static final String DATA_SCALE = "DATA_SCALE"; private static final String ISPK = "ISPK"; private static final String COMMENTS = "COMMENTS"; public static void main(String[] args) { BeanUtil beanUtil = new BeanUtil(); beanUtil.createBean("tablename","beanname","com.test.bean","E:\\trunk\\Sources\\trust\\com\\test\\bean\\");//填寫表名,bean名,bean存放路徑 } @SuppressWarnings({ "rawtypes", "unchecked" }) private void createBean(String tableName,String className,String packageName,String path){ StringBuffer result = new StringBuffer(); StringBuffer getsetresult = new StringBuffer(); try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "hstdc", "tdc"); Statement stat = conn.createStatement(); StringBuffer sql = new StringBuffer(); sql.append(" select ") .append(" cols.TABLE_NAME, ") .append(" cols.COLUMN_NAME, ") .append(" cols.DATA_TYPE, ") .append(" cols.DATA_SCALE, ") .append(" com.COMMENTS, ") .append(" case when cc.column_name=cols.COLUMN_NAME then 1 ") .append(" else 0 end ISPK ") .append(" from ") .append(" user_tab_cols cols, ") .append(" user_constraints ct, ") .append(" user_cons_columns cc, ") .append(" user_col_comments com ") .append(" where cols.TABLE_NAME = ct.table_name(+) ") .append(" and cols.TABLE_NAME = com.table_name(+) ") .append(" and cols.COLUMN_NAME = com.column_name(+) ") .append(" and ct.constraint_name = cc.constraint_name ") .append(" and ct.constraint_type='P' ") .append(" and ct.TABLE_NAME='") .append(tableName) .append("'"); ResultSet rs = stat.executeQuery(sql.toString()); List<Map> ls = new ArrayList<Map>(); while(rs.next()){ Map map = new HashMap<String,Object>(); String columnName = rs.getString(BeanUtil.COLUMN_NAME); String dataType = rs.getString(BeanUtil.DATA_TYPE); Integer dataScale = rs.getInt(BeanUtil.DATA_SCALE); Boolean isPK = rs.getBoolean(BeanUtil.ISPK); String comments = rs.getString(BeanUtil.COMMENTS); map.put(BeanUtil.TABLE_NAME, tableName); map.put(BeanUtil.COLUMN_NAME, columnName); map.put(BeanUtil.DATA_TYPE, dataType); map.put(BeanUtil.DATA_SCALE, dataScale); map.put(BeanUtil.ISPK, isPK); map.put(BeanUtil.COMMENTS, comments); ls.add(map); } result.append("package ").append(packageName).append(";\n"); result.append("import java.io.Serializable;\n"); result.append("import javax.persistence.Column;\n"); result.append("import javax.persistence.Entity;\n"); result.append("import javax.persistence.Id;\n"); result.append("import javax.persistence.Table;\n"); result.append("//Auto-Generated by com.common.uitl.BeanUtil \n"); result.append("@Entity\n") .append("@Table(name=\"" + tableName + "\")\n") .append("public class " + className + " implements Serializable{\n"); for(int i=0; i<ls.size(); i++){ Map map = ls.get(i); String columnName = (String)map.get(BeanUtil.COLUMN_NAME); String varName = columnName.toLowerCase().substring(2);//擷取兩位之後的,由於欄位命名櫃子是型別加下劃線,如:L_NO,C_NAME String getsetName=varName.replaceFirst(varName.substring(0, 1),varName.substring(0, 1).toUpperCase()); String comments = (String)map.get(BeanUtil.COMMENTS); if(comments==null || comments.equals("")) comments = "無"; String dataType = (String)map.get(BeanUtil.DATA_TYPE); Integer dataScale = (Integer)map.get(BeanUtil.DATA_SCALE); Boolean isPK = (Boolean)map.get(BeanUtil.ISPK); //System.out.println(dataType); String varType = null; if(dataType.equals("DATE")){ varType = "Date"; }else if(dataType.equals("NUMBER") && dataScale > 0){ varType = "Double"; }else if(dataType.equals("NUMBER")){ varType = "Integer"; }else{ varType = "String"; } if(isPK){ result.append("\t @Id\n"); } result.append("\t @Column(name=\"" + columnName + "\")" + "\n") .append("\t private " + varType + " " + varName + ";" + "\t\t //" + comments + "\n\n"); getsetresult.append("\t public " + varType + " " + "get"+getsetName +"(){\n") .append("\t\t return "+varName+";"+"\n") .append("\t } \n\n") .append("\t public void " + "set"+getsetName +"("+varType + " " + varName +"){"+"\n") .append("\t\t"+"this."+varName+"="+varName+";"+"\n") .append("\t } \n\n"); } result.append(getsetresult).append("}"); File file = new File(path + className + ".java"); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); bw.write(result.toString()); bw.flush(); bw.close(); }catch(Exception e){ e.printStackTrace(); } } }
import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="TEST") public class Tset implements Serializable{ @Id @Column(name="C_MESTYPE") private String mestype; //報文型別 @Column(name="C_MESTYPE_NAME") private String mestype_name; //型別名稱 @Column(name="C_CONTENT") private String content; //報文內容 public String getMestype(){ return mestype; } public void setMestype(String mestype){ this.mestype=mestype; } public String getMestype_name(){ return mestype_name; } public void setMestype_name(String mestype_name){ this.mestype_name=mestype_name; } public String getContent(){ return content; } public void setContent(String content){ this.content=content; } }