![](https://csdnimg.cn/release/download_crawler_static/10820299/bg4.jpg)
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null)
? sessionFactory.openSession(): null;
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
}
8、在 hibernate-prj2 的 cn.edu.zjut.dao 包中创建 BaseHibernateDAO.java,作为数
据库操作基础类,具体代码如下:
package cn.edu.zjut.dao;
import org.hibernate.Session;
public class BaseHibernateDAO{
public Session getSession() {
return HibernateUtil.getSession();
}
}
9
、在 hibernate-prj2 的 cn.edu.zjut.dao 包中创建 CustomerDAO.java,继承数据库
操作基础类 BaseHibernateDAO.java,实现 Customer 增删改查操作,代码片段
如下:
package cn.edu.zjut.dao;
import java.util.List;
import org.hibernate.Query;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.edu.zjut.po.Customer;
public class CustomerDAO extends BaseHibernateDAO{
private Log log = LogFactory.getLog(CustomerDAO.class);
public List findByHql(String hql) {
log.debug("finding Customer instance by hql");
try {