由于工作中使用eclipse开发,没有安装hibernate插件,所以无法使用自动生成实体类的功能,为了方便在网上找了段别人写的自动生成实体类方法,稍加修改,直接运行就可以生成对应表的实体类。
注意使用时方法中的数据库地址、用户名、密码要改下,还有要记得录入想要生成实体类的表明,private String tablename = "ad_campaign";
方法如下:
- package com.test;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- public class CreateBean {
- private String tablename = "ad_campaign";
- private String[] colnames; // 列名数组
- private String[] colTypes; // 列名类型数组
- private int[] colSizes; // 列名大小数组
- private boolean f_util = false; // 是否需要导入包java.util.*
- private boolean f_sql = false; // 是否需要导入包java.sql.*
- private String url = "jdbc:mysql://localhost:3306/haierdb";
- private String user = "permitdbuser";
- private String pwd = "permitdbpwd";
- public Connection getConnection() {
- Connection conn = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(url, user, pwd);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
- public void createBeanMethod() {
- Connection conn = getConnection(); // 得到数据库连接
- //myDB为数据库名
- String strsql = "select * from " + tablename;
- PreparedStatement pstmt = null;
- ResultSetMetaData rsmd = null;
- try {
- pstmt = conn.prepareStatement(strsql);
- rsmd = pstmt.getMetaData();
- int size = rsmd.getColumnCount(); // 共有多少列
- colnames = new String[size];
- colTypes = new String[size];
- colSizes = new int[size];
- for (int i = 0; i < rsmd.getColumnCount(); i++) {
- colnames[i] = rsmd.getColumnName(i + 1);
- colTypes[i] = rsmd.getColumnTypeName(i + 1);
- if (colTypes[i].equalsIgnoreCase("datetime")) {
- f_util = true;
- }
- if (colTypes[i].equalsIgnoreCase("image")
- || colTypes[i].equalsIgnoreCase("text")) {
- f_sql = true;
- }
- colSizes[i] = rsmd.getColumnDisplaySize(i + 1);
- }
- String content = parse(colnames, colTypes, colSizes);
- try {
- FileWriter fw = new FileWriter(initcap(tablename) + ".java");
- PrintWriter pw = new PrintWriter(fw);
- pw.println(content);
- pw.flush();
- pw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- pstmt.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 解析处理(生成实体类主体代码)
- */
- private String parse(String[] colNames, String[] colTypes, int[] colSizes) {
- StringBuffer sb = new StringBuffer();
- if (f_util) {
- sb.append("import java.util.Date;\r\n");
- }
- if (f_sql) {
- sb.append("import java.sql.*;\r\n\r\n\r\n");
- }
- sb.append("public class " + initcap(tablename) + " {\r\n");
- processAllAttrs(sb);
- processAllMethod(sb);
- sb.append("}\r\n");
- System.out.println(sb.toString());
- return sb.toString();
- }
- /**
- * 生成所有的方法
- *
- * @param sb
- */
- private void processAllMethod(StringBuffer sb) {
- for (int i = 0; i < colnames.length; i++) {
- sb.append("\tpublic void set" + initcap(colnames[i]) + "("
- + sqlType2JavaType(colTypes[i]) + " " + colnames[i]
- + "){\r\n");
- sb.append("\t\tthis." + colnames[i] + "=" + colnames[i] + ";\r\n");
- sb.append("\t}\r\n");
- sb.append("\tpublic " + sqlType2JavaType(colTypes[i]) + " get"
- + initcap(colnames[i]) + "(){\r\n");
- sb.append("\t\treturn " + colnames[i] + ";\r\n");
- sb.append("\t}\r\n");
- }
- }
- /**
- * 解析输出属性
- *
- * @return
- */
- private void processAllAttrs(StringBuffer sb) {
- for (int i = 0; i < colnames.length; i++) {
- sb.append("\tprivate " + sqlType2JavaType(colTypes[i]) + " "
- + colnames[i] + ";\r\n");
- }
- }
- /**
- * 把输入字符串的首字母改成大写
- *
- * @param str
- * @return
- */
- private String initcap(String str) {
- char[] ch = str.toCharArray();
- if (ch[0] >= 'a' && ch[0] <= 'z') {
- ch[0] = (char) (ch[0] - 32);
- }
- return new String(ch);
- }
- private String sqlType2JavaType(String sqlType) {
- if (sqlType.equalsIgnoreCase("bit")) {
- return "bool";
- } else if (sqlType.equalsIgnoreCase("tinyint")) {
- return "byte";
- } else if (sqlType.equalsIgnoreCase("smallint")) {
- return "short";
- } else if (sqlType.equalsIgnoreCase("int")) {
- return "int";
- } else if (sqlType.equalsIgnoreCase("bigint")) {
- return "long";
- } else if (sqlType.equalsIgnoreCase("float")) {
- return "float";
- } else if (sqlType.equalsIgnoreCase("decimal")
- || sqlType.equalsIgnoreCase("numeric")
- || sqlType.equalsIgnoreCase("real")) {
- return "double";
- } else if (sqlType.equalsIgnoreCase("money")
- || sqlType.equalsIgnoreCase("smallmoney")) {
- return "double";
- } else if (sqlType.equalsIgnoreCase("varchar")
- || sqlType.equalsIgnoreCase("char")
- || sqlType.equalsIgnoreCase("nvarchar")
- || sqlType.equalsIgnoreCase("nchar")) {
- return "String";
- } else if (sqlType.equalsIgnoreCase("datetime")) {
- return "Date";
- }
- else if (sqlType.equalsIgnoreCase("image")) {
- return "Blob";
- } else if (sqlType.equalsIgnoreCase("text")) {
- return "Clob";
- }
- return null;
- }
- public static void main(String[] args) {
- CreateBean createBean = new CreateBean();
- createBean.createBeanMethod();
- }
- }