快下班了,写点东西吧,
这个小工具写的有几个月了,中间做个小demo的时候还用到了,
自我感觉用着挺爽的,很小,一个类,很快,全自动,很方便,直接修改配置就行。
不多说了。
package com.base; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * @author q821424508@sina.com * @version 1.0 * */ public class Table2Entity { private static String Entity2Store = "src/com/hung/model";// 以项目名为根目录 //private static String tablesSql = "SELECT table_name FROM information_schema.tables WHERE table_schema ='common' AND NOT table_name LIKE 'sys%'"; private static String tablesSql = "SELECT table_name FROM information_schema.tables WHERE table_schema ='common' "; private static List<String> tables = new ArrayList<String>(); private static String url = "jdbc:mysql://127.0.0.1/common?useUnicode=true&characterEncoding=utf8"; private static String driver = "com.mysql.jdbc.Driver"; private static String username = "admin"; private static String password = "123456"; private static String sql = "select * from "; private static String author = "q821424508"; private static Connection conn = null; private static Statement stat = null; public static void main(String[] args) { System.out.println("---begin---"); init(); table2Entity(); System.out.println("---end---"); } /** */ private static void init() { File f = new File(Entity2Store); if (!f.exists()) { f.mkdirs(); } } /** */ private static void table2Entity() { tables = getTables(); if (null != tables && tables.size() > 0) { for (String tableName : tables) { doTable2Entity(tableName); } } } /** */ private static void doTable2Entity(String tableName) { String sql1 = sql + tableName; System.out.println("tableName = " + tableName + "\n"); conn = getConn(); try { stat = conn.createStatement(); ResultSet rs = stat.executeQuery(sql1); ResultSetMetaData metaDate = rs.getMetaData(); int count = metaDate.getColumnCount(); File classFile = getCF(tableName); FileWriter fw = new FileWriter(classFile, true); BufferedWriter bw = new BufferedWriter(fw); StringBuffer sbFields = new StringBuffer(); //toString 方法 StringBuffer toString = new StringBuffer("\tpublic String toString(){\n"); toString.append("\t\tString str=\"{"); for (int j = 0; j < count; j++) { String columnName = metaDate.getColumnName(j + 1); int type = metaDate.getColumnType(j + 1); System.out.println(columnName + " : " + type); sbFields.append(column2field(columnName, type)); toString.append(""+column2fieldName(columnName)+":'\" + "+column2fieldName(columnName)+" + \"',\" + \""); } bw.write(sbFields.toString()); //toString.replace(toString.length()-1, toString.length(), ""); toString.replace(toString.length()-8, toString.length(), ""); toString.append("\"'}\";\n"); toString.append("\t\treturn str;\n\t}\n"); bw.write(toString.toString()); bw.write("\n}\n"); bw.close(); fw.close(); System.out.println("---------------------------------"); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @param columnName * @param type * @return */ private static String column2field(String columnName, int type) { String field = ""; String getseter = ""; String fieldName = column2fieldName(columnName); field = getField(fieldName, type); getseter = getGetseter(fieldName, type); return field + "\n" + getseter; } /** * @param columnName * @param type * @return */ private static String getGetseter(String columnName, int type) { String filedName = firstChar2Upper(columnName); StringBuffer sb = new StringBuffer(""); switch (type) { case Types.BIGINT: sb.append("\tpublic long get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(long "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.INTEGER: sb.append("\tpublic int get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(int "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.CHAR: sb.append("\tpublic String get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(String "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.DATE: sb.append("\tpublic Date get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(Date "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.DOUBLE: sb.append("\tpublic double get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(double "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.FLOAT: sb.append("\tpublic float get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(float "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.LONGNVARCHAR: sb.append("\tpublic String get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(String "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.LONGVARCHAR: sb.append("\tpublic String get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(String "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.NVARCHAR: sb.append("\tpublic String get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(String "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.SMALLINT: sb.append("\tpublic int get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(int "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.TIMESTAMP: sb.append("\tpublic Date get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(Date "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.TINYINT: sb.append("\tpublic int get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(int "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.NUMERIC: sb.append("\tpublic long get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(long "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.VARCHAR: sb.append("\tpublic String get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(String "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.BLOB: sb.append("\tpublic byte[] get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(byte[] "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; default: break; } return sb.toString(); } /** * @param columnName * @param type * @return */ private static String getField(String columnName, int type) { StringBuffer sb = new StringBuffer("\tprivate "); switch (type) { case Types.BIGINT: sb.append("long ").append(columnName); break; case Types.INTEGER: sb.append("int ").append(columnName); break; case Types.NUMERIC: sb.append("long ").append(columnName); break; case Types.CHAR: sb.append("String ").append(columnName); break; case Types.DATE: sb.append("Date ").append(columnName); break; case Types.DOUBLE: sb.append("double ").append(columnName); break; case Types.FLOAT: sb.append("float ").append(columnName); break; case Types.LONGNVARCHAR: sb.append("String ").append(columnName); break; case Types.LONGVARCHAR: sb.append("String ").append(columnName); break; case Types.NVARCHAR: sb.append("String ").append(columnName); break; case Types.SMALLINT: sb.append("int ").append(columnName); break; case Types.TIMESTAMP: sb.append("Date ").append(columnName); break; case Types.TINYINT: sb.append("int ").append(columnName); break; case Types.VARCHAR: sb.append("String ").append(columnName); break; case Types.BLOB: sb.append("byte[] ").append(columnName); break; default: System.err.println("columnName = " + columnName + " : type = " + type); break; } sb.append(";\n"); return sb.toString(); } /** * @param tableName * @return */ private static File getCF(String tableName) { File classFile = new File(Entity2Store + File.separator + table2EntityName(tableName) + ".java"); try { FileWriter fw = new FileWriter(classFile); BufferedWriter bw = new BufferedWriter(fw); String cr = getCopyRight(); String packagePath = getPachagePath(); String importPath = getImportPath(); String clazzName = getClassName(tableName); bw.write(cr); bw.write(packagePath); bw.write(importPath); bw.write(clazzName); bw.flush(); bw.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } return classFile; } /** * @param tableName * @return */ private static String getClassName(String tableName) { String className = table2EntityName(tableName); className = "public class " + className + " implements Serializable{\n"; className += "\n\t" + "private static final long serialVersionUID =" + (tableName + (Math.random() * 90000000 + 10000000)).hashCode()+"l"; className += ";\n\n"; return className; } /** * @param tableName * @return */ private static String table2EntityName(String tableName) { tableName = column2fieldName(tableName); tableName = firstChar2Upper(tableName); return tableName; } /** * @param tableName * @return */ private static String firstChar2Upper(String tableName) { String firstChar = tableName.substring(0, 1); String entityName = tableName.replaceFirst(firstChar, firstChar .toUpperCase()); return entityName; } /** * @param tableName * @return */ private static String column2fieldName(String columnName) { columnName = columnName.toLowerCase(); while (columnName.contains("_")) { int i = columnName.indexOf('_'); String char2Upper = columnName.substring(i + 1, i + 2); columnName = columnName.replaceFirst("_" + char2Upper, char2Upper .toUpperCase()); } while (columnName.contains("-")) { int i = columnName.indexOf('-'); String char2Upper = columnName.substring(i + 1, i + 2); columnName = columnName.replaceFirst("-" + char2Upper, char2Upper .toUpperCase()); } return columnName; } /** * @return */ private static String getImportPath() { StringBuffer sb = new StringBuffer(); sb.append("import java.io.Serializable;\n"); sb.append("import java.util.Date;\n\n"); return sb.toString(); } /** * @return */ private static String getPachagePath() { return "package " + Entity2Store.replace("src/", "").replace('/', '.') + ";\n\n"; } /** * @return */ private static String getCopyRight() { StringBuffer cr = new StringBuffer("/**"); cr.append("\n"); cr.append("*/"); cr.append("\n"); cr.append("\n"); cr.append("\n"); return cr.toString(); } /** * @return */ private static List<String> getTables() { conn = getConn(); try { stat = conn.createStatement(); ResultSet rs = stat.executeQuery(tablesSql); while (rs.next()) { tables.add(rs.getString(1)); } } catch (SQLException e) { e.printStackTrace(); } return tables; } /** * @return */ private static Connection getConn() { if (null != conn) { return conn; } try { Class.forName(driver); conn = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } } // end
其实这小工具早就想放出来了,但是考虑下,netbean自带的有这个工具,eclipse好像也有(需要插件)
myeclipse更不用说了,必须有,不过要是不知道的人,用着也挺不方便,
为什么要写这个东西?
我们开发的时候用的是eclipse,尝试了安装插件来完成,但是没有成功,之前在上家公司的时候,boss用一个晚上搞出来的东西,自己也想试试,其实自从boss写出来之后一直有这个冲动,boss是用python写的,
这次做项目的机会,自己写了一个,
总共花费2天不到的时间吧,看来boss就是boss,不得不服
先说明一下,这个小工具并不是支持全部sql类型,有需要的可以自己扩充,
原来我想把这个工具优化一下,写成配置文件的方式,后来就一拖再拖,直到现在。
欢迎随便转发,使用,修改,不过请注明原文出出。
欢迎拍砖,
附加:
DB2查询所有表明的sql:select tabname from syscat.tables where tabschema ='DB2ADMIN' and tabname like '%TMP1'
-------------------------------------------------------------------------------------------------------------
今天更新个小版本,添加对Types.REAL类型的支持,
今天测试时,在mysql里面,mysql的数据类型是float,
所以代码里面也按照Types.REAL对应float来做的
同时实现了,包名和文件路径不同的设置,如果不单独设置包名,则默认和文件路径相同
package com.areashop; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * @author q821424508@sina.com * @version 1.1 * */ public class Table2Entity { private static String pakagePath="model"; private static String Entity2Store = "src/main/java/model";// 以项目名为根目录 //private static String tablesSql = "SELECT table_name FROM information_schema.tables WHERE table_schema ='common' AND NOT table_name LIKE 'sys%'"; private static String tablesSql = "SELECT table_name FROM information_schema.tables WHERE table_schema ='property' "; private static List<String> tables = new ArrayList<String>(); private static String url = "jdbc:mysql://127.0.0.1/property?useUnicode=true&characterEncoding=utf8"; private static String driver = "com.mysql.jdbc.Driver"; private static String username = "root"; private static String password = ""; private static String sql = "select * from "; private static String author = "q821424508"; private static Connection conn = null; private static Statement stat = null; public static void main(String[] args) { System.out.println("---begin---"); init(); table2Entity(); System.out.println("---end---"); } /** */ private static void init() { File f = new File(Entity2Store); if (!f.exists()) { f.mkdirs(); } } /** */ private static void table2Entity() { tables = getTables(); if (null != tables && tables.size() > 0) { for (String tableName : tables) { doTable2Entity(tableName); } } } /** */ private static void doTable2Entity(String tableName) { String sql1 = sql + tableName; System.out.println("tableName = " + tableName + "\n"); conn = getConn(); try { stat = conn.createStatement(); ResultSet rs = stat.executeQuery(sql1); ResultSetMetaData metaDate = rs.getMetaData(); int count = metaDate.getColumnCount(); File classFile = getCF(tableName); FileWriter fw = new FileWriter(classFile, true); BufferedWriter bw = new BufferedWriter(fw); StringBuffer sbFields = new StringBuffer(); //toString 方法 StringBuffer toString = new StringBuffer("\tpublic String toString(){\n"); toString.append("\t\tString str=\"{"); for (int j = 0; j < count; j++) { String columnName = metaDate.getColumnName(j + 1); int type = metaDate.getColumnType(j + 1); System.out.println(columnName + " : " + type); sbFields.append(column2field(columnName, type)); toString.append(""+column2fieldName(columnName)+":'\" + "+column2fieldName(columnName)+" + \"',\" + \""); } bw.write(sbFields.toString()); //toString.replace(toString.length()-1, toString.length(), ""); toString.replace(toString.length()-8, toString.length(), ""); toString.append("\"'}\";\n"); toString.append("\t\treturn str;\n\t}\n"); bw.write(toString.toString()); bw.write("\n}\n"); bw.close(); fw.close(); System.out.println("---------------------------------"); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @param columnName * @param type * @return */ private static String column2field(String columnName, int type) { String field = ""; String getseter = ""; String fieldName = column2fieldName(columnName); field = getField(fieldName, type); getseter = getGetseter(fieldName, type); return field + "\n" + getseter; } /** * @param columnName * @param type * @return */ private static String getGetseter(String columnName, int type) { String filedName = firstChar2Upper(columnName); StringBuffer sb = new StringBuffer(""); switch (type) { case Types.BIGINT: sb.append("\tpublic long get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(long "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.INTEGER: sb.append("\tpublic int get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(int "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.CHAR: sb.append("\tpublic String get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(String "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.DATE: sb.append("\tpublic Date get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(Date "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.DOUBLE: sb.append("\tpublic double get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(double "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.FLOAT: sb.append("\tpublic float get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(float "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.REAL: sb.append("\tpublic float get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(float "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.LONGNVARCHAR: sb.append("\tpublic String get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(String "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.LONGVARCHAR: sb.append("\tpublic String get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(String "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.NVARCHAR: sb.append("\tpublic String get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(String "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.SMALLINT: sb.append("\tpublic int get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(int "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.TIMESTAMP: sb.append("\tpublic Date get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(Date "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.TINYINT: sb.append("\tpublic int get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(int "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.NUMERIC: sb.append("\tpublic long get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(long "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.VARCHAR: sb.append("\tpublic String get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(String "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; case Types.BLOB: sb.append("\tpublic byte[] get"); sb.append(filedName); sb.append("(){\n"); sb.append("\t\treturn "); sb.append("this."); sb.append(columnName); sb.append(";\n"); sb.append("\t}\n"); sb.append("\tpublic void set"); sb.append(filedName); sb.append("(byte[] "); sb.append(columnName); sb.append("){\n"); sb.append("\t\t"); sb.append("this."); sb.append(columnName); sb.append("=" + columnName); sb.append(";\n"); sb.append("\t}\n\n"); break; default: break; } return sb.toString(); } /** * @param columnName * @param type * @return */ private static String getField(String columnName, int type) { StringBuffer sb = new StringBuffer("\tprivate "); switch (type) { case Types.BIGINT: sb.append("long ").append(columnName); break; case Types.INTEGER: sb.append("int ").append(columnName); break; case Types.NUMERIC: sb.append("long ").append(columnName); break; case Types.CHAR: sb.append("String ").append(columnName); break; case Types.DATE: sb.append("Date ").append(columnName); break; case Types.DOUBLE: sb.append("double ").append(columnName); break; case Types.FLOAT: sb.append("float ").append(columnName); break; case Types.REAL: sb.append("float ").append(columnName); break; case Types.LONGNVARCHAR: sb.append("String ").append(columnName); break; case Types.LONGVARCHAR: sb.append("String ").append(columnName); break; case Types.NVARCHAR: sb.append("String ").append(columnName); break; case Types.SMALLINT: sb.append("int ").append(columnName); break; case Types.TIMESTAMP: sb.append("Date ").append(columnName); break; case Types.TINYINT: sb.append("int ").append(columnName); break; case Types.VARCHAR: sb.append("String ").append(columnName); break; case Types.BLOB: sb.append("byte[] ").append(columnName); break; default: System.err.println("columnName = " + columnName + " : type = " + type); break; } sb.append(";\n"); return sb.toString(); } /** * @param tableName * @return */ private static File getCF(String tableName) { File classFile = new File(Entity2Store + File.separator + table2EntityName(tableName) + ".java"); try { FileWriter fw = new FileWriter(classFile); BufferedWriter bw = new BufferedWriter(fw); String cr = getCopyRight(); String packagePath = getPachagePath(); String importPath = getImportPath(); String clazzName = getClassName(tableName); bw.write(cr); bw.write(packagePath); bw.write(importPath); bw.write(clazzName); bw.flush(); bw.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } return classFile; } /** * @param tableName * @return */ private static String getClassName(String tableName) { String className = table2EntityName(tableName); className = "public class " + className + " implements Serializable{\n"; className += "\n\t" + "private static final long serialVersionUID =" + (tableName + (Math.random() * 90000000 + 10000000)).hashCode()+"l"; className += ";\n\n"; return className; } /** * @param tableName * @return */ private static String table2EntityName(String tableName) { tableName = column2fieldName(tableName); tableName = firstChar2Upper(tableName); return tableName; } /** * @param tableName * @return */ private static String firstChar2Upper(String tableName) { String firstChar = tableName.substring(0, 1); String entityName = tableName.replaceFirst(firstChar, firstChar .toUpperCase()); return entityName; } /** * @param tableName * @return */ private static String column2fieldName(String columnName) { columnName = columnName.toLowerCase(); while (columnName.contains("_")) { int i = columnName.indexOf('_'); String char2Upper = columnName.substring(i + 1, i + 2); columnName = columnName.replaceFirst("_" + char2Upper, char2Upper .toUpperCase()); } while (columnName.contains("-")) { int i = columnName.indexOf('-'); String char2Upper = columnName.substring(i + 1, i + 2); columnName = columnName.replaceFirst("-" + char2Upper, char2Upper .toUpperCase()); } return columnName; } /** * @return */ private static String getImportPath() { StringBuffer sb = new StringBuffer(); sb.append("import java.io.Serializable;\n"); sb.append("import java.util.Date;\n\n"); return sb.toString(); } /** * @return */ private static String getPachagePath() { if(null!=pakagePath){ return "package " +pakagePath.replace('/', '.')+ ";\n\n"; } return "package " + Entity2Store.replace("src/", "").replace('/', '.') + ";\n\n"; } /** * @return */ private static String getCopyRight() { StringBuffer cr = new StringBuffer("/**"); cr.append("\n"); cr.append("*/"); cr.append("\n"); cr.append("\n"); cr.append("\n"); return cr.toString(); } /** * @return */ private static List<String> getTables() { conn = getConn(); try { stat = conn.createStatement(); ResultSet rs = stat.executeQuery(tablesSql); while (rs.next()) { tables.add(rs.getString(1)); } } catch (SQLException e) { e.printStackTrace(); } return tables; } /** * @return */ private static Connection getConn() { if (null != conn) { return conn; } try { Class.forName(driver); conn = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } } // end
相关推荐
总的来说,"逆向生成工具,有数据库表生成java bean实体类"是一种提高开发效率、降低维护成本的利器。通过合理的XML配置和简单的执行命令,我们可以快速地从数据库中获取到对应的Java Bean实体类,使开发过程更加...
"根据数据库表自动生成Java代码"是一个非常实用的功能,它允许开发者基于现有的数据库表结构快速生成对应的Java类,如DAO(Data Access Object)、XML配置文件以及实体类(Entity)。这样,开发者可以避免手动编写...
"mybatis根据数据库表自动生成SQL、实体类、mapper文件工具"就是这样一个解决方案,它基于Eclipse集成开发环境,特别适用于使用SSM(Spring、SpringMVC、MyBatis)框架的项目。 首先,MyBatis是一个优秀的持久层...
具体来说,"数据库文档生成工具.zip"支持一键生成包括表结构、字段定义、键关系、索引和注释等关键信息的数据库设计内容。尤其对于使用MySQL等关系型数据库的开发者来说,这一点尤为重要。这些文档信息不仅详尽记录...
在给定的标题"自动读取mysql数据库字段并自动生成java属性和set和get方法"中,我们可以理解这是一个自动化过程,它减少了开发者手动编写Java代码的工作量。该过程通常包括以下几个步骤: 1. **连接数据库**:首先,...
举例来说,Apache MyBatis的MyBatis Generator就是一个广泛应用的Java代码生成工具,它可以自动生成MyBatis的Mapper接口、XML配置文件和实体类。另外,还有一些IDEA、Eclipse等集成开发环境内的插件,如JPA Code ...
4. **执行生成**:运行像`WindowsFormsApplication1.exe`这样的生成工具,提供配置文件和数据库连接信息,工具会自动扫描数据库表,根据配置和模板生成对应的Java Bean类。 5. **代码整合**:最后,将生成的Java源...
开发者可能需要输入数据库连接信息,选择要操作的表,然后工具会自动生成对应的Java源代码。 4. **使用教程**: - 提供使用教程是工具的重要组成部分,它帮助初学者或不熟悉工具的开发者快速上手。教程通常包含...
在这个场景中,"JAVA通过实体生成数据库表"指的是使用特定的工具或框架,根据预先定义好的Java实体类,自动生成对应的数据库表结构。 首先,我们需要理解什么是实体类。在Java中,实体类通常是对应数据库中的一张表...
总的来说,自动将MySQL数据表生成Java实体类是提高开发效率的有效手段。通过理解数据库表结构并将其转化为符合Java语法规则的类,我们可以更快速地构建数据驱动的应用程序,同时减少了出错的可能性。这个过程涉及到...
3. 字段映射:根据数据库表的字段信息,自动生成Java Bean的属性,包括数据类型和注释。 4. 注解支持:生成的Java Bean类可能包含如`@Entity`(JPA)、`@Table`、`@Column`等注解,方便集成ORM框架,如Hibernate或...
TableGo可以根据数据库表结构自动生成JavaBean、MyBaits的Mapper映射配置文件、数据库设计文档,更重要的是可以根据每个项目的不同开发框架编写自定义模板生成各模块的结构性代码,让开发人员的开发效率提高60%以上...
在IT行业中,数据库管理和开发是不可或缺...总的来说,"java代码生成器数据库表一键生成类文件"是Java开发中的利器,它能够帮助开发者快速搭建数据层和业务层的基础架构,使他们能够更专注于提供高质量的业务解决方案。
这些框架提供了元数据映射功能,可以根据数据库表自动生成Java实体类,而这些实体类可以视作VO类的变种。 1. Hibernate: Hibernate 是一个强大的ORM框架,它允许开发者将Java对象映射到数据库表。通过配置...
标题中的"mybatis自动生成Java实体类和映射文件的自动生成类工具"指的就是这样的一个辅助开发工具,它可以分析数据库表结构,并根据表信息自动生成对应的Java实体类和Mapper接口及其XML配置文件。 在Java开发中,...
总结来说,"Oracle根据数据库自动生成JAVA代码"是一个集成开发环境(IDE)或代码生成工具的功能,它利用元数据信息自动生成Java项目的基本架构,包括实体类、DAO、Service、Controller以及前端HTML模板。这种自动化...
标题提到的“根据数据库生成实体类的一个工具”是一种自编写的程序,旨在自动化这个过程。开发者通常会为每个数据库表创建一个对应的JavaBean类,手动编写字段、getter和setter方法以及相关的注解。这种工具可以显著...
2. **执行Generator**:配置好后,可以通过Java API或者命令行来运行Generator,它会根据配置文件中的设置生成相应的Java文件。 3. **生成的文件**:生成的文件包括Java实体类(PO),这个类包含了数据库表中的所有...
总的来说,“Java自动生成Mysql数据库结构”是一项实用的开发工具,它结合了Java编程、数据库管理和文档自动化,体现了IT行业的效率提升理念。通过持续优化,它可以成为数据库管理员和开发者的得力助手,使得数据库...
4. `代码生成工具.vshost.exe` 和 `代码生成工具.vshost.exe.manifest`:这两个文件与Visual Studio的调试环境相关,帮助在开发过程中提供服务,如自动重启动和内存泄漏检测。 5. `代码生成工具.pdb`:这是程序的...