Hibernate简易BaseDao演示单例模式和线程池

 
 
package com . dao ;

import java.util.List ;

import org.hibernate.HibernateException ;
import org.hibernate.Session ;
import org.hibernate.SessionFactory ;
import org.hibernate.Transaction ;
import org.hibernate.cfg.Configuration ;

import com.IConst.IConst ;

public class BaseDao {
private static Configuration cf ;
private static SessionFactory sf ;
/* 实例化线程池 */
private ThreadLocal < Session > thread = new ThreadLocal < Session >();

static {
init ();
}

/*
* 单例模式
* */
private static synchronized void init () {
try {
if ( null == cf || null == sf ) {
cf = new Configuration (). configure ();
sf = cf . buildSessionFactory ();
}
} catch ( HibernateException e ) {
e . printStackTrace ();
}
}

/*
* open方法
* */
private void open () throws Exception {
/* 先从线程池中取 */
Session session = thread . get ();
/* 取不到,利用工厂新建 */
if ( null == session ) {
session = sf . openSession ();
/* 将新建的session放入线程池 */
thread . set ( session );
}
}

/*
* close方法
* */
private void close () {
try {
/* 从线程池中取 */
Session session = thread . get ();
/* 取到,关闭 */
if ( null != session ) {
session . close ();
/* 清空线程池 */
thread . set ( null );
}
} catch ( HibernateException e ) {
e . printStackTrace ();
}
}

/*
* 执行增\删\改
* */
public boolean update ( Object obj , int type ) {
Session session = null ;
Transaction tran = null ;
try {
open ();

/* 取出session */
session = thread . get ();

/* 打开事务 */
tran = session . beginTransaction ();
switch ( type ) {
case IConst . DELETE :
session . delete ( obj );
break ;

case IConst . INSERT :
session . save ( obj );
break ;

case IConst . UPDATE :
session . update ( obj );
break ;
}

/* Hibernate的事务机制是先将其保存在缓存中 */
/* 只有commit以后,才将事务提交 */
/* 提交事务 */
tran . commit ();
return true ;
} catch ( Exception e ) {
if ( null != tran ) {
tran . rollback ();
}
e . printStackTrace ();
} finally {
close ();
}
return false ;
}

/*
* 查询方法
* */
public List select ( String sql ) {
Session session = null ;
try {
open ();
session = thread . get ();
return session . createQuery ( sql ). list ();
} catch ( Exception e ) {
e . printStackTrace ();
} finally {
close ();
}
return null ;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值