SpringMVC实体类生成器
package com.generate; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; public class JDBC_Connection { public void getTableText(String[] TableNameList, String nameSpace) throws ClassNotFoundException, SQLException { String currentPath = "d:\\"; //数据地址url String cnnStr2 = "jdbc:db2://110.119.195.180:60000/SAMPLE"; //加载数据库驱动程序 Class.forName("com.ibm.db2.jcc.DB2Driver"); Connection conn = DriverManager.getConnection(cnnStr2, "user", "password"); Statement stmt = conn.createStatement(); ResultSet rst = null; String sql = ""; String outStr = ""; HashMap<String, String> mapColumn = new HashMap<String, String>(); for (String tabName : TableNameList) { String cols = ""; // POJO file PrintWriter prwPojo = this.getAPojoFile(currentPath + "\\" + getPOJOTableName(tabName) + ".java"); prwPojo.println(); prwPojo.println("import java.io.Serializable;"); prwPojo.println("import java.util.Date;"); prwPojo.println("import java.sql.Timestamp;"); prwPojo.println("import javax.persistence.Column;"); prwPojo.println("import javax.persistence.Entity;"); prwPojo.println("import javax.persistence.Id;"); prwPojo.println(); //添加Entity注解 prwPojo.println("@Entity(name = \"" + tabName + "\")"); prwPojo.println("public class " + getPOJOTableName(tabName) + " implements Serializable {"); prwPojo.println(); prwPojo.println(" private static final long serialVersionUID = 1L;"); // Get COLNAME, TYPENAME, LENGTH, REMARKS sql = "SELECT COLNAME, TYPENAME, LENGTH, REMARKS FROM SYSCAT.COLUMNS WHERE TABNAME=‘" + tabName.toUpperCase() + "‘ ORDER BY COLNO"; rst = stmt.executeQuery(sql); while (rst.next()) { //添加注释 outStr = this.getPojoRow(rst.getString("COLNAME"), rst.getString("TYPENAME"), true); prwPojo.println(""); prwPojo.println(" /**"); prwPojo.println(" * " + rst.getString("REMARKS")); prwPojo.println(" */"); prwPojo.println(" " + outStr); String COLNAME = rst.getString("COLNAME"); mapColumn.put(COLNAME, getFiledName(COLNAME)); cols = cols + rst.getString("COLNAME") + ","; } rst.close(); prwPojo.println(); sql = "SELECT COLNAME, TYPENAME, KEYSEQ FROM SYSCAT.COLUMNS WHERE TABNAME=‘" + tabName.toUpperCase() + "‘ ORDER BY COLNO"; rst = stmt.executeQuery(sql); while (rst.next()) { //get、set方法 outStr = this.getPojoSetAndGetRow(rst.getString("COLNAME"), rst.getString("TYPENAME"), rst.getString("KEYSEQ"), true); prwPojo.println(" " + outStr); } rst.close(); prwPojo.println("}"); prwPojo.close(); System.out.println(tabName + " Done!"); } stmt.close(); conn.close(); } private String getPojoRow(String COLNAME, String TYPENAME, boolean IsInit) { String typeStr = this.getStrType(TYPENAME); String outStr = "private " + typeStr + " " + getFiledName(COLNAME) + ";"; return outStr; } private String getPojoSetAndGetRow( String COLNAME, String TYPENAME, String KEYSEQ, boolean IsInit) { String typeStr = this.getStrType(TYPENAME); String outStr = getSetFunction(COLNAME, typeStr); outStr = outStr + "\n" + getGetFunction(COLNAME, KEYSEQ, typeStr); return outStr; } private String getSetFunction(String COLNAME, String TYPENAME) { String name = "public " + "void" + " " + "set"; name = name + getPOJOCamelName(COLNAME); name = name + "(" + TYPENAME + " " + getFiledName(COLNAME) + ") {"; name = name + "\n"; name = name + " this." + getFiledName(COLNAME) + " = " + getFiledName(COLNAME) + ";" + "\n }\n"; return name; } private String getGetFunction(String COLNAME, String KEYSEQ, String TYPENAME) { String name = ""; if (KEYSEQ != null) { name = name + " @Id\n"; } name = name + " @Column(name = \"" + COLNAME + "\")"; name = name + "\n"; name = name + " public " + TYPENAME + " " + "get"; name = name + getPOJOCamelName(COLNAME); name = name + "() {"; name = name + "\n"; name = name + " return this." + getFiledName(COLNAME) + ";" + "\n }\n"; return name; } private String getStrType(String TypeName) { String typeStr = "String"; if (TypeName.equalsIgnoreCase("VARCHAR")) { typeStr = "String"; } else if (TypeName.equalsIgnoreCase("INTEGER") || TypeName.equalsIgnoreCase("BIGINT")) { typeStr = "Integer"; } else if (TypeName.equalsIgnoreCase("DATE")) { typeStr = "Date"; } else if (TypeName.equalsIgnoreCase("TIMESTAMP")) { typeStr = "Timestamp"; } else if (TypeName.equalsIgnoreCase("CLOB")) { typeStr = "Clob"; } else if (TypeName.equalsIgnoreCase("DECIMAL")) { typeStr = "double"; } return typeStr; } private static String getCName(String str) { String name = ""; name += str.substring(0, 1); for (int i = 1; i < str.length(); i++) { name += str.substring(i, i + 1).toLowerCase(); } return name; } private static String getCName2(String str) { String name = ""; // name+=str.substring(0, 1); for (int i = 0; i < str.length(); i++) { name += str.substring(i, i + 1).toLowerCase(); } return name; } public String getClassName(String str) { String name = ""; String[] arg = str.split("_"); for (int i = 0; i < arg.length; i++) { name += getCName(arg[i]); } return name + "ModelCrud"; } public String getPOJOCamelName(String str) { String name = ""; String[] arg = str.split("_"); for (int i = 0; i < arg.length; i++) { name += getCName(arg[i]); } return name; } public String getPOJOTableName(String str) { String name = ""; String[] arg = str.split("_"); for (int i = 0; i < arg.length; i++) { name += getCName(arg[i]); } return name; } public String getFiledName(String str) { String name = ""; String[] arg = str.split("_"); for (int i = 0; i < arg.length; i++) { if (i == 0) { name += getCName2(arg[i]); } else { name += getCName(arg[i]); } } return name; } private PrintWriter getAPojoFile(String PojoFileName) { PrintWriter prw = null; try { prw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(PojoFileName), "UTF-8")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return prw; } } package com.generate; public class TestJDBC { public static void main(String[] args) { try { String[] tablesName = new String[]{"EMPLOYEE","PRODUCT"}; ///调用JDBC连接,生成对应的实体类 JDBC_Connection jd = new JDBC_Connection(); jd.getTableText(tablesName, "oms"); } catch (Exception e) { e.printStackTrace(); } } }
最后生成的文件会放在D:\路径下,若表名为 USER,则会出现一个user.java文件,文件里的格式大致如下:
@Entity(name = "USER") class User { /** * 员工姓名 */ private String userName; /** * @return the UserName */ @Column(name = "USER_NAME") public String getUserName() { return userName; } /** * @param UserName * the UserName to set */ public void setUserName(String userName) { this.userName = userName; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。