在基於MVC設計模式的JAVA WEB應用中
除配置文件
a)Configuration類
b)SessionFactory接口
c)Session接口
d)Query接口
e)Transaction接口
Persistent Object
持久化對象可以是普通的Javabeans
Hibernate的運行過程
Hibernate的運行過程如下
A:應用程序先調用Configration類
B:然後從SessionFactory對象生成一個Session對象
Hibernate簡單示例
數據
create table T_register
(
id int primary key
userName varchar(
userPwd varchar(
sex varchar(
age int
)
視圖層
用戶名
密 碼
性 別
年 齡
設計持久化類TRegister
package hibernate
/**
* 持久化類
*/
public class TRegister implements java
// Fields
private Integer id;
private String userName;
private String userPwd;
private String sex;
private Integer age;
// Constructors
/** default constructor */
public TRegister() {
}
/** minimal constructor */
public TRegister(Integer id) {
this
}
/** full constructor */
public TRegister(Integer id
this
this
this
this
this
}
// Property accessors
public Integer getId() {
return this
}
public void setId(Integer id) {
this
}
public String getUserName() {
return this
}
public void setUserName(String userName) {
this
}
public String getUserPwd() {
return this
}
public void setUserPwd(String userPwd) {
this
}
public String getSex() {
return this
}
public void setSex(String sex) {
this
}
public Integer getAge() {
return this
}
public void setAge(Integer age) {
this
}
}
設計Hibernate配置文件hibernate
root
jdbc:mysql://localhost:
org
MySQL
root
org
true
設計映射文件TRegister
設計hibernate基礎類HibernateUtil
package hibernate;
/**
* hibernate 基礎類
* @author fengyan
* date
*/
import org
import org
import org
import org
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static
{
try
{
Configuration config = new Configuration(nfigure(
sessionFactory = config
}
catch(Throwable e)
{
throw new ExceptionInInitializerError(e);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException
{
Session s = (Session)session
//Open a new Session
if(s == null || !s
{
s = sessionFactory
session
}
return s;
}
public static void closeSession() throws HibernateException
{
Session s = (Session)session
session
if(s != null)
s
}
}
設計控制類
package hibernate
/**
* @author fengyan
* date
* 設計Hibernate控制類
*/
import hibernate
import hibernate
import java
import java
import javax
import javax
import javax
import javax
import org
import org
import org
public class RegisterServlet extends HttpServlet {
private static final String CONTENT_TYPE =
public void init() throws ServletException {
// Put your code here
}
public void destroy() {
super
// Put your code here
}
public void doGet(HttpServletRequest request
throws ServletException
response
request
PrintWriter out = response
String userName = request
String userPwd = request
String sex = request
int age = Integer
TRegister rg = new TRegister();
rg
rg
rg
rg
Session session = HibernateUtil
Transaction tx = session
try
{
session
mit(); //提交到數據庫
session
response
}
catch(HibernateException e)
{
e
tx
}
}
public void doPost(HttpServletRequest request
throws ServletException
doGet(request
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28144.html