示例代码
@Test
public void fun1(){
//1.创建, 调用空参构造函数
Configuration conf = new Configuration();
//2.读取指定配置文件
conf.configure();
//3.读取指定orm元数据(拓展)
// conf.addResource(resourceName);
// conf.addClass(persistentClass);
//根据配置信息创建SessionFactory对象
SessionFactory sessionFactory = conf.buildSessionFactory();
//4.获取Session对象
Session session = sessionFactory.openSession();
//获得一个与线程绑定的Session对象
// sessionFactory.getCurrentSession();
//5.Session获得操作事务的Transaction对象
// Transaction tx = session.getTransaction();
//开启事务并获得操作事务的transaction对象(建议使用)
Transaction tx = session.beginTransaction();
//------------------------------------------
Customer c = new Customer();
c.setCust_name("王大锤");
session.save(c);
//------------------------------------------
//提交事务
tx.commit();
//回滚事务
tx.rollback();
//释放资源
session.close();
sessionFactory.close();
}
Configuration对象
配置加载类, 用于加载主配置以及orm元数据配置
configure()方法
空参数, 默认就是加载src下的hibernate.cfg.xml配置文件
其内部就是重载的有参的configure(String), 内部指定路径为src下的hibernate.cfg.xml (指定了完整类名, 再通过反射加载配置文件)
conf.addResource()
conf.addClass()
在hibernate1中出现的远古方法, 之前的hibernate只能配置.properties文件, 所以当时要分别加载主配置和orm配置,
现在是xml配置文件方式, 主配置文件中已经引入了orm映射配置, 所以这两个方法基本用不到了.
//1.创建, 调用空参构造函数
Configuration conf = new Configuration();
//2.加载主配置文件
conf.configure();
//读取指定orm元数据(拓展)
//conf.addResource(resourceName);
//conf.addClass(persistentClass);
//3.根据配置信息创建SessionFactory对象
SessionFactory sessionFactory = conf.buildSessionFactory();
SessionFactory对象
用于创建操作数据库核心对象--Session对象的工厂, 功能就一个, 就是创建Session对象.
注意:
(1).SessionFactory负责保存和使用所有配置, 消耗内存资源非常大.
(2).SessionFactory属于线程安全对象设计(多个用户同时请求服务, 就是多线程)
综上两点, 要保证在web项目中, 只创建一个SessionFactory对象 (所有客户端共用一个SessionFactory)
//根据配置信息创建SessionFactory对象
SessionFactory sessionFactory = conf.buildSessionFactory();
Session对象
Session功能: 表达hibernate框架与数据库之间的连接(会话).
类似JDBC中的Connection对象, 还可以完成对数据库中数据的增删改查操作.
Session是Hibernate操作数据库的核心对象.
sessionFactory.openSession() 每次调用,都会获取一个不同的session对象,
sessionFactory.getCurrentSession() 每次获取的都是同一个session对象 (和ThreadLocal对象相似, 与当前线程进行了绑定)
//4.获取Session对象
Session session = sessionFactory.openSession();
//获得一个与线程绑定的Session对象
//sessionFactory.getCurrentSession();
Transaction对象
获得操作的事务对象
//5.Session获得操作事务的Transaction对象
// Transaction tx = session.getTransaction();
//开启事务并获得操作事务的transaction对象(建议使用)
Transaction tx = session.beginTransaction();
//------------------------------------------
Customer c = new Customer();
c.setCust_name("itheima");
session.save(c);
//------------------------------------------
//提交事务
tx.commit();
//回滚事务
tx.rollback();
SessionFactory封装
hibernate的增删改查操作, 有好多相同的代码.
每次操作对象都要读取配置, 创建sessionFactory, 获取Session连接, 我们可以对这些操作进行抽取, 封装成工具类.
//1.创建并读取配置文件
Configuration conf = new Configuration().configure();
//2.根据配置信息创建SessionFactory对象
SessionFactory sessionFactory = conf.buildSessionFactory();
//3.获取Session对象
Session session = sessionFactory.openSession();
SessionFactory非常消耗资源且属于线程安全对象, 一个web项目中要保证只创建一个SessionFactory对象.
因此, 把SessionFactory拿到外面, 放到静态代码块中执行, 就保证了只有一个SessionFactory
静态代码块会在类加载时执行, 类是由类加载器读取的, 类加载器会把读取的类对象缓存起来, 所以在一个虚拟机运行期间, 一个类只会被加载一次. 因此只有一个SessionFactory
package com.aisi.test.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory sessionFactory = null;
static{
// 1.创建并读取配置文件
Configuration configure = new Configuration().configure();
// 2.根据配置信息创建SessionFactory对象
sessionFactory = configure.buildSessionFactory();
}
/**
* 获取Session(每次获取一个新的Session)
* @return
*/
public static Session openSession(){
return sessionFactory.openSession();
}
/**
* 获取Session(获取线程绑定的Session)
* @return
*/
public static Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}
}