Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的J2EE架构中取代CMP,完成数据持久化的重任
核心接口和类
Hibernate的核心类和接口一共有6个,分别为:Session、SessionFactory、
Transaction、Query、Criteria和Configuration。这6个核心类和接口在任何开发中都会用到。通过这些接口,不仅可以对持久化对象进行存取,还能够进行事务控制。下面对这6个核心接口和类分别加以介绍。1:创建第一个hibernate,(开发工具以eclipse为例)首先安装hibernate-tools 插件,安装教程地址:
http://jingyan.baidu.com/article/a24b33cd695bbe19fe002bd7.html
2:安装完毕之后,去hibernate 官网去下载必要jar包,源码在官网source code点击下载:
基础jar包如下
3:创建一个实体类,和其hibernate映射(映射是告诉hibernate框架,对象如何和数据库的表对应的):
User对象:
package com.leige.domain;
import java.util.Date;
public class User {
private int id;
private String name;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
User对象的实体映射文件,命名为---对象.hbm.xml,也可以用eclipse的hibernate插件自动生成,生成方法如下:右键对象, new-other-hibernate-hibernate xml mapping file,如下:
<?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.leige.domain">
<class name="User">
<id name="id" >
<generator class="assigned" />
</id>
<property name="name" unique="true"/>
<property name="birthday" />
</class>
</hibernate-mapping>
一般系统默认id为主键,主键的generator, class属性若为assigend则为主键自己指定,若为native则为系统自增
4:添加hibernate运行是的配置文件,如同struts2运行需要strust.xml文件一样,我们可以在hibernate.cfg.xml,,配置我们要连接的数据库类型,配置连接参数,hibernate底层也是用jdbc实现的,所以也需要jdbc四大参数,以后我们还可学习配置数据库连接池来实现数据库连接,简单配置如下:
<span style="color:#ffffff;"><!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
</span>
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 指定方言,指定mysql ,建议配置成上面,不然hibernate在执行创建表的时候会报错,目前没有找到原因-->
<property name="hbm2ddl.auto">update</property><!-- -这个参数指定,hibernate可以更新数据,当为create时,hibernate每次执行插入操作都会先删除表,然后在创建表插入数据 -->
<property name="show_sql">true</property><!-- -这个参数,表示,hibernate会把自动生成的参数,显示在控制台给我们看,一般用于开发阶段 -->
<mapping resource="com/leige/domain/User.hbm.xml"/><!-- -指定映射文件,告诉hibernate,对象与表的映射关系 -->
</session-factory>
</hibernate-configuration>
5:编写hibernate工具类,用于获取sessionfactory和session(session是hibernate中 操作数据库的对象,可以从sessionFactory中得到,sessionFactory从配置中得到):
package com.leige.domain;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
static Configuration cfg=null;
//加载配置方法
static{
//默认加载src下的/hibernate.cfg.xml,也可以指定其他文件
cfg=new Configuration();
cfg.configure();
}
public static SessionFactory getSessionFactory(){
//返回一个session工厂
return cfg.buildSessionFactory();
}
public static Session getSession(){
//从session工厂获取session
return getSessionFactory().openSession();
}
}
6:测试(标准流程是:try -catch-finally,类似于jdbc的标准流程):
public class Test {
public static void main(String[] args) {
User user=new User();
user.setId(1);
<span style="white-space:pre"> </span>user.setBirthday(new Date());
user.setName("leige3");
add(user);
}
public static void add(User user){
Session session= HibernateUtils.getSession();
Transaction tx=null;
//标准格式
try {
tx=session.beginTransaction();
session.save(user);
tx.commit();
} catch (Exception e) {
if(tx!=null)tx.rollback();
System.out.println(e);
}finally{
session.close();
}
}
}
6:执行结束后我们可以看到,hibernate生成的语句sql:
语句如下:数据库中也有了相应的数据,(注意,若一开始没有表建议把hbm2ddl.auto设置为create,这样就可以生成表了,之后再改为update)
Hibernate: insert into User (name, birthday, id) values (?, ?, ?)