最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
一个自动生成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;
}
}
}
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("
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("
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;
}
}
}
<
相关文章
- 人们熟悉的寄居蟹属于以下哪种分类 神奇海洋11月21日答案 11-21
- 第五人格11.22共研服有什么更新 11月22日共研服更新内容介绍 11-21
- 原神恰斯卡怎么培养 11-21
- 无期迷途四星装束是谁 11-21
- 王者荣耀帝丹高中校服怎么获得 11-21
- 光遇姆明季后续版本怎么玩 11-21
热门栏目
-
php教程
php入门 php安全 php安装 php常用代码 php高级应用 -
asp.net教程
基础入门 .Net开发 C语言 VB.Net语言 WebService -
手机开发
安卓教程 ios7教程 Windows Phone Windows Mobile 手机常见问题 -
css教程
CSS入门 常用代码 经典案例 样式布局 高级应用 -
网页制作
设计基础 Dreamweaver Frontpage js教程 XNL/XSLT -
办公数码
word excel powerpoint 金山WPS 电脑新手 -
jsp教程
Application与Applet J2EE/EJB/服务器 J2ME开发 Java基础 Java技巧及代码