java--生成实体类方法

由于工作中使用eclipse开发,没有安装hibernate插件,所以无法使用自动生成实体类的功能,为了方便在网上找了段别人写的自动生成实体类方法,稍加修改,直接运行就可以生成对应表的实体类。

    注意使用时方法中的数据库地址、用户名、密码要改下,还有要记得录入想要生成实体类的表明,private String tablename = "ad_campaign";

 

方法如下:

[java]  view plain  copy
  1. package com.test;  
  2.   
  3. import java.io.FileWriter;  
  4. import java.io.IOException;  
  5. import java.io.PrintWriter;  
  6. import java.sql.Connection;  
  7. import java.sql.DriverManager;  
  8. import java.sql.PreparedStatement;  
  9. import java.sql.ResultSetMetaData;  
  10. import java.sql.SQLException;  
  11.   
  12. public class CreateBean {  
  13.     private String tablename = "ad_campaign";  
  14.   
  15.     private String[] colnames; // 列名数组  
  16.   
  17.     private String[] colTypes; // 列名类型数组  
  18.   
  19.     private int[] colSizes; // 列名大小数组  
  20.   
  21.     private boolean f_util = false// 是否需要导入包java.util.*  
  22.   
  23.     private boolean f_sql = false// 是否需要导入包java.sql.*  
  24.       
  25.   
  26.     private String url = "jdbc:mysql://localhost:3306/haierdb";  
  27.     private String user = "permitdbuser";  
  28.     private String pwd = "permitdbpwd";  
  29.   
  30.     public Connection getConnection() {  
  31.         Connection conn = null;  
  32.         try {  
  33.             Class.forName("com.mysql.jdbc.Driver");  
  34.             conn = DriverManager.getConnection(url, user, pwd);  
  35.         } catch (ClassNotFoundException e) {  
  36.             e.printStackTrace();  
  37.         } catch (SQLException e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.         return conn;  
  41.     }  
  42.   
  43.     public void createBeanMethod() {  
  44.         Connection conn = getConnection(); // 得到数据库连接  
  45.         //myDB为数据库名  
  46.         String strsql = "select * from " + tablename;  
  47.         PreparedStatement pstmt = null;  
  48.         ResultSetMetaData rsmd = null;  
  49.         try {  
  50.             pstmt = conn.prepareStatement(strsql);  
  51.             rsmd = pstmt.getMetaData();  
  52.             int size = rsmd.getColumnCount(); // 共有多少列  
  53.             colnames = new String[size];  
  54.             colTypes = new String[size];  
  55.             colSizes = new int[size];  
  56.             for (int i = 0; i < rsmd.getColumnCount(); i++) {  
  57.                 colnames[i] = rsmd.getColumnName(i + 1);  
  58.                 colTypes[i] = rsmd.getColumnTypeName(i + 1);  
  59.                 if (colTypes[i].equalsIgnoreCase("datetime")) {  
  60.                     f_util = true;  
  61.                 }  
  62.                 if (colTypes[i].equalsIgnoreCase("image")  
  63.                         || colTypes[i].equalsIgnoreCase("text")) {  
  64.                     f_sql = true;  
  65.                 }  
  66.                 colSizes[i] = rsmd.getColumnDisplaySize(i + 1);  
  67.             }  
  68.             String content = parse(colnames, colTypes, colSizes);  
  69.             try {  
  70.                 FileWriter fw = new FileWriter(initcap(tablename) + ".java");  
  71.                 PrintWriter pw = new PrintWriter(fw);  
  72.                 pw.println(content);  
  73.                 pw.flush();  
  74.                 pw.close();  
  75.             } catch (IOException e) {  
  76.                 e.printStackTrace();  
  77.             }  
  78.         } catch (SQLException e) {  
  79.             e.printStackTrace();  
  80.         } finally {  
  81.             try {  
  82.                 pstmt.close();  
  83.                 conn.close();  
  84.             } catch (SQLException e) {  
  85.                 e.printStackTrace();  
  86.             }  
  87.         }  
  88.     }  
  89.   
  90.     /** 
  91.      * 解析处理(生成实体类主体代码) 
  92.      */  
  93.     private String parse(String[] colNames, String[] colTypes, int[] colSizes) {  
  94.         StringBuffer sb = new StringBuffer();  
  95.         if (f_util) {  
  96.             sb.append("import java.util.Date;\r\n");  
  97.         }  
  98.         if (f_sql) {  
  99.             sb.append("import java.sql.*;\r\n\r\n\r\n");  
  100.         }  
  101.         sb.append("public class " + initcap(tablename) + " {\r\n");  
  102.         processAllAttrs(sb);  
  103.         processAllMethod(sb);  
  104.         sb.append("}\r\n");  
  105.         System.out.println(sb.toString());  
  106.         return sb.toString();  
  107.   
  108.     }  
  109.   
  110.     /** 
  111.      * 生成所有的方法 
  112.      *  
  113.      * @param sb 
  114.      */  
  115.     private void processAllMethod(StringBuffer sb) {  
  116.         for (int i = 0; i < colnames.length; i++) {  
  117.             sb.append("\tpublic void set" + initcap(colnames[i]) + "("  
  118.                     + sqlType2JavaType(colTypes[i]) + " " + colnames[i]  
  119.                     + "){\r\n");  
  120.             sb.append("\t\tthis." + colnames[i] + "=" + colnames[i] + ";\r\n");  
  121.             sb.append("\t}\r\n");  
  122.   
  123.             sb.append("\tpublic " + sqlType2JavaType(colTypes[i]) + " get"  
  124.                     + initcap(colnames[i]) + "(){\r\n");  
  125.             sb.append("\t\treturn " + colnames[i] + ";\r\n");  
  126.             sb.append("\t}\r\n");  
  127.         }  
  128.     }  
  129.   
  130.     /** 
  131.      * 解析输出属性 
  132.      *  
  133.      * @return 
  134.      */  
  135.     private void processAllAttrs(StringBuffer sb) {  
  136.         for (int i = 0; i < colnames.length; i++) {  
  137.             sb.append("\tprivate " + sqlType2JavaType(colTypes[i]) + " "  
  138.                     + colnames[i] + ";\r\n");  
  139.   
  140.         }  
  141.     }  
  142.   
  143.     /** 
  144.      * 把输入字符串的首字母改成大写 
  145.      *  
  146.      * @param str 
  147.      * @return 
  148.      */  
  149.     private String initcap(String str) {  
  150.         char[] ch = str.toCharArray();  
  151.         if (ch[0] >= 'a' && ch[0] <= 'z') {  
  152.             ch[0] = (char) (ch[0] - 32);  
  153.         }  
  154.         return new String(ch);  
  155.     }  
  156.   
  157.     private String sqlType2JavaType(String sqlType) {  
  158.         if (sqlType.equalsIgnoreCase("bit")) {  
  159.             return "bool";  
  160.         } else if (sqlType.equalsIgnoreCase("tinyint")) {  
  161.             return "byte";  
  162.         } else if (sqlType.equalsIgnoreCase("smallint")) {  
  163.             return "short";  
  164.         } else if (sqlType.equalsIgnoreCase("int")) {  
  165.             return "int";  
  166.         } else if (sqlType.equalsIgnoreCase("bigint")) {  
  167.             return "long";  
  168.         } else if (sqlType.equalsIgnoreCase("float")) {  
  169.             return "float";  
  170.         } else if (sqlType.equalsIgnoreCase("decimal")  
  171.                 || sqlType.equalsIgnoreCase("numeric")  
  172.                 || sqlType.equalsIgnoreCase("real")) {  
  173.             return "double";  
  174.         } else if (sqlType.equalsIgnoreCase("money")  
  175.                 || sqlType.equalsIgnoreCase("smallmoney")) {  
  176.             return "double";  
  177.         } else if (sqlType.equalsIgnoreCase("varchar")  
  178.                 || sqlType.equalsIgnoreCase("char")  
  179.                 || sqlType.equalsIgnoreCase("nvarchar")  
  180.                 || sqlType.equalsIgnoreCase("nchar")) {  
  181.             return "String";  
  182.         } else if (sqlType.equalsIgnoreCase("datetime")) {  
  183.             return "Date";  
  184.         }  
  185.   
  186.         else if (sqlType.equalsIgnoreCase("image")) {  
  187.             return "Blob";  
  188.         } else if (sqlType.equalsIgnoreCase("text")) {  
  189.             return "Clob";  
  190.         }  
  191.         return null;  
  192.     }  
  193.   
  194.     public static void main(String[] args) {  
  195.         CreateBean createBean = new CreateBean();  
  196.         createBean.createBeanMethod();  
  197.     }  
  198. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值