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) {