import java.io.FileWriter;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/*******************************************************************************
*
* <b>创建日期: </b> 2012-01-05<br>
* <b>标题: </b> java Hibernate工具<br>
* <b>类描述: </b>Hibernate映射工具,能获得xml,java<br>
* <br>
* �?��
* <p>
* Copyright: Copyright (c)2012
* </p>
* <p>
* Company:
* </p>
*
* @author zhouxj
*
* @version 1.00
*
* @since 2012-02-21
*
* @see
******************************************************************************/
public class HibernateTools {
public static void main(String[] args) {
try {
//genOneFile("T_CUS_SERVICE");
genAllFile();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void genAllFile() throws MalformedURLException,
ClassNotFoundException, InstantiationException,
IllegalAccessException, SQLException {
List<String> tblist = DBTools.getTableName();
for (int iCol = 0; iCol < tblist.size(); iCol++) {
String tbName = tblist.get(iCol);
try {
genOneFile(tbName);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void genOneFile(String tableName) throws Exception {
String table_name = tableName;
PropUtil prop1 = new PropUtil("/Hibernate.properties");
String pkgName = prop1.getProperty("javaPack");
String root_path = prop1.getProperty("OutfilePath");
Connection conn = DBTools.getConnet();
DatabaseMetaData dmd2 = conn.getMetaData();
ResultSet pkRs = dmd2.getPrimaryKeys(null, null, tableName);
// List<String> pklist = new ArrayList();
HashMap<String, String> pkh1 = new HashMap<String, String>();
while (pkRs.next()) {
// for (int i = 1; i < 6; i++) {
pkh1.put(pkRs.getString(4), pkRs.getString(4));
System.out.print(pkRs.getString(4) + " "); // 主键信息
// }
}
DatabaseMetaData metaRemark = conn.getMetaData();
ResultSet rsRemark = metaRemark.getColumns(null, null, tableName, null);
HashMap<String, String> h1 = new HashMap<String, String>();
while (rsRemark.next()) {
h1.put(rsRemark.getString("COLUMN_NAME"),
rsRemark.getString("REMARKS"));
System.out.println(rsRemark.getString("COLUMN_NAME") + ": "
+ rsRemark.getString("REMARKS"));
}
Statement st = conn.createStatement();
String sql = "select * from " + table_name + " where 1 = 2";
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData meta = rs.getMetaData();
String hbmFile = getHbmFile(root_path, table_name);
String beanFile = getBeanFile(root_path, table_name);
System.out.println("output hbm file: " + hbmFile);
System.out.println("output bean file: " + beanFile);
String beanName = getBeanName(table_name);
FileWriter outHbm = new FileWriter(hbmFile);
FileWriter outBean = new FileWriter(beanFile);
outHbm.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outHbm.write(" \r\n");
outHbm.write("<!DOCTYPE hibernate-mapping PUBLIC");
outHbm.write(" ");
outHbm.write(" \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\"");
outHbm.write(" ");
outHbm.write(" \"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">");
outHbm.write(" ");
outHbm.write("<hibernate-mapping>");
outHbm.write(" \r\n");
outHbm.write(" <class name=\"" + pkgName + "." + beanName
+ "\" table=\"" + table_name + "\">");
outHbm.write(" \r\n");
outBean.write("package " + pkgName + ";");
outBean.write(" \r\n");
outBean.write(" ");
outBean.write("public class " + beanName);
outBean.write(" ");
outBean.write("{");
outBean.write(" \r\n ");
StringBuffer toStr = new StringBuffer();
for (int iCol = 1; iCol <= meta.getColumnCount(); iCol++) {
String colName = meta.getColumnName(iCol);
int colType = meta.getColumnType(iCol);
String propName = getPropertyName(colName);
String javaType = getJavaType(colType).getName();
outBean.write(" private " + javaType + " " + propName + ";");
outBean.write(" \r\n");
}
for (int iCol = 1; iCol <= meta.getColumnCount(); iCol++) {
String colName = meta.getColumnName(iCol);
int colType = meta.getColumnType(iCol);
String propName = getPropertyName(colName);
String javaType = getJavaType(colType).getName();
Boolean flag = false; //主键ID标示
String pks = pkh1.get(colName); // 查找是否存在主件
if (pks == null || pks.equals("")) { // 主键字段,用ID表示
} else {
if(flag==true){
//已经有ID标示,不再用ID表示
}else{
outHbm.write(" <id name=\"" + propName + "\" type=\""
+ javaType + "\">\r\n");
outHbm.write(" <column name=\"" + colName + "\"> \r\n");
if (h1.get(colName) == null || h1.get(colName).equals("")) {
} else {
outHbm.write(" <comment>" + h1.get(colName)
+ "</comment> \r\n");
}
outHbm.write(" </column> \r\n");
outHbm.write(" </id> \r\n");
flag = true;
}
}
if(pkh1.size()==0 && iCol==1){ //如果表没有主键,则将第一字段定为ID项
outHbm.write(" <id name=\"" + propName + "\" type=\""
+ javaType + "\">\r\n");
outHbm.write(" <column name=\"" + colName + "\"> \r\n");
if (h1.get(colName) == null || h1.get(colName).equals("")) {
} else {
outHbm.write(" <comment>" + h1.get(colName)
+ "</comment> \r\n");
}
outHbm.write(" </column> \r\n");
outHbm.write(" </id> \r\n");
flag = true;
}
if (!flag) {
outHbm.write(" <property name=\"" + propName + "\" type=\""
+ javaType + "\">\r\n");
outHbm.write(" <column name=\"" + colName + "\"> \r\n");
if (h1.get(colName) == null || h1.get(colName).equals("")) {
} else {
outHbm.write(" <comment>" + h1.get(colName)
+ "</comment>\r\n");
}
outHbm.write(" </column> \r\n");
outHbm.write(" </property> \r\n");
}
outBean.write(" ");
outBean.write(" public " + javaType + " "
+ getGetterMethod(propName) + "()");
outBean.write(" ");
outBean.write(" {");
outBean.write(" \r\n");
outBean.write(" return this." + propName + ";");
outBean.write(" \r\n");
outBean.write(" }");
outBean.write(" \r\n");
outBean.write(" ");
outBean.write(" public void " + getSetterMethod(propName) + "("
+ javaType + " " + propName + ")");
outBean.write(" ");
outBean.write(" {");
outBean.write(" \r\n");
outBean.write(" this." + propName + " = " + propName + ";");
outBean.write(" \r\n");
outBean.write(" }");
outBean.write(" \r\n");
outBean.write(" ");
toStr.append("\"" + propName + " = " + "this." + propName
+ " /r/n\" ");
if (iCol != meta.getColumnCount()) {
toStr.append(" + ");
}
}
outHbm.write(" </class> \r\n");
outHbm.write(" ");
outHbm.write("</hibernate-mapping>");
outHbm.flush();
outHbm.close();
System.out.println("hbm file generated sucessfully!");
outBean.write(" @Override");
outBean.write(" ");
outBean.write(" public String toString()");
outBean.write(" ");
outBean.write(" {");
outBean.write(" ");
outBean.write(" return " + toStr.toString() + ";");
outBean.write(" ");
outBean.write(" }");
outBean.write(" ");
outBean.write("}");
outBean.flush();
outBean.close();
System.out.println("bean file generated sucessfully!");
}
private static String getHbmFile(String root_path, String table_name) {
没有合适的资源?快使用搜索试试~ 我知道了~
Hibernate映射工具,能获得xml,java
共31个文件
class:8个
java:7个
properties:4个
4星 · 超过85%的资源 需积分: 10 25 下载量 73 浏览量
2012-02-21
15:31:40
上传
评论
收藏 1.09MB ZIP 举报
温馨提示
Hibernate映射工具,能获得xml,jav。oracle10测试通过,其他数据库不保证能用。
资源推荐
资源详情
资源评论
收起资源包目录
TestWeb.zip (31个子文件)
TestWeb
.project 1KB
.settings
.jsdtscope 503B
org.eclipse.core.resources.prefs 88B
org.eclipse.wst.common.component 451B
org.eclipse.jdt.core.prefs 395B
org.eclipse.wst.common.project.facet.core.xml 305B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
src
Hibernate.properties 89B
PropUtil.java 4KB
com
css
pake
ParamTest.java 385B
dbconfig.properties 241B
PwdUtil.java 6KB
lld
test
hibernate
ParaProcess.java 2KB
HibernateTools.java 10KB
CheckThread.java 1KB
DBTools.java 5KB
build
classes
PropUtil.class 3KB
Hibernate.properties 89B
DBTools.class 4KB
ParaProcess.class 3KB
HibernateTools.class 9KB
com
css
pake
ParamTest.class 670B
CheckThread.class 2KB
dbconfig.properties 306B
lld
test
hibernate
PwdUtil.class 5KB
Person.class 470B
WebContent
WEB-INF
web.xml 631B
lib
ojdbc14.jar 1.12MB
META-INF
MANIFEST.MF 39B
.classpath 586B
共 31 条
- 1
资源评论
- snage2012-12-18确实不错,帮我省了很多的工作量
- qpc3533860512012-09-30很不错,要是能加上更多配置项就更好了
- c_faiy2012-06-28纯java代码,可当小工具使用,更重要的是可以从中学习到java开发技巧,很感谢作者分享
阿星777
- 粉丝: 115
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功