Hibernate的核心组件
在基于MVC设计模式的JAVA WEB应用中,Hibernate可以作为模型层/数据访问层。它通过配置文件(hibernate.properties或hibernate.cfg.xml)和映射文件(***.hbm.xml)把JAVA对象或PO(Persistent Object,持久化对象)映射到数据库中的数据库,然后通过操作PO,对数据表中的数据进行增,删,改,查等操作。
除配置文件,映射文件和持久化类外,Hibernate的核心组件包括以下几部分:
a)Configuration类:用来读取Hibernate配置文件,并生成SessionFactory对象。
b)SessionFactory接口:产生Session实例工厂。
c)Session接口:用来操作PO。它有get(),load(),save(),update()和delete()等方法用来对PO进行加载,保存,更新及删除等操作。它是Hibernate的核心接口。
d)Query接口:用来对PO进行查询操。它可以从Session的createQuery()方法生成。
e)Transaction接口:用来管理Hibernate事务,它主要方法有commit()和rollback(),可以从Session的beginTrancation()方法生成。
Persistent Object
持久化对象可以是普通的Javabeans,惟一特殊的是它们与(仅一个)Session相关联。JavaBeans在Hibernate中存在三种状态:
1.临时状态(transient):当一个JavaBean对象在内存中孤立存在,不与数据库中的数据有任何关联关系时,那么这个JavaBeans对象就称为临时对象(Transient Object)。
2.持久化状态(persistent):当一个JavaBean对象与一个Session相关联时,就变成持久化对象(Persistent Object)
3.脱管状态(detached):在这个Session被关闭的同时,这个对象也会脱离持久化状态,就变成脱管状态(Detached Object),可以被应用程序的任何层自由使用,例如可以做与表示层打交道的数据舆对象(Data Transfer Object)。
Hibernate的运行过程
Hibernate的运行过程如下:
A:应用程序先调用Configration类,该类读取Hibernate的配置文件及映射文件中的信息,并用这些信息生成一个SessionFactpry对象。
B:然后从SessionFactory对象生成一个Session对象,并用Session对象生成Transaction对象;可通过Session对象的get(),load(),save(),update(),delete()和saveOrUpdate()等方法对PO进行加载,保存,更新,删除等操作;在查询的情况下,可通过Session对象生成一个Query对象,然后利用Query对象执行查询操作;如果没有异常,Transaction对象将 提交这些操作结果到数据库中。
Hibernate的运行过程如下图:
![](https://i-blog.csdnimg.cn/blog_migrate/5a02c876001770373a9cf3cce0df80a4.jpeg)
Hibernate简单示例:
数据:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
create
table
T_register
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
(
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
id
int
primary
key
,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
userName
varchar
(
30
),
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
userPwd
varchar
(
30
),
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
sex
varchar
(
10
),
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
age
int
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
)
视图层:注册页面register.jsp
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
<%
@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"
%>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
html
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
head
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=GBK"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
title
>
Insert title here
</
title
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
head
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
body
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
form
action
="servlet/RegisterServlet"
method
="POST"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
用户名:
<
input
type
="text"
name
="userName"
/><
BR
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
密 码:
<
input
type
="text"
name
="userPwd"
/><
BR
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
性 别:
<
input
type
="text"
name
="sex"
/><
BR
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
年 龄:
<
input
type
="text"
name
="age"
/><
BR
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
input
type
="submit"
value
="save"
/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
form
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
body
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
html
>
设计持久化类TRegister.java
持久化类
package hibernate.PO;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
* 持久化类
*/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class TRegister implements java.io.Serializable
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Fields
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Integer id;
private String userName;
private String userPwd;
private String sex;
private Integer age;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Constructors
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//** default constructor */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public TRegister()
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//** minimal constructor */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public TRegister(Integer id)
{
this.id = id;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//** full constructor */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public TRegister(Integer id, String userName, String userPwd, String sex, Integer age)
{
this.id = id;
this.userName = userName;
this.userPwd = userPwd;
this.sex = sex;
this.age = age;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Property accessors
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Integer getId()
{
return this.id;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setId(Integer id)
{
this.id = id;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getUserName()
{
return this.userName;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setUserName(String userName)
{
this.userName = userName;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getUserPwd()
{
return this.userPwd;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setUserPwd(String userPwd)
{
this.userPwd = userPwd;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getSex()
{
return this.sex;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setSex(String sex)
{
this.sex = sex;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Integer getAge()
{
return this.age;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setAge(Integer age)
{
this.age = age;
}
}
设计Hibernate配置文件hibernate.cfg.xml
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/study
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">MySQL5.0</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
org.gjt.mm.mysql.Driver
</property>
<property name="show_sql">true</property>
<mapping resource="hibernate/PO/TRegister.hbm.xml" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</session-factory>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</hibernate-configuration>
设计映射文件TRegister.hbm.xml
TRegister.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="hibernate.PO.TRegister" table="t_register">
<id name="id" type="java.lang.Integer">
<column name="id" />
<!-- 我在MYSQL中并没有设置该字段递增,但在Hibernate中依然可以设置为递增 -->
<generator class="increment" />
</id>
<property name="userName" type="java.lang.String">
<column name="userName" length="30" />
</property>
<property name="userPwd" type="java.lang.String">
<column name="userPwd" length="30" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" length="10" />
</property>
<property name="age" type="java.lang.Integer">
<column name="age" />
</property>
</class>
</hibernate-mapping>
设计hibernate基础类HibernateUtil.java
设计控制类
编译----->打包----->运行----->OK!