1.Configuration:Hibernate的配置对象
Configuration 类的作用是对Hibernate 进行配置,以及对它进行启动。在Hibernate 的启动过程中,Configuration 类的实例首先定位映射文档的位置,读取这些配置,然后创建一个SessionFactory对象。虽然Configuration 类在整个Hibernate 项目中只扮演着一个很小的角色,但它是启动hibernate 时所遇到的第一个对象。
作用:
- 加载核心配置文件
- hibernate.properties
Configuration cfg = new Configuration(); - hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
- hibernate.properties
- 加载映射文件
// 手动加载映射
configuration.addResource(“day01/Customer.hbm.xml”);
2.SessionFactory
SessionFactory接口负责初始化Hibernate。它充当数据存储源的代理,并负责创建Session对象。这里用到了工厂模式。需要注意的是SessionFactory并不是轻量级的,因为一般情况下,一个项目通常只需要一个SessionFactory就够,当需要操作多个数据库时,可以为每个数据库指定一个SessionFactory。
SessionFactory内部维护了Hibernate的连接池和Hibernate的二级缓存。是线程安全的对象。一个项目创建一个对象即可。
配置连接池:
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!--在连接池中可用的数据库连接的最少数目 -->
<property name="c3p0.min_size">5</property>
<!--在连接池中所有数据库连接的最大数目 -->
<property name="c3p0.max_size">20</property>
<!--设定数据库连接的过期时间,以秒为单位,
如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
<property name="c3p0.timeout">120</property>
<!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
<property name="c3p0.idle_test_period">3000</property>
抽取工具类
package hibernateUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/*
*
* Hibernate工具类
*
*
*/
public class HibernateUtils {
public static final Configuration cfg;
public static final SessionFactory sf;
static {
cfg=new Configuration().configure();
sf=cfg.buildSessionFactory();
}
public static Session openSession() {
return sf.openSession();
}
}
3.Session
Session接口负责执行被持久化对象的CRUD操作(CRUD的任务是完成与数据库的交流,包含了很多常见的SQL语句)。但需要注意的是Session对象是非线程安全的。同时,Hibernate的session不同于JSP应用中的HttpSession。这里当使用session这个术语时,其实指的是Hibernate中的session,而以后会将HttpSession对象称为用户session。
Session代表的是Hibernate与数据库的链接对象。不是线程安全的。与数据库交互桥梁。
Session中的API
- 保存方法:
Serializable save(Object obj); - 查询方法:
T get(Class c,Serializable id);
T load(Class c,Serializable id);
get方法和load方法的区别? - 修改方法
void update(Object obj); - 删除方法
void delete(Object obj);s - 保存或更新
void saveOrUpdate(Object obj) - 查询所有
package day01;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import hibernateUtils.HibernateUtils;
/*
* Hibernate的工具类测试
*
*
*/
public class HibernateDemo2 {
@Test
public void demo1(){
//Session session = HibernateUtils.openSession();
//Transaction tx = session.beginTransaction();
//使用get方法查询
//Customer customer = session.get(Customer.class, 1l);
//System.out.println(customer);
//使用load查询
//Customer customer = session.get(Customer.class, 2l);
//System.out.println(customer);
/*
* get方法
* 采用的是立即加载,执行到这个代码时,就会马上发送SQL语句去查询
* 查询后返回真实对象本身
* 查询一个找不到的对象时,返回null
*
* load方法
* 采用延迟加载(lazy懒加载,执行到这个代码时,不会发送sql语句,当真正使用这个对象时才会发送SQL语句)
* 查询后返回的是代理对象,利用javassist技术产生的代理
* 查询一个找不到的对象的时候,返回ObjectNotFounfdException
*
*
*/
/*Customer customer = new Customer();
customer.setCust_name("张一");
Serializable id = session.save(customer);
System.out.println(id);
*/
//tx.commit();
//session.close();
}
@Test
//修改操作
public void demo3() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//直接创建,进行修改
/*Customer customer = new Customer();
customer.setCust_id(1l);
customer.setCust_name("张小敬");
session.update(customer);
*/
//先查询,再修改
//Customer customer1 = session.get(Customer.class, 1l);
//customer1.setCust_name("张大大");
//session.update(customer1);
tx.commit();
session.close();
}
@Test
//删除操作
public void demo4() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//直接创建,删除
/*Customer customer = new Customer();
customer.setCust_id(1l);
session.delete(customer);
*/
//先查询再删除--级联删除
//Customer customer = session.get(Customer.class, 2l);
//session.delete(customer);
tx.commit();
session.close();
}
@Test
//保存或更新
public void demo5() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
/*Customer customer = new Customer();
customer.setCust_name("王八");
session.saveOrUpdate(customer);
*/
Customer customer = new Customer();
customer.setCust_id(3l);
customer.setCust_name("如花话");
session.saveOrUpdate(customer);
tx.commit();
session.close();
}
@Test
public void demo6() {
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//接受HQL:Hibernate Query Language 面向对象的查询语言
/*Query query = session.createQuery("from Customer");
List<Customer> list = query.list();
for(Customer customer : list) {
System.out.println(customer);
}
*/
//接收SQL
SQLQuery query = session.createSQLQuery("select * from cst_customer");
List<Object[]> list = query.list();
for(Object [] objects : list) {
System.out.println(Arrays.toString(objects));
}
tx.commit();
session.close();
}
}