一个自动生成Hibernate ORM映射文件的工具函数
没啥,前几天有人问我如何生成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("<?xml version="1.0" encoding="UTF-8"?>");
outHbm.write(" ");
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(" ");
outHbm.write(" <class name="" + 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(" <property name="" + 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(" </class>");
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)
{
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;
}
}
}
<
补充:Jsp教程,Java基础
上一个:商店购物
下一个:serversocket 堵塞
- 更多JSP疑问解答:
- jsp新手求指导,不要笑!
- 如何让一个form提取的值传递给多个jsp?
- DW中,新建的html页面能否有jsp或php代码?
- jsp 如何限制表单,实现只能填写特定的数据。
- jsp 和javabean结合的程序有问题
- 从数据库里取出的数据如何传递到另外的jsp页面中
- 你好,ext嵌入那个jsp页面,是不是还需要加上一些插件啊,不太懂,麻烦你了。
- JSP不能处理所有问题吗?还要来一大堆的TLD,TAG,XML。为JSP 非要 Servlet 不可吗?
- 光标离开时全角转半角在jsp中怎么实现
- jsp 页面 打开 pdf 文件 控制大小 和 工具栏 能发份源码么 谢啦
- jsp页面点保存按钮,运行缓慢,弹出对话框提示
- jsp刷新页面如何不闪屏
- jsp 与html 的交互问题?
- jsp小数显示问题 例如 我在oracle 数据库中查询出来的是 0.01 但是在jsp页面上就显示成 .01 没有前面的0
- jsp中日历控件