2023年7月13日发(作者:)
利⽤JAVA反射,读取数据库表名,⾃动⽣成对应实体类本代码是利⽤java反射,读取数据库表⾃动根据表名⽣成实体类,数据库采⽤⽼牌SQLSERVER 2000,驱动为JTDS,其他数据库可根据情况⾃定修改。代码中包含了⼤部分数据库类型与JAVA类型的转换,少数未包含进去的会在⽣成代码时打印出来,⽅⾯后期查找修改。import ;import iter;import ption;import riter;import tion;import Manager;import edStatement;import SetMetaData;import eption;import DateFormat;import ;/** * 从数据库表反射出实体类,⾃动⽣成实体类 *
* @author Zero * */public class SqlHelper { //基本数据配置 private String packageOutPath = "";// 指定实体⽣成所在包的路径 private String authorName = "Zero";// 作者名字 private String tablename = "xxx";// 表名 private String[] colnames; // 列名数组 private String[] colTypes; // 列名类型数组 private String version = "V0.01"; // 版本 private int[] colSizes; // 列名⼤⼩数组 private boolean f_util = false; // 是否需要导⼊包.* private boolean f_sql = false; // 是否需要导⼊包.* private boolean f_lang = false; // 是否需要导⼊包.* private String defaultPath = "/src/main/java/"; // 数据库连接 private static final String URL = "jdbc:jtds:sqlserver://192.168.29.128:1433/xxx"; private static final String NAME = "sa"; private static final String PASS = "xxx"; private static final String DRIVER = ""; /* * 构造函数 */ public SqlHelper() { // 创建连接 Connection con; // 查要⽣成实体类的表 String sql = "select * from " + tablename; PreparedStatement pStemt = null; try { try { e(DRIVER); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block tackTrace(); } con = nection(URL, NAME, PASS); pStemt = eStatement(sql); pStemt = eStatement(sql); ResultSetMetaData rsmd = aData(); int size = umnCount(); // 统计列 colnames = new String[size]; colTypes = new String[size]; colSizes = new int[size]; for (int i = 0; i < size; i++) { colnames[i] = umnName(i + 1); colTypes[i] = umnTypeName(i + 1); //⾃动⽣成包配置
// if (colTypes[i].equalsIgnoreCase("datetime")) { // f_util = true; // } if (colTypes[i].equalsIgnoreCase("image") || colTypes[i].equalsIgnoreCase("text") || colTypes[i].equalsIgnoreCase("datetime") || colTypes[i].equalsIgnoreCase("time") || colTypes[i].equalsIgnoreCase("date") || colTypes[i].equalsIgnoreCase("datetime2")) { f_sql = true; } // if (colTypes[i].equalsIgnoreCase("int")) { // f_lang = true; // } colSizes[i] = umnDisplaySize(i + 1); } String content = parse(colnames, colTypes, colSizes); try { File directory = new File("");
String path = ss().getResource("").getPath(); n(path);
String outputPath = olutePath() + tPath + e(".", "/") + "/" + initcap(tablename) + ".java"; n("执⾏完毕,⽣成路径为:"+outputPath); FileWriter fw = new FileWriter(outputPath); PrintWriter pw = new PrintWriter(fw); n(content); (); (); } catch (IOException e) { tackTrace(); } } catch (SQLException e) { tackTrace(); } finally {
} } /** * 功能:⽣成实体类主体代码 *
* @param colnames * @param colTypes * @param colSizes * @return */ private String parse(String[] colnames, String[] colTypes, int[] colSizes) { StringBuffer sb = new StringBuffer(); // ⽣成package包路径 ("package " + eOutPath + ";rn"); // 判断是否导⼊⼯具包 if (f_util) { ("import ;rn"); } if (f_sql) { ("import .*;rn"); } if (f_lang) { ("import .*;rn"); } ("rn"); // 注释部分 (" /**rn"); (" * @⽂件名称:" + ame + ".javarn"); (" * @创建时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "rn"); (" * @创 建 ⼈:" + Name + " rn"); (" * @⽂件描述:" + tablename + " 实体类rn"); (" * @⽂件版本:" + n + " rn"); (" */ rn"); // 实体部分 ("rnrnpublic class " + initcap(tablename) + "{rn"); processAllAttrs(sb);// 属性 processAllMethod(sb);// get set⽅法 ("}rn"); // n(ng()); return ng(); } /** * 功能:⽣成所有属性 *
* @param sb */ private void processAllAttrs(StringBuffer sb) { for (int i = 0; i < ; i++) { ("tprivate " + sqlType2JavaType(colTypes[i]) + " " + colnames[i] + ";rn"); } } /** * 功能:⽣成所有⽅法 *
* @param sb */ private void processAllMethod(StringBuffer sb) { for (int i = 0; i < ; i++) { ("tpublic void set" + initcap(colnames[i]) + "(" + sqlType2JavaType(colTypes[i]) + " " + colnames[i] + "){rn"); ("tthis." + colnames[i] + "=" + colnames[i] + ";rn"); ("t}rn"); ("tpublic " + sqlType2JavaType(colTypes[i]) + " get" + initcap(colnames[i]) + "(){rn"); ("ttreturn " + colnames[i] + ";rn"); ("t}rn"); } } /** * 功能:将输⼊字符串的⾸字母改成⼤写 *
* @param str * @return */ private String initcap(String str) { char[] ch = Array(); if (ch[0] >= 'a' && ch[0] <= 'z') { ch[0] = (char) (ch[0] - 32); } return new String(ch); } /** * 功能:获得列的数据类型 *
* @param sqlType * @return */ private String sqlType2JavaType(String sqlType) { if (IgnoreCase("bit")) { return "Boolean"; } else if (IgnoreCase("decimal") || IgnoreCase("money") || IgnoreCase("smallmoney") || IgnoreCase("numeric") || IgnoreCase("bigint")) { return "Long"; } else if (IgnoreCase("float")) { return "Double"; } else if (IgnoreCase("int") || IgnoreCase("int identity")) { return "Integer"; } else if (IgnoreCase("image") || IgnoreCase("varbinary(max)") || IgnoreCase("varbinary") || IgnoreCase("udt") || IgnoreCase("timestamp") || IgnoreCase("binary")) { return "Byte[]"; } else if (IgnoreCase("nchar") || IgnoreCase("nvarchar(max)") || IgnoreCase("nvarchar") || IgnoreCase("nvarchar(ntext)") || IgnoreCase("uniqueidentifier") || IgnoreCase("xml") || IgnoreCase("char") || IgnoreCase("varchar(max)") || IgnoreCase("text") || IgnoreCase("varchar")) { return "String"; } else if (IgnoreCase("real")) { return "Float"; } else if (IgnoreCase("smallint") || IgnoreCase("tinyint")) { return "Short"; } else if (IgnoreCase("date") || IgnoreCase("datetime") || IgnoreCase("time") || IgnoreCase("datetime2")) { return "Date"; } else { n("数据类型异常,类型为:" + sqlType); } return null; } /** * 出⼝ TODO *
* @param args */ public static void main(String[] args) { new SqlHelper(); }}
发布者:admin,转转请注明出处:http://www.yc00.com/news/1689250355a225774.html
评论列表(0条)