Hibernate
典型的三层架构是表示层,业务层,持久层!Hibernate是持久层的框架,它的底层主要是对JDBC的封装。
下面是关于如何搭建一个Hibernate框架的步骤
配置Hibernate的步骤
1导包
将hibernate-release-4.3.11.Final\lib\required路径下的全部压缩包及所使用的数据库驱动包导入工程。
建立hibernate.cfg.xml文件,将该文件建在src根目录下;
2.1:拷一个配置模板:路径:
hibernate-release-4.3.11.Final/documentation/manual/en-US/html/ch01.html#tutorial-firstapp-configuration
示例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings数据库连接 -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:XE</property>
<property name="connection.username">HQ133</property>
<property name="connection.password">HQ133</property>
<!-- JDBC connection pool (use the built-in)JDBC 连接池:Hibernate内置的连接池 -->
<property name="connection.pool_size">20</property>
<!-- SQL dialect根据不同的数据库选择不同的方言 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Enable Hibernate's automatic session context management获取当前线程的Session对象 -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout 执行hibernate过程中,将执行的sql语句显示到Console上 -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup 加载 对象与表的映射文件-->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/iotek/pojo/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3创建一个Pojo包:建一个实体类(要实现Serializable接口):
public class Student implements Serializable {
private Long id;
private String name; //attribute
private String address;
private String gender;
private int age;
public Student(){}
public Student(Long id, String name, String address, String gender, Integer age) {
super();
this.id = id;
this.name = name;
this.address = address;
this.gender = gender;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
.............
此处有省略。。。。。。。。
..............
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", address=" + address + ", gender=" + gender + ", age=" + age
+ "]";
}
}
4建立映射文件配置:userinfo.hbm.xml
模板位置:
hibernate-release-4.3.11.Final/documentation/manual/en-US/html/ch01.html#tutorial-firstapp-mapping
示例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.iotek.pojo">
<!-- name:代表索要映射的类名 table:代表索要映射的表名 -->
<class name="Student" table="STUDENT">
<!-- name:代表索要映射的类中id的属性名 table:代表索要映射的表ID列 -->
<id name="id" column="ID" type="long">
<!-- <generator class="sequence">
<param name="sequence">student_seq</param>
</generator> -->
</id>
<property name="name" type="string" column="NAME"/>
<property name="address" type="string" column="ADDRESS"/>
<property name="gender" type="string" column="GENDER"/>
<property name="age" type="int" column="AGE"/>
</class>
</hibernate-mapping>
5建立SessionFactory
package com.iotek.session;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateSessionFactory {
//static ThreadLocal<Session> local = new ThreadLocal<Session>();
private static SessionFactory factory = null;
private static Session session = null;
static{
//--Hibernate 3.X之前版本
/*Configuration config = new Configuration();
config.configure("hibernate.cfg.xml");
factory = config.buildSessionFactory();*/
//--Hibernate 3.X之后版本
Configuration config = new Configuration();
config.configure("hibernate.cfg.xml");
ServiceRegistryBuilder builder = new ServiceRegistryBuilder();
builder.applySettings(config.getProperties());
ServiceRegistry service = builder.buildServiceRegistry();
factory = config.buildSessionFactory(service);
}
private HibernateSessionFactory(){}
public static Session getSession(){
//--Session:第一种获得方式
//session = factory.openSession();
//--Session:第二种获得方式
/*
session = local.get();
if(session==null){
session = factory.openSession();
local.set(session);
}*/
//--Session:第三种获得方式
session = factory.getCurrentSession();
return session;
}
public static void closeSession(){
//--第一种优化方式的:关闭
/*session = local.get();
if(session!=null){
if(session.isOpen()){
session.close();
local.set(null);
}
}*/
if(session!=null){
if(session.isOpen()){
session.close();
}
}
}
}
第六步:构建测试类:
public class Demo {
public static void main(String[] args) {
//--可以操作数据库的对象
Session session = HibernateSessionFactory.getSession();
/*
Student stu = new Student("小明",55);
//--创建操作Session的事务
Transaction tran = null;
//--开启session中的事务
tran = session.beginTransaction();
//--对数据的保存
session.save(stu);
//--提交事务
tran.commit();*/
Transaction tran = null;
tran = session.beginTransaction();
Student stu1 = (Student)session.get(Student.class, 2);
tran.commit();
System.out.println(stu1);
// HibernateSessionFactory.closeSession();
}
}