• 通过数据库表反向生成pojo类


    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import com.onedear.util.database.DataBaseType;
    /**
    * 数据库工具类
    *
    * @author onedear
    * @data:2010-10-21 下午06:12:39
    */
    public class DBUtil {

    public static final int SQLSERVER = 1;
    public static final int MYSQL = 2;
    public static final int ORACLE = 3;
    private static final String LINE = "\r\n";
    private static final String TAB = "\t";

    /**
    * 用于调试,懒得写相应的参数
    *
    * @return
    * @throws SQLException
    * @throws ClassNotFoundException
    */
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
    return getConnection("192.168.0.161:1997", "ETForMonitor_V2", "sa",
    "password", DBUtil.SQLSERVER);
    }
    /**
    *
    * 通过jdbc获取相应的数据库链接connection
    *
    * @param ipport
    * ip+port ,eg.: 192.168.0.161:1997
    * @param dbName
    * databaseName ,eg. : ETForMonitor_V2
    * @param username
    * eg.:sa
    * @param password
    * eg. :password
    * @param type
    * 请看本类的静态变量
    * @return
    * @throws ClassNotFoundException
    * @throws SQLException
    */
    public static Connection getConnection(String ipport, String dbName,
    String username, String password, int type)
    throws ClassNotFoundException, SQLException {
    String jdbcString = null;
    if (type == SQLSERVER) {
    jdbcString = "jdbc:jtds:sqlserver://" + ipport + ";databaseName="
    + dbName;
    Class.forName("net.sourceforge.jtds.jdbc.Driver");
    } else if (type == MYSQL) {
    jdbcString = "jdbc:mysql://" + ipport + "/" + dbName;
    Class.forName("org.gjt.mm.mysql.Driver");
    } else if (type == ORACLE) {
    jdbcString = "jdbc:oracle:thin:@" + ipport + ":" + dbName;
    Class.forName("oracle.jdbc.driver.OracleDriver");
    }
    Connection connection = null;
    connection = DriverManager
    .getConnection(jdbcString, username, password);
    return connection;
    }
    /**
    * 数据库表生成相应的java类,生成规则
    * 类名= 表名(第一个字母大写)
    * 属性名= 数据库列名
    * get/set方法 = 根据标准生成
    * 其中生成的基本类型均为包装类,例如Integer , Long , Boolean , String
    * @param connection
    * @param tableName
    * @param dbType
    * @param path
    * @param isCreateFile
    * @return
    * @throws SQLException
    */
    public static String table2pojo(Connection connection, String tableName,
    int dbType, String path , boolean isCreateFile) throws SQLException {
    String sql = "select * from " + tableName + " where 1 <> 1";
    PreparedStatement ps = null;
    ResultSet rs = null;
    ps = connection.prepareStatement(sql);
    rs = ps.executeQuery();
    ResultSetMetaData md = rs.getMetaData();
    int columnCount = md.getColumnCount();
    StringBuffer sb = new StringBuffer();
    tableName = tableName.substring(0, 1).toUpperCase() +tableName.subSequence(1, tableName.length());
    sb.append("public class " + tableName + " {");
    sb.append(LINE);
    for (int i = 1; i <= columnCount; i++) {
    sb.append(TAB);
    sb.append("private "
    + DataBaseType.getPojoType(md.getColumnTypeName(i)) + " "
    + md.getColumnName(i) + ";");
    // System.out.println("name : " + md.getColumnName(i) +
    // " , type :"
    // + md.getColumnTypeName(i));
    sb.append(LINE);
    }
    for (int i = 1; i <= columnCount; i++) {
    sb.append(TAB);
    String pojoType = DataBaseType.getPojoType(md.getColumnTypeName(i));
    String columnName = md.getColumnName(i);
    String getName = null;
    String setName = null;
    if (columnName.length() > 1) {
    getName = "public "+pojoType+" get" + columnName.substring(0, 1).toUpperCase()
    + columnName.substring(1, columnName.length()) + "() {";
    setName = "public void set" + columnName.substring(0, 1).toUpperCase()
    + columnName.substring(1, columnName.length()) + "("
    + pojoType + " " + columnName + ") {";
    } else {
    getName = "public get" + columnName.toUpperCase() + "() {";
    setName = "public set" + columnName.toUpperCase() + "(" + pojoType
    + " " + columnName + ") {";
    }
    sb.append(LINE).append(TAB).append(getName);
    sb.append(LINE).append(TAB).append(TAB);
    sb.append("return " + columnName +";");
    sb.append(LINE).append(TAB).append("}");
    sb.append(LINE);
    sb.append(LINE).append(TAB).append(setName);
    sb.append(LINE).append(TAB).append(TAB);
    sb.append("this." + columnName + " = " + columnName +";" );
    sb.append(LINE).append(TAB).append("}");
    sb.append(LINE);
    }
    sb.append("}");
    System.out.println(sb.toString());
    if(isCreateFile)
    FileUtils.stringToFile(null,tableName +".java" , sb.toString());
    return null;
    }
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
    Connection con = getConnection();
    table2pojo(con, "PlayBackVisitorControl", DBUtil.SQLSERVER, "" , true);
    }
    }

  • 相关阅读:
    基于Redis的短链接设计思路
    再谈对协变和逆变的理解(Updated)
    Java基础—ClassLoader的理解
    遇到个小问题,Java泛型真的是鸡肋吗?
    一次失败升级后的反思
    JVM是如何分配和回收内存?有实例!
    一个Java对象到底占用多大内存?
    《深入理解Java虚拟机》读书笔记:垃圾收集器与内存分配策略
    快速掌握RabbitMQ(二)——四种Exchange介绍及代码演示
    快速掌握RabbitMQ(一)——RabbitMQ的基本概念、安装和C#驱动
  • 原文地址:https://www.cnblogs.com/pyrmkj/p/2505687.html
Copyright © 2020-2023  润新知