一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

一个自动生成Hibernate ORM映射文件的工具函数

时间:2022-11-14 23:26:39 编辑:袖梨 来源:一聚教程网

没啥,前几天有人问我如何生成Hibernate的映射文件,我随口说,就那点儿东西,自己随便写个函数不就得了,谁知对方这伙计有够懒,直接就开口说兄弟帮忙了,无奈之下随手应付了一个



import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.Types;
import java.util.Date;

public class ConfigGenerator
{

public static void main(String[] args) throws Exception
{
String table_name
= "user_list";
String root_path
= "C:/";
String pkgName
= "lld.test.hibernate";
Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver");
DriverManager.registerDriver(
new com.microsoft.jdbc.sqlserver.SQLServerDriver());
Connection conn
= DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test", "sa", "sa");
System.out.println(
"connected to database successfully.");
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(
"");
outHbm.write(
" ");
outHbm.write(
"");
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(
"");
outHbm.write(
" ");
outHbm.write(
" " + pkgName + "." + beanName + "" table="" + table_name + "">");
outHbm.write(
" ");

outBean.write(
"package " + pkgName + ";");
outBean.write(
" ");
outBean.write(
" ");
outBean.write(
"public class " + beanName);
outBean.write(
" ");
outBean.write(
"{");
outBean.write(
" ");

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();
outHbm.write(
" " + propName + "" column="" + colName + ""/>");
outHbm.write(
" ");

outBean.write(
" private " + javaType + " " + propName + ";");
outBean.write(
" ");
outBean.write(
" ");
outBean.write(
" public " + javaType + " " + getGetterMethod(propName) + "()");
outBean.write(
" ");
outBean.write(
" {");
outBean.write(
" ");
outBean.write(
" return this." + propName + ";");
outBean.write(
" ");
outBean.write(
" }");
outBean.write(
" ");
outBean.write(
" ");

outBean.write(
" public void " + getSetterMethod(propName) + "(" + javaType + " " + propName + ")");
outBean.write(
" ");
outBean.write(
" {");
outBean.write(
" ");
outBean.write(
" this." + propName + " = " + propName + ";");
outBean.write(
" ");
outBean.write(
" }");
outBean.write(
" ");
outBean.write(
" ");

toStr.append(
""" + propName + " = " + this." + propName + " + "rn" ");
if(iCol != meta.getColumnCount())
{
toStr.append(
" + ");
}


}


outHbm.write(
" ");
outHbm.write(
" ");
outHbm.write(
"");
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)
{
return root_path + table_name + ".hbm.xml";
}


private static String getBeanFile(String root_path, String table_name)
{
String beanName
= getBeanName(table_name);
return root_path + beanName + ".java";
}


private static String getBeanName(String table_name)
{
String[] segs
= table_name.split("_");
String beanName
= "";
for(int i = 0; i < segs.length; i++)
{
beanName
+= segs[i].substring(0, 1).toUpperCase() + segs[i].substring(1, segs[i].length()).toLowerCase();
}

return beanName;
}


private static String getPropertyName(String col_name)
{
String[] segs
= col_name.split("_");
String propName
= "";
for(int i = 0; i < segs.length; i++)
{
if(i == 0)
propName
+= segs[i];
else
propName
+= segs[i].substring(0, 1).toUpperCase() + segs[i].substring(1, segs[i].length()).toLowerCase();
}

return propName;
}


private static String getGetterMethod(String prop_name)
{
return "get" + prop_name.substring(0, 1).toUpperCase() + prop_name.substring(1, prop_name.length());
}


private static String getSetterMethod(String prop_name)
{
return "set" + prop_name.substring(0, 1).toUpperCase() + prop_name.substring(1, prop_name.length());
}


@SuppressWarnings(
"unchecked")
private static Class getJavaType(int colType)
{
switch(colType)
{
case Types.BIT:
case Types.TINYINT:
case Types.SMALLINT:
return Short.class;
case Types.INTEGER:
return Integer.class;
case Types.BIGINT:
return Long.class;
case Types.CHAR:
case Types.VARCHAR:
case Types.CLOB:
case Types.LONGNVARCHAR:
return String.class;
case Types.FLOAT:
return Float.class;
case Types.DOUBLE:
return Double.class;
case Types.DATE:
case Types.TIME:
case Types.TIMESTAMP:
return Date.class;
default:
return Object.class;
}

}

}