- package com.shop.base;
- import org.hibernate.*;
- import org.hibernate.criterion.Criterion;
- import org.hibernate.criterion.Example;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.List;
- import java.io.Serializable;
- /**
- * HiberNate操作基类
- * @author Administrator
- *
- */
- public abstract class BaseHibernateDao {
- /**
- * 加载指定Id的对象到Session缓存中,
- * 如果指定的记录不存在.则返回null.
- * @param clz
- * 类名
- * @param id
- * 表示实现这个接口的类可以序列化
- * @return
- * 对象
- */
- protected Object getObject(Class clz,Serializable id){
- Object object=null;
- Session session = HibernateSessionFactory.getSession();
- Transaction tx = null;
- try{
- tx = session.beginTransaction();
- //加载指定的对象
- object=session.get(clz, id);
- tx.commit();
- }catch(Exception e){
- if(tx!=null){
- tx.rollback();
- }
- e.printStackTrace();
- }finally{
- HibernateSessionFactory.closeSession();
- }
- return object;
- }
- /**
- * 加载指定Id的对象到Session缓存中,
- * 如果指定的记录不存在.则抛出异常.
- * @param clz
- * 类名
- * @param id
- * 表示实现这个接口的类可以序列化
- * @return
- * 对象
- */
- protected Object loadObject(Class clz,Serializable id){
- Object object = null;
- Session session = HibernateSessionFactory.getSession();
- Transaction tx = null;
- try {
- tx = session.beginTransaction();
- object = session.load(clz, id);
- tx.commit();
- } catch (Exception e) {
- if(tx!=null){
- tx.rollback();
- }
- e.printStackTrace();
- }finally{
- }
- return object;
- }
- /**
- * 添加对象
- * @param item
- * 对象
- */
- protected boolean addObject(Object item){
- boolean mark = false;
- Session session = HibernateSessionFactory.getSession();
- Transaction tx = null;
- try{
- tx=session.beginTransaction();
- session.save(item);
- tx.commit();
- mark = true;
- }catch(Exception e){
- if(null!=tx){
- tx.rollback();
- }
- e.printStackTrace();
- }finally{
- HibernateSessionFactory.closeSession();
- }
- return mark;
- }
- /**
- * 更新对象
- * @param item
- * 对象
- */
- protected boolean updateObject(Object item){
- boolean mark = false;
- Session session = HibernateSessionFactory.getSession();
- Transaction tx=null;
- try{
- tx=session.beginTransaction();
- session.update(item);
- tx.commit();
- }catch(Exception e){
- if(null!=null){
- tx.rollback();
- }
- e.printStackTrace();
- }finally{
- HibernateSessionFactory.closeSession();
- }
- return mark;
- }
- /**
- * 执行SQL语句,执行非查询操作
- * @param sql
- * sql语句
- */
- protected boolean updateObjectSQL(String sql){
- boolean mark = false;
- Connection connection = null;
- try{
- connection = HibernateSessionFactory.getSession().connection();
- PreparedStatement pstm = connection.prepareStatement(sql);
- pstm.executeUpdate();
- mark=true;
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- try {
- connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return mark;
- }
- /**
- * 删除对象
- * @param clz
- * 类
- * @param id
- * 表示实现这个接口的类可以序列化
- */
- protected boolean deleteObject(Class clz,Serializable id){
- boolean mark = false;
- Transaction tx = null;
- Session session = HibernateSessionFactory.getSession();
- try{
- tx=session.beginTransaction();
- //启用getOjbect()方法,装载指定ID的对象到内存中
- session.delete(this.getObject(clz, id));
- tx.commit();
- }catch(Exception e){
- if(null!=tx){
- tx.rollback();
- }
- e.printStackTrace();
- }finally{
- HibernateSessionFactory.closeSession();
- }
- return mark;
- }
- /**
- * 动态查询(QBC查询)
- * @param clz
- * 要查询的类
- * @param condition
- * 查询类的条件
- * @return
- *
- * Example接口的静态方法create()创建一个Criterion对象,它代表
- * 按照样板对象的属性来比较查询条件.不常用.一个典型的应用是在查询窗口中
- * 让用户输入一系列的查询条件.然后返回匹配对象.
- * 把所有不为null值的condition作为查询条件
- */
- protected List searchObject(Class clz,Object condition){
- List results = null;
- Session session = HibernateSessionFactory.getSession();
- Transaction tx = null;
- try{
- tx=session.beginTransaction();
- results=session.createCriteria(clz)//QBC查询
- .add(Example.create(condition))
- .list();
- tx.commit();
- //Example接口的静态方法create()创建一个Criterion对象,它代表
- //按照样板对象的属性来比较查询条件.不常用.一个典型的应用是在查询窗口中
- //让用户输入一系列的查询条件.然后返回匹配对象.
- }catch(RuntimeException re){
- if(null!=tx){
- tx.rollback();
- }
- re.printStackTrace();
- }finally{
- HibernateSessionFactory.closeSession();
- }
- return results;
- }
- /**
- * QBC查询
- * @param clz
- * 要查询的类
- * @param criterion
- * 查询条件
- * @return
- */
- protected List searchObjectQBC(Class clz,Criterion criterion){
- List results = null;
- Session session = HibernateSessionFactory.getSession();
- Transaction tx = null;
- try{
- tx=session.beginTransaction();
- results=session.createCriteria(clz)
- .add(criterion)
- .list();
- tx.commit();
- }catch(RuntimeException re){
- if(null!=tx){
- tx.rollback();
- }
- re.printStackTrace();
- }finally{
- HibernateSessionFactory.closeSession();
- }
- return results;
- }
- /**
- * HQL查询
- * @param hql
- * HQL查询语句
- * @return
- */
- protected List searchObjectHQL(String hql){
- List results = null;
- Session session=HibernateSessionFactory.getSession();
- Transaction tx= null;
- try {
- tx=session.beginTransaction();
- Query query = session.createQuery(hql);
- results=query.list();
- tx.commit();
- } catch (Exception e) {
- if(null!=tx){
- tx.rollback();
- }
- e.printStackTrace();
- }finally{
- HibernateSessionFactory.closeSession();
- }
- return results;
- }
- /**
- * HQL查询
- * @param hql
- * HQL查询语句
- * 带条件参数
- * @return
- */
- protected List searchObjectHQLWithParam(String hql,Object[] conditions){
- List results = null;
- Session session=HibernateSessionFactory.getSession();
- Transaction tx= null;
- try {
- tx=session.beginTransaction();
- Query query = session.createQuery(hql);
- for (int i = 0; i < conditions.length; i++) {
- query.setParameter(i, conditions[i]);
- }
- results=query.list();
- tx.commit();
- } catch (Exception e) {
- if(null!=tx){
- tx.rollback();
- }
- e.printStackTrace();
- }finally{
- HibernateSessionFactory.closeSession();
- }
- return results;
- }
- /**
- * SQL查询
- * @param sql
- * SQL查询语句
- * @return
- */
- protected List searchObjectSQL(String sql) {
- List results = null;
- Session session=HibernateSessionFactory.getSession();
- Transaction tx= null;
- try {
- tx=session.beginTransaction();
- Query query = session.createSQLQuery(sql);
- results=query.list();
- tx.commit();
- } catch (Exception e) {
- if(null!=tx){
- tx.rollback();
- }
- e.printStackTrace();
- }finally{
- HibernateSessionFactory.closeSession();
- }
- return results;
- }
- /**
- * 执行hql语句,得到单个值
- * @param hql
- * hql语句
- * @return
- */
- protected Object getUniqueObjectHQL(String hql,Object[] conditions ){
- Object results = null;
- Session session=HibernateSessionFactory.getSession();
- Transaction tx= null;
- try {
- tx=session.beginTransaction();
- Query query = session.createQuery(hql);
- for (int i = 0; i < conditions.length; i++) {
- query.setParameter(i, conditions[i]);
- }
- results=query.uniqueResult();
- tx.commit();
- } catch (Exception e) {
- if(null!=tx){
- tx.rollback();
- }
- e.printStackTrace();
- }finally{
- HibernateSessionFactory.closeSession();
- }
- return results;
- }
- /**
- * 执行sql语句,得到单个值
- * @param sql
- * sql语句
- * @return
- */
- protected Object getUniqueObjectSQL(String sql){
- Object results = null;
- Session session=HibernateSessionFactory.getSession();
- Transaction tx= null;
- try {
- tx=session.beginTransaction();
- Query query = session.createSQLQuery(sql);
- results=query.uniqueResult();
- tx.commit();
- } catch (Exception e) {
- if(null!=tx){
- tx.rollback();
- }
- e.printStackTrace();
- }finally{
- HibernateSessionFactory.closeSession();
- }
- return results;
- }
- }