Hibernate entry-level Configuration
I. Create a table
<Table cat >>>>>>>>>>>>>>>>>>>>>>>> >>>>
Create Table CAT (
Cat_id varchar (20) not null,
Name varchar (20) not null,
Sex char (1 ),
Weight float,
Primary Key (cat_id)
);
Ii. Po layer (the system uses cat. HBM. XML as the standard, and one XML can write multiple classes)
<Cat. HBM. XML >>>>>>>>>>>>>>>>>>>>>>>
<? XML version = "1.0"?>
<! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD // en" http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd ">
<Hibernate-mapping>
<Class name = "com. Netease. Wireless. groupsms. hbnt. Po. Cat" table = "cat">
<ID name = "ID" type = "string" unsaved-value = "null">
<Column name = "cat_id" SQL-type = "varchar (20)" not-null = "true"/>
<Generator class = "UUID. Hex"/>
</ID>
<Property name = "name">
<Column name = "name" SQL-type = "varchar (20)" not-null = "true"/>
</Property>
<Property name = "sex"/>
<Property name = "weight"/>
</Class>
</Hibernate-mapping>
<Cat. java >>>>>>>>>>>>>>>>>>>>>>>>
Package com. Netease. Wireless. groupsms. hbnt. Po;
Public class cat {
Private string ID;
Private string name;
Private char sex;
Private float weight;
Public CAT (){
}
Public String GETID (){
Return ID;
}
Public void setid (string ID ){
This. ID = ID;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public char getsex (){
Return sex;
}
Public void setsex (char sex ){
This. Sex = sex;
}
Public float getweight (){
Return weight;
}
Public void setweight (float weight ){
This. Weight = weight;
}
Public String tostring (){
String strcat = new stringbuffer ()
. Append (this. GETID (). append (",")
. Append (this. getname (). append (",")
. Append (this. getsex (). append (",")
. Append (this. getweight ())
. Tostring ();
Return strcat;
}
}
Iii. hibernate. cfg. xml
(Do not add attributes by yourself. Basically, there are two connection. datasource and dialect)
<Property name = "connection. username"> root </property>
<Property name = "connection. Password"> root </property>
<Property name = "connection. provider_class"> net. SF. hibernate. Connection. performanceconnectionprovider </property>
<Property name = "JNDI. Class"> org. gjt. Mm. MySQL. Driver </property>
<Hibernate. cfg. XML >>>>>>>>>>>>>>>>>>>>>>>
<? XML version = '1. 0' encoding = 'utf-8'?>
<! Doctype hibernate-configuration public
"-// Hibernate/hibernate configuration DTD 2.0 // en"
Http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd>
<! -- Do not edit: This is a generated file that is synchronized -->
<! -- By myeclipse hibernate tool integration. -->
<Hibernate-configuration>
<Session-factory>
<! -- Properties -->
<Property name = "connection. datasource"> JAVA: COMP/ENV/jdbc/test </property>
<Property name = "show_ SQL"> true </property>
<Property name = "dialect"> net. SF. hibernate. dialect. mysqldialect </property>
<! -- Mapping Files -->
<Mapping Resource = "com/Netease/wireless/groupsms/hbnt/po/cat. HBM. xml"/>
</Session-factory>
</Hibernate-configuration>
Iv. Test Servlet
// Session generation/Closure
<Hibernateutil. Java >>>>>>>>>>>>>>>>>>>>>>>
Package com. Netease. Wireless. groupsms. hbnt. util;
Import net. SF. hibernate. hibernateexception;
Import net. SF. hibernate. Session;
Import net. SF. hibernate. cfg. configuration;
/**
* Configures and provides access to hibernate sessions, tied to
* Current thread of execution. follows the Thread Local session
* Pattern, see {@ link http://hibernate.org/42.html }.
*/
Public class hibernateutil {
/**
* Location of hibernate. cfg. xml file.
* Notice: Location shocould be on the classpath as Hibernate uses
* # Resourceasstream style lookup for its configuration file. That
* Is place the config file in a Java package-the default location
* Is the default Java package. <br>
* Examples: <br>
* <Code> config_file_location = "/hibernate. conf. xml ".
* Config_file_location = "/COM/Foo/BAR/myhiberstuff. conf. xml". </code>
*/
Private Static string config_file_location = "/hibernate. cfg. xml"; // hibernate. cfg. xml
/** Holds a single instance of session */
Private Static final threadlocal = new threadlocal ();
/** The single instance of hibernate configuration */
Private Static final configuration CFG = new configuration ();
/** The single instance of hibernate sessionfactory */
Private Static net. SF. hibernate. sessionfactory;
/**
* Returns the threadlocal session instance. Lazy initialize
* The <code> sessionfactory </code> if needed.
*
* @ Return session
* @ Throws hibernateexception
*/
Public static session currentsession () throws hibernateexception {
Session session = (Session) threadlocal. Get ();
If (session = NULL ){
If (sessionfactory = NULL ){
Try {
Cfg. Configure (config_file_location );
// Cfg. Configure ();
Sessionfactory = cfg. buildsessionfactory ();
}
Catch (exception e ){
System. Err. println ("% error creating sessionfactory % ");
System. Err. println ("|" + E. getmessage ());
E. printstacktrace ();
}
}
Session = sessionfactory. opensession ();
Threadlocal. Set (session );
}
Return session;
}
/**
* Close the single hibernate session instance.
*
* @ Throws hibernateexception
*/
Public static void closesession () throws hibernateexception {
Session session = (Session) threadlocal. Get ();
Threadlocal. Set (null );
If (session! = NULL ){
Session. Close ();
}
}
/**
* Default constructor.
*/
Private hibernateutil (){
}
}
<Test hbnttestsvlt. Java >>>>>>>>>>>>>>>>>>>>>>>>
/*
*
* @ (#) Corpsms. corpsms V1.0 2005-1-15
* Copyright 2003 Netease, Inc. All rights reserved.
*
* Coder: sweater
* Email: wtshi@corp.netease.com
*
* Graphic Designer:
* Email:
*
* Fuction: Add a group and phone number to the phone book.
*
*/
Package com. Netease. Wireless. groupsms. VO;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import java. util. iterator;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import net. SF. hibernate. hibernateexception;
Import net. SF. hibernate. query;
Import net. SF. hibernate. Session;
Import net. SF. hibernate. transaction;
Import com. Netease. Wireless. groupsms. hbnt. Po. CAT;
Import com. Netease. Wireless. groupsms. hbnt. util. hibernateutil;
/**
*
* @ Author sweater
*/
Public class hbnttestsvlt extends httpservlet {
/**
* Constructor of the object.
*/
Public hbnttestsvlt (){
Super ();
}
/**
* Destruction of the servlet. <br>
*/
Public void destroy (){
Super. Destroy (); // just puts "Destroy" string in log
// Put your code here
}
/**
* The doget method of the servlet. <br>
*
* This method is called when a form has its tag Value Method equals to get.
*
* @ Param request the request send by the client to the server
* @ Param response the response send by the server to the client
* @ Throws servletexception if an error occurred
* @ Throws ioexception if an error occurred
*/
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. setcontenttype ("text/html ");
Printwriter out = response. getwriter ();
Out. println ("<HTML> ");
Out. println ("Out. println ("<title> Hello hibernate </title> ");
Out. println ("Out. println ("<body> ");
Out. println ("Hello hibernate! <Br> ");
Try {
Session session = hibernateutil. currentsession ();
Transaction Tx = session. begintransaction ();
Query query = session. createquery ("select cat from cat as cat where Cat. Sex =: Sex ");
Query. setcharacter ("sex", 'F ');
For (iterator it = query. iterate (); it. hasnext ();){
Cat cat = (CAT) it. Next ();
Out. println ("Cat:" + cat. tostring () + "<br> ");
}
TX. Commit ();
Hibernateutil. closesession ();
} Catch (hibernateexception e ){
System. Out. println (E. getmessage ());
// System. Out. println (E. printstacktrace ());
}
Out. println ("</body> ");
Out. println ("}
/**
* The dopost method of the servlet. <br>
*
* This method is called when a form has its tag Value Method equals to post.
*
* @ Param request the request send by the client to the server
* @ Param response the response send by the server to the client
* @ Throws servletexception if an error occurred
* @ Throws ioexception if an error occurred
*/
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. setcontenttype ("text/html ");
Printwriter out = response. getwriter ();
Out. println ("<! Doctype HTML public/"-// W3C // dtd html 4.01 transitional // en/"> ");
Out. println ("<HTML> ");
Out. println ("Out. println ("<body> ");
Out. Print ("this is ");
Out. Print (this. getclass ());
Out. println (", using the POST method ");
Out. println ("</body> ");
Out. println ("Out. Flush ();
Out. Close ();
}
/**
* Returns information about the servlet, such
* Author, version, and copyright.
*
* @ Return string information about this servlet
*/
Public String getservletinfo (){
Return "this is my default servlet created by Eclipse ";
}
/**
* Initialization of the servlet. <br>
*
* @ Throws servletexception if an error occure
*/
Public void Init () throws servletexception {
// Put your code here
}
}
<Web. XML >>>>>>>>>>>>>>>>>>>>>>>
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Servlet>
<Description> hbnttest </description>
<Display-Name> hbnttest </display-Name>
<Servlet-Name> hbnttestsvlt </servlet-Name>
<Servlet-class> com. Netease. Wireless. groupsms. VO. hbnttestsvlt </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> hbnttestsvlt </servlet-Name>
<URL-pattern>/servlet/hbnttestsvlt </url-pattern>
</Servlet-mapping>
</Web-app>