[Hibernate学习第一课]入门案例

时间:2022-03-22 22:01:30

开发流程:
1.创建一个项目,项目结构如图所示:
[Hibernate学习第一课]入门案例
2.引入hibernate开发包,所需要的jar包如图黑色框框所示,这是精简过后的jar包。
3.在数据库中创建表:employee,这里以oralce数据库为例。

创建employe 表.
create table employee(
id number primary key,
name varchar2(64) not null,
email varchar2(64) not null,
hiredate date not null)

创建一个序列,将来用于主键的自动增长.

--创建一个序列
create sequence emp_seq
start with 1
increment by 1
minvalue 1
nomaxvalue
nocycle
nocache

4.开发domain对象和对象关系映射文件

对象关系映射文件: 作用是用于指定 domain对象和表的映射关系. ,该文件的取名有规范:
domain对象.hbm.xml,一般我们放在 和domain对象同一个文件夹下(包下)

<?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">


<!-- package : 表示该类在哪个包下 -->
<hibernate-mapping package="com.shizongger.domain">
<!-- name : 表示类名 table 表示 该类和哪个表映射 -->
<class name="Employee" table="employee">
<id name="id" type="java.lang.Integer">
<!-- 主键生成策略 -->
<generator class="sequence">
<param name="sequence">emp_sq</param>
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" not-null="true" />
</property>
<property name="email" type="java.lang.String">
<column name="email" not-null="true"/>
</property>
<property name="hiredate" type="java.util.Date">
<column name="hiredate" not-null="true"></column>
</property>
</class>
</hibernate-mapping>

5.手动配置我们的hibernate.cfg.xml文件,该文件用于配置 连接的数据库的类型,driver,
,用户名,密码 ,url ….同时管理 对象关系映射文件 ,该文件的名称,我们一般不修改.

<?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">

<!-- 该文件用于配置连接数据的种类,用户名,密码,ul ,驱动.. 连接池,二级缓存.. 有点类似strus struts-config.xml -->
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
<property name="connection.username">scott</property>
<property name="connection.password">mypasswd</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/shizongger/domain/Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>

pojo类如下:

package com.shizongger.domain;

import java.io.Serializable;
import java.util.Date;

public class Employee implements Serializable {
private static final long serialVersionUID = -1L;

private Integer id;
private String name;
private String email;
private Date hiredate;

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
}

这里pojo类要实现序列化.

测试类,这是一个main方法,相当于mvc中的view层。

package com.shizongger.view;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.shizongger.domain.Employee;

public class TestHibernate {

public static void main(String[] args) {
// addEmployee();
// updateEmp();
delEmp();
}

//添加一个雇员
private static void addEmployee() {
//1.得到Configuration
Configuration configuration = new Configuration().configure();
//2.得到SessionFactory(会话工厂,这是一个重量级的类,因此要保证在一个应用程序中只能有一个)
SessionFactory sessionFactory = configuration.buildSessionFactory();
//3.从sessionFactory中取出一个Session对象(它表示和数据库的一次会话)
Session session = sessionFactory.openSession();
//4.开始一个事务
Transaction transaction = session.beginTransaction();
//保存一个对象到数据库(持久化一个对象)
Employee emp = new Employee();
emp.setEmail("lily@qq.com");
emp.setHiredate(new Date());
emp.setName("lily");
//这里不必给出id值,因为它是自增的
session.save(emp);
transaction.commit();
session.close();
}

//更新用户
private static void updateEmp() {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Employee emp = (Employee)session.load(Employee.class, 6);
emp.setName("shizongger");
emp.setEmail("shizongger@qq.com");
transaction.commit();
session.close();
}

//删除用户
private static void delEmp() {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction ts = session.beginTransaction();
Employee emp = (Employee)session.load(Employee.class, 6);
session.delete(emp);
ts.commit();
session.close();
}
}

对于重量级的类SessionFactroy,我们只确保只能有一个,所以在工具类中获取一次sessionFacotrory.

package com.shizongger.utils;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

final public class MySessionFactory {
private static SessionFactory sessionFactory = null;
private static Configuration configuration = null;

static{
configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
}

private MySessionFactory() {
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

注:本博文同时在我的chinaunix博客更新:http://blog.chinaunix.net/uid/29917103.html