博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过数据库表反向生成pojo类
阅读量:5050 次
发布时间:2019-06-12

本文共 4469 字,大约阅读时间需要 14 分钟。

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);
}
}

转载于:https://www.cnblogs.com/pyrmkj/archive/2012/05/17/2505687.html

你可能感兴趣的文章
js/jQuery实现类似百度搜索功能
查看>>
【转】排序算法复习(Java实现)(一): 插入,冒泡,选择,Shell,快速排序
查看>>
myeclipse中修改servlet的模板代码
查看>>
js 倒计时10s
查看>>
再次陷入迷惘期的一点感想
查看>>
字符串的常用方法
查看>>
SQL查询语句 常用示例
查看>>
抄写例题作业1
查看>>
Nginx geo模块
查看>>
mingw using pthread
查看>>
centos6.4搭建基于ftp的yum源让本地局域网服务器使用
查看>>
php GD 和图像处理函数, 制作一张图片
查看>>
设计模式第二篇-观察者模式
查看>>
把一元可以分解成几个1毛,2毛,5毛?
查看>>
windows系统安装gcc编译器----c/c++语言编译器
查看>>
12、Oracle中的dual数据表
查看>>
python网络编程-OSI七层模型详解
查看>>
scala 模式匹配详解 2 scala里是怎么实现的?
查看>>
音视频处理中的硬压缩与软压缩
查看>>
STL-map
查看>>