一、在Web项目中导入Hibernate最小依赖jar包、数据库驱动jar包,如下图所示。
二、在src/config目录下添加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"
>
<
hibernate-configuration
>
<
session-factory
>
<
property
name
=
"myeclipse.connection.profile"
>
com.mysql.jdbc.MySqlDriver
</
property
>
<
property
name
=
"hibernate.connection.username"
>
root
</
property
>
<
property
name
=
"hibernate.connection.password"
>
root
</
property
>
<
property
name
=
"hibernate.connection.url"
>
jdbc:mysql:// localhost:3306/hibernate_demo?useUnicode=true
&
characterEncoding= utf-8
</
property
>
<
property
name
=
"hibernate.connection.driver_class"
>
com.mysql.jdbc.Driver
</
property
>
<
property
name
=
"hibernate.dialect"
>
org.hibernate.dialect.MySQLDialect
</
property
>
<
property
name
=
"hibernate.show_sql"
>
true
</
property
>
<
property
name
=
"hibernate.hbm2ddl.auto"
>
validate
</
property
>
<
mapping
resource
=
"Student.hbm.xml"
/>
</
session-factory
>
</
hibernate-configuration
>
三、创建实体类(持久化类)。
【Student.java】对应数据库中的实体
public
class
Student {
private
int
sid
;
private
String
sno
;
private
String
sname
;
private
String
sgender
;
private
String
college
;
public
Student() {
}
public
Student(
int
sid, String sno, String sname, String sgender,
String college) {
super
();
this
.
sid
= sid;
this
.
sno
= sno;
this
.
sname
= sname;
this
.
sgender
= sgender;
this
.
college
= college;
}
public
int
getSid() {
return
sid
;
}
public
void
setSid(
int
sid) {
this
.
sid
= sid;
}
public
String getSno() {
return
sno
;
}
public
void
setSno(String sno) {
this
.
sno
= sno;
}
public
String getSname() {
return
sname
;
}
public
void
setSname(String sname) {
this
.
sname
= sname;
}
public
String getSgender() {
return
sgender
;
}
public
void
setSgender(String sgender) {
this
.
sgender
= sgender;
}
public
String getCollege() {
return
college
;
}
public
void
setCollege(String college) {
this
.
college
= college;
}
}
四、创建对象-关系映射文件,并在hibernate.config.xml中添加标签<mapping resource="Student.hbm.xml "/>
【Student.hbm.xml】
<?
xml
version
=
"1.0"
?>
<!
DOCTYPE
hibernate-mapping
PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<
hibernate-mapping
package
=
"com.demo.hibernate.entity"
>
<
class
name
=
"Student"
table
=
"student"
>
<
id
name
=
"sid"
type
=
"int"
>
<
column
name
=
"sid"
></
column
>
<
generator
class
=
"increment"
></
generator
>
</
id
>
<
property
name
=
"sno"
type
=
"java.lang.String"
>
<
column
name
=
"sno"
></
column
>
</
property
>
<
property
name
=
"sname"
type
=
"java.lang.String"
>
<
column
name
=
"sname"
></
column
>
</
property
>
<
property
name
=
"sgender"
type
=
"java.lang.String"
>
<
column
name
=
"sgender"
></
column
>
</
property
>
<
property
name
=
"college"
type
=
"java.lang.String"
>
<
column
name
=
"college"
></
column
>
</
property
>
</
class
>
</
hibernate-mapping
>
五、使用Hibernate API编写代码操作数据库
【CustomSessionFactory.java】
public
class
CustomSessionFactory {
private
static
SessionFactory
sessionFactory
;
private
static
Session
session
;
private
static
Transaction
transaction
;
public
static
Session getSession(){
init();
return
session
;
}
public
static
void
init(){
//创建配置对象
Configuration config=
new
Configuration().configure();
//创建服务配置对象
ServiceRegistry serviceRegistry=
new
ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建会话工厂对象
sessionFactory
=config.buildSessionFactory(serviceRegistry);
//创建会话对象
session
=
sessionFactory
.openSession();
//开启事务
transaction
=
session
.beginTransaction();
}
public
static
void
closeSession(){
//提交事务
transaction
.commit();
//关闭会话
session
.close();
//关闭会话工厂
sessionFactory
.close();
}
}
eg:保存一个Student对象到数据库:
【StudentDaoIml.java】
public class StudentDaoImpl implements StudentDao{
@Override
public void saveStudent(Student student) {
Session session=CustomSessionFactory.getSession();
session.save(student);
CustomSessionFactory.closeSession();
}
}