hibernate是当前应用最为广泛的持久层框架之一,简单来说,是对jdbc进行了进一步封装,完成了关系型数据库和对象之间的映射,只需要面向对象进行操作,然后映射到数据库,不用再面对复杂的sql语句。
一、简介
hibernate主要有几个接口,
1、Configuration:读取配置,启动hibernate
2、SessionFactory:顾名思义,是创建Session的工厂,用于初始化hibernate
3、Session:负责进行增删改查操作,和数据库打交道的,是一个持久化管理器
4、Transaction:事务管理
5、Query:负责各种数据库查询
被持久化管理的对象有三种状态:Transient:刚new出来,还没有被session管理,数据库中也没有相关数据;
Persist:持久化状态,处于事务操作中,在事务结束的时候和数据库进行同步(flush);Detached:session关闭之后,不再被session管理,数据库中有相关数据。
这几种状态,会对对象进行save或者update等操作或者事务commit,会改变状态。
二、实例
创建一个实例,简单实现hibernate:
1、创建项目,引入jar:
实例采用hibernate3的版本,下载hibernate相关,为了使用方便,创建了user library,加入了hibernate核心jar和hibernate引用的jar和MySQL驱动jar:
项目引入该user library之后:
2、引入log4j.properties:
可以直接将hibernate-3.2/etc/log4j.properties拷贝到项目中。
3、创建实体对象User:
package com.tgb.hibernate;
import java.util.Date;
public class User {
private String id;
private String name;
private String password;
private Date createTime;
private Date expireTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getExpireTime() {
return expireTime;
}
public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}
}
4、创建hibernate全局配置文件:hibernate.cfg.xml,并配置:
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<!-- MySQL方言配置 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 读取映射文件 -->
<mapping resource="com/tgb/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<?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>
<class name="com.tgb.hibernate.User">
<id name="id">
<generator class="uuid"/>
</id>
<property name="name"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>这个user对象就对应这个class标签,id是主键,property是普通属性,id的子标签generator定义了主键生成策略:uuid。
6、创建根据实体映射生成数据库表的工具类:ExportDB.java:
public class ExportDB {
public static void main(String[] args){
//默认读取hibernate.cfg.xml
Configuration cfg=new Configuration().configure();
SchemaExport se=new SchemaExport(cfg);
se.create(true, true);
}
}Configuration类是hibernate中专门用来读取配置文件的,SchemaExport根据配置文件来生成数据库,hbm2ddl;
当然也可以在hibernate.cfg.xml中配置<property name="hibernate.hbm2ddl.auto">update</property>,第一次会创建表(存在数据库),之后会根据实体对象关系更新表结构;这个是常用的属性,其他还有create、create-drop、validate。但还是最好使用自己创建的类来更新表。
7、创建测试:
在第6步创建了对象关系映射之后,创建一个测试类Client:
public class Client {
public static void main(String[] args){
//读取hibernate.cfg.xml
Configuration cfg=new Configuration().configure();
//创建sessionfactory
SessionFactory sf=cfg.buildSessionFactory();
//取得session
Session session=null;
try {
session=sf.openSession();
//开启事务
session.beginTransaction();
User user=new User();
user.setName("foo");
user.setPassword("123456");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
//保存
session.save(user);
//提交事务
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
//异常回滚
session.getTransaction().rollback();
}finally{
//放回连接池
if (session!=null) {
if (session.isOpen()) {
session.close();
}
}
}
}
}控制台输出:
数据库:
这样就完成了通过hibernate添加一条记录进数据库。
本文介绍Hibernate框架的基本概念,包括主要接口及对象状态,并通过实例演示如何使用Hibernate进行数据库操作。
2万+

被折叠的 条评论
为什么被折叠?



