原理
package com.hsp.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
//在使用hibernate开发项目,请一定保证只有一个SessionFactory
//一个数据库对应一个SessionFactory 对象.
final public class MySessionFactory {
private static SessionFactory sessionFactory=null;
private MySessionFactory(){
}
static{
//地址可以自己写
sessionFactory =new Configuration().configure("com/hsp/config/hsp.cfg.xml").buildSessionFactory();
System.out.println("sessionFactory 类型"+sessionFactory);
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
1、configuration
Configuraion类
①负责管理hibernate的配置信息
②读取hibernate.cfg.xml
③加载hibernate.cfg.xml配置文件中
配置的驱动,url,用户名,密码,连接池.
④管理 *.hbm.xml对象关系文件.
2、hibernate.cfg.xml文件
①该文件主要用于指定各个参数,是hibernate核心文件
②默认放在src目录下,也可以放在别的目录下。
③指定连接数据库的驱动、用户名、密码、url、连接池..
④指定对象关系映射文件的位置.
⑤也可使用hibernate.properties文件来替代该文件.(推荐使用
hibernate.cfg.xml)。
3、hbm.xml
4、sessionFactory
openSession()获取一个新的对象,通过地址hash值可以得知不同
getCurrentSession()获取当前线程绑定的 session ,有利于事物管理
用load时getCurrentSession需要事务Transaction
■ get()和load()区别
1、get()方法直接返回实体类,如果查不到数据则返回null。load()会返回一个实体代理对象(当前这个对象可以自动转化为实体对象),但当代理对象被调用时,如果没有数据不存在,就会抛出org.hibernate.ObjectNotFoundException异常
2.load先到缓存(session缓存/二级缓存)中去查,如果没有则返回一个代理对象(不马上到DB中去找),等后面使用这个代理对象操作的时候,才到DB中查询,这就是我们常说的 load在默认情况下支持延迟加载(lazy)
3. get先到缓存(session缓存/二级缓存)中去查,如果没有就到DB中去查(即马上发出sql)。总之,如果你确定DB中有这个对象就用
load(),不确定就用get()(这样效率高)
懒加载
线程局部变量模式
变量和线程关联作用起来
package com.hsp.util;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
final public class HibernateUtil {
private static SessionFactory sessionFactory=null;
//使用线程局部模式
private static ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();
private HibernateUtil(){};
static {
sessionFactory=new Configuration().configure().buildSessionFactory();
}
//获取全新的全新的sesession
public static Session openSession(){
return sessionFactory.openSession();
}
//获取和线程关联的session
public static Session getCurrentSession(){
Session session=threadLocal.get();
//判断是否得到
if(session==null){
session=sessionFactory.openSession();
//把session对象设置到 threadLocal,相当于该session已经和线程绑定
threadLocal.set(session);
}
return session;
}
//统一的一个修改和删除(批量 hql) hql"delete upate ...??"
public static void executeUpdate(String hql,String [] parameters){
Session s=null;
Transaction tx=null;
try {
s=openSession();
tx=s.beginTransaction();
Query query=s.createQuery(hql);
//先判断是否有参数要绑定
if(parameters!=null&& parameters.length>0){
for(int i=0;i<parameters.length;i++){
query.setString(i, parameters[i]);
}
}
query.executeUpdate();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
// TODO: handle exception
}finally{
if(s!=null&&s.isOpen()){
s.close();
}
}
}
//统一的添加的方法
public static void save(Object obj){
Session s=null;
Transaction tx=null;
try {
s=openSession();
tx=s.beginTransaction();
s.save(obj);
tx.commit();
} catch (Exception e) {
if(tx!=null){
tx.rollback();
}
throw new RuntimeException(e.getMessage());
// TODO: handle exception
}finally{
if(s!=null && s.isOpen()){
s.close();
}
}
}
//提供一个统一的查询方法(带分页) hql 形式 from 类 where 条件=? ..
public static List executeQueryByPage(String hql,String [] parameters,int pageSize,int pageNow){
Session s=null;
List list=null;
try {
s=openSession();
Query query=s.createQuery(hql);
//先判断是否有参数要绑定
if(parameters!=null&& parameters.length>0){
for(int i=0;i<parameters.length;i++){
query.setString(i, parameters[i]);
}
}
query.setFirstResult((pageNow-1)*pageSize).setMaxResults(pageSize);
list=query.list();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
// TODO: handle exception
}finally{
if(s!=null&&s.isOpen()){
s.close();
}
}
return list;
}
//提供一个统一的查询方法 hql 形式 from 类 where 条件=? ..
public static List executeQuery(String hql,String [] parameters){
Session s=null;
List list=null;
try {
s=openSession();
Query query=s.createQuery(hql);
//先判断是否有参数要绑定
if(parameters!=null&& parameters.length>0){
for(int i=0;i<parameters.length;i++){
query.setString(i, parameters[i]);
}
}
list=query.list();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
// TODO: handle exception
}finally{
if(s!=null&&s.isOpen()){
s.close();
}
}
return list;
}
}