Hibernate持久层框架的使用
首先建立一个Java Project,Hibernate的测试非常简单,没有环境要求,只需要几个Hibernate的jar包就可以测试,所以Hibernate没有侵入性,称为轻量级框架.
现在将步骤列举如下,供复习使用,:
一、配置Hibernate环境
1.拷贝Hibernate需要的jar包:
*Hibernate_HOME/Hibernate.jar
*Hibernate_HOME/lib/*.jar
*MySQL jdbc驱动(这里使用MySQL作为数据库)
2.创建hibernate.cfg.xml文件,可以在Hibernate_HOME下的实例拷贝一个模板
*Hibernate是一个ORM映射框架,这个文件就是映射的桥梁.
3.配置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.url">jdbc:mysql://localhost/hibernate</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 声明MySQL方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 是否在控制台显示SQL语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 加载映射对象 -->
<mapping resource="net/knight/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.创建log4j.properties,便于调试
5.创建对象net.knight.hibernate.User.java
package net.knight.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 creatTime) {
this.createTime = creatTime;
}
public Date getExpireTime() {
return expireTime;
}
public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}
}
6.创建对象映射文件User.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>
<class name="net.knight.hibernate.User">
<id name="id">
<generator class="uuid"/>
</id>
<property name="name"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
7.因为要将对象导入到表,所以要先建立数据库,我的数据库名称叫Hibernate,可以根据自己的要求改,改完后hibernate.cfg.xml中的jdbc连接需要和你的数据库一致.
8.为了生成表,建立一个生成表工具类net.knight.hibernate.ExportDB.java
package net.knight.hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDB {
/**
* 生成表工具类
*/
public static void main(String[] args) {
// 读取配置文件hibernate.cfg.xml
// Configuration cfg = new Configuration();读取.properties文件
Configuration cfg = new Configuration().configure();
// 将对象导成表
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
9.执行这个类,打开MySQL客户端,登录
* 输入use hibernate;切换数据库
* 输入desc User;可以看到表已经建立好了.
10.写一个客户端类,在表中增加一条记录net.knight.hibernate.Client
package net.knight.hibernate;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Client {
public static void main(String args[]) {
// 读取hibernate.cfg.xml配置文件
Configuration cfg = new Configuration().configure();
// 创建SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
Session session = null;
try {
session = factory.openSession();
// 开启事务
session.beginTransaction();
User user = new User();
user.setName("张三");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
// 保存数据
session.save(user);
// 提交事务
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
// 回滚事务
session.getTransaction().commit();
}finally {
if(session != null) {
if(session.isOpen()) {
// 关闭事务
session.close();
}
}
}
}
}
11.在MySQL客户端输入select * from user;应该可以看到添加的结果.
重点:先建立对象,再建立对象和数据库的映射文件.以及Hibernate中常用的几个方法.
文件目录结构如下
Hibernate
|_____lib
| |_____*.jar
|_____net
| |_____knight
| |_______hibernate
| |________User.java
| |________User.hbm.xml
| |________ExportDB.java
| |________Client.java
|_____hibernate.cfg.xml
|_____log4j.properties