HibernateUtil
package ease
import net
import net
import net
/**
* Configures and provides access to Hibernate sessions
* current thread of execution
* pattern
*/
public class HibernateUtil {
/**
* Location of hibernate
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file
* is place the config file in a Java package
* is the default Java package
* Examples: <br>
* <code>CONFIG_FILE_LOCATION =
* CONFIG_FILE_LOCATION =
*/
private static String CONFIG_FILE_LOCATION =
/** Holds a single instance of Session */
private static final ThreadLocal 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
/**
* Returns the ThreadLocal Session instance
* the <code>SessionFactory</code> if needed
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal
if (session == null) {
if (sessionFactory == null) {
try {
nfigure(CONFIG_FILE_LOCATION);
//nfigure();
sessionFactory = cfg
}
catch (Exception e) {
System
System
e
}
}
session = sessionFactory
threadLocal
}
return session;
}
/**
* Close the single hibernate session instance
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal
threadLocal
if (session != null) {
session
}
}
/**
* Default constructor
*/
private HibernateUtil() {
}
}
一
cat表
代碼
CREATE TABLE cat (
cat_id varchar(
NAME varchar(
sex CHAR(
weight FLOAT
PRIMARY KEY (cat_id)
);
二
Xml代碼
<<<<<<<<<<<<<cat
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<column name=
<generator class=
</id>
<property name=
<column name=
</property>
<property name=
<property name=
</class>
</hibernate
Java代碼
<<<<<<<<<<<<<Cat
package ease
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
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this
}
public String toString() {
String strCat = new StringBuffer()
return strCat;
}
}
三
(切切不要自己去加屬性
Xml代碼
<property name=
<property name=
<property name=
<property name=
<<<<<<<<<<<<<hibernate
<?xml version=
<!DOCTYPE hibernate
<!
<!
<hibernate
<session
<!
<property name=
<property name=
<property name=
<!
<mapping resource=
</session
</hibernate
四
//Session生成/關閉
Java代碼
<<<<<<<<<<<<<測試HbntTestSvlt
/*
*
* @(#)rpsms V
* Copyright
*
* coder: sweater
* email:
*
* graphic designer:
* email:
*
* fuction
*
*/
package ease
import java
import java
import java
import javax
import javax
import javax
import javax
import net
import net
import net
import net
import ease
import ease
/**
*
* @author sweater
*/
public class HbntTestSvlt extends HttpServlet {
/**
* Constructor of the object
*/
public HbntTestSvlt() {
super();
}
/**
* Destruction of the servlet
*/
public void destroy() {
super
// Put your code here
}
/**
* The doGet method of the servlet
*
* 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
throws ServletException
response
PrintWriter out = response
out
out
out
out
out
out
try {
Session session = HibernateUtil
Transaction tx = session
Query query = session
query
for (Iterator it = erate(); it
Cat cat = (Cat) it
out
}
mit();
HibernateUtil
} catch (HibernateException e) {
System
//System
}
out
out
}
/**
* The doPost method of the servlet
*
* 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
throws ServletException
response
PrintWriter out = response
out
out
out
out
out
out
out
out
out
out
out
}
/**
* Returns information about the servlet
* author
*
* @return String information about this servlet
*/
public String getServletInfo() {
return
}
/**
* Initialization of the servlet
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
Xml代碼
<<<<<<<<<<<<<該servlet使用時的web
<?xml version=
<web
xmlns=
xmlns:xsi=
xsi:schemaLocation=
<servlet>
<description>hbnttest</description>
<display
<servlet
<servlet
</servlet>
<servlet
<servlet
<url
</servlet
</web
</SPAN< p>
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27890.html