A tool function that automatically generates Hibernate ORM mapping files

Source: Internet
Author: User
No, a few days ago someone asked me how to generate hibernate mapping file, I casually said, on that little thing, their own casually write a function does not get, who knows the other side this guy has enough lazy, directly to say brother help, helpless under the hand to deal with a











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 voidMain (string[] args)throwsException
...{
String table_name= "user_list";
String Root_path= "c:/";
String Pkgname= "lld.test.hibernate";
Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver");
Drivermanager.registerdriver (Newcom.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= NewFileWriter (Hbmfile);
FileWriter Outbean= NewFileWriter (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 ("");
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= NewStringBuffer ();

for(intIcol= 1; Icol<=Meta.getcolumncount (); Icol++)
...{
String colname=Meta.getcolumnname (Icol);
intColtype=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 ("");
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 Staticstring Gethbmfile (String Root_path, String table_name)
...{
return root_path + table_name + " . Hbm.xml " ;
}

Private StaticString Getbeanfile (String Root_path, String table_name)
...{
String beanname = getbeanname (table_name);
return root_path + beanname + ". Java ";
}

Private Staticstring Getbeanname (String table_name)
...{
String[] Segs=Table_name.split ("_");
String Beanname= "";
for(intI= 0; I<Segs.length i++)
...{
Beanname + + segs[i].substring (0, 1). toUpperCase () + segs[i].substring (1, Segs[i].length ()). toLowerCase ();
}
returnBeanname;
}

Private StaticString Getpropertyname (String col_name)
...{
String[] Segs=Col_name.split ("_");
String propname= "";
for(intI= 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 ();
}
returnpropname;
}

Private Staticstring Getgettermethod (String prop_name)
...{
return ' get ' + prop_ Name.substring (0, 1). toUpperCase () + prop_ Name.substring (1, Prop_name.length ());
}

Private Staticstring Getsettermethod (String prop_name)
...{
return "set" + prop_ Name.substring (0, 1). toUpperCase () + prop_ Name.substring (1, Prop_name.length ());
}

@SuppressWarnings ("unchecked")
Private StaticClass Getjavatype (intColtype)
...{
Switch(Coltype)
...{
CaseTypes.bit:
CaseTypes.tinyint:
CaseTypes.smallint:
returnShort .class;
CaseTypes.integer:
returnInteger.class;
CaseTypes.bigint:
returnLong.class;
CaseTypes.char:
CaseTypes.varchar:
CaseTypes.clob:
CaseTypes.longnvarchar:
returnString.class;
Casetypes.float:
returnFloat.class;
Casetypes.double:
returnDouble.class;
Casetypes.date:
CaseTypes.time:
CaseTypes.timestamp:
returnDate.class;
default:
returnObject.class;
}
}
}






<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.