hibernate 操作基类

  1. package com.shop.base;
  2. import org.hibernate.*;
  3. import org.hibernate.criterion.Criterion;
  4. import org.hibernate.criterion.Example;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.SQLException;
  8. import java.util.List;
  9. import java.io.Serializable;
  10. /**
  11.  * HiberNate操作基类
  12.  * @author Administrator
  13.  *
  14.  */
  15. public abstract class BaseHibernateDao {
  16.     
  17.     /**
  18.      * 加载指定Id的对象到Session缓存中,
  19.      * 如果指定的记录不存在.则返回null.
  20.      * @param clz
  21.      *      类名
  22.      * @param id
  23.      *      表示实现这个接口的类可以序列化
  24.      * @return
  25.      *      对象
  26.      */
  27.     protected Object getObject(Class clz,Serializable id){
  28.         Object object=null;
  29.         Session session = HibernateSessionFactory.getSession();
  30.         Transaction tx = null;
  31.         try{
  32.             tx = session.beginTransaction();
  33.             //加载指定的对象
  34.             object=session.get(clz, id);
  35.             tx.commit();
  36.         }catch(Exception e){
  37.             if(tx!=null){
  38.                 tx.rollback();
  39.             }
  40.             e.printStackTrace();
  41.         }finally{
  42.             HibernateSessionFactory.closeSession();
  43.         }
  44.         return object;
  45.     }
  46.     
  47.     /**
  48.      * 加载指定Id的对象到Session缓存中,
  49.      * 如果指定的记录不存在.则抛出异常.
  50.      * @param clz
  51.      *      类名
  52.      * @param id
  53.      *      表示实现这个接口的类可以序列化 
  54.      * @return
  55.      *      对象
  56.      */
  57.     protected Object loadObject(Class clz,Serializable id){
  58.         Object object = null;
  59.         Session session = HibernateSessionFactory.getSession();
  60.         Transaction tx = null;
  61.         try {
  62.             tx = session.beginTransaction();
  63.             object = session.load(clz, id);
  64.             tx.commit();
  65.         } catch (Exception e) {
  66.             if(tx!=null){
  67.                 tx.rollback();
  68.             }
  69.             e.printStackTrace();
  70.         }finally{
  71.             
  72.         }
  73.         return object;
  74.         
  75.     }
  76.     /**
  77.      * 添加对象
  78.      * @param item
  79.      *          对象
  80.      */
  81.     protected boolean addObject(Object item){
  82.         boolean mark = false;
  83.         Session session = HibernateSessionFactory.getSession();
  84.         Transaction tx = null;
  85.         try{
  86.             tx=session.beginTransaction();
  87.             session.save(item);
  88.             tx.commit();
  89.             mark = true;
  90.         }catch(Exception e){
  91.             if(null!=tx){
  92.                 tx.rollback();
  93.             }
  94.             e.printStackTrace();
  95.         }finally{
  96.             HibernateSessionFactory.closeSession();
  97.         }
  98.         return mark;
  99.     }
  100.     
  101.     /**
  102.      * 更新对象
  103.      * @param item
  104.      *          对象
  105.      */
  106.     protected boolean updateObject(Object item){
  107.         boolean mark = false;
  108.         Session session = HibernateSessionFactory.getSession();
  109.         Transaction tx=null;
  110.         try{
  111.             tx=session.beginTransaction();
  112.             session.update(item);
  113.             tx.commit();
  114.         }catch(Exception e){
  115.             if(null!=null){
  116.                 tx.rollback();
  117.             }
  118.             e.printStackTrace();
  119.         }finally{
  120.             HibernateSessionFactory.closeSession();
  121.         }
  122.         return mark;
  123.     }
  124.     /**
  125.      * 执行SQL语句,执行非查询操作
  126.      * @param sql
  127.      *          sql语句
  128.      */
  129.     protected boolean updateObjectSQL(String sql){
  130.         boolean mark = false;
  131.         Connection connection = null;
  132.         try{
  133.             connection = HibernateSessionFactory.getSession().connection();
  134.             PreparedStatement pstm = connection.prepareStatement(sql);
  135.             pstm.executeUpdate();
  136.             mark=true;
  137.         }catch(Exception e){
  138.             e.printStackTrace();
  139.         }finally{
  140.             try {
  141.                 connection.close();
  142.             } catch (SQLException e) {
  143.                 e.printStackTrace();
  144.             }
  145.         }
  146.         return mark;
  147.     }
  148.     
  149.     /**
  150.      * 删除对象
  151.      * @param clz
  152.      *      类
  153.      * @param id
  154.      *      表示实现这个接口的类可以序列化
  155.      */
  156.     protected boolean deleteObject(Class clz,Serializable id){
  157.         boolean mark = false;
  158.         Transaction tx = null;
  159.         Session session = HibernateSessionFactory.getSession();
  160.         try{
  161.             tx=session.beginTransaction();
  162.             //启用getOjbect()方法,装载指定ID的对象到内存中
  163.             session.delete(this.getObject(clz, id));
  164.             tx.commit();
  165.         }catch(Exception e){
  166.             if(null!=tx){
  167.                 tx.rollback();
  168.             }
  169.             e.printStackTrace();
  170.         }finally{
  171.             HibernateSessionFactory.closeSession();
  172.         }
  173.         return mark;
  174.     }
  175.     
  176.     /**
  177.      * 动态查询(QBC查询)
  178.      * @param clz
  179.      *      要查询的类
  180.      * @param condition
  181.      *      查询类的条件
  182.      * @return
  183.      * 
  184.      *      Example接口的静态方法create()创建一个Criterion对象,它代表
  185.      *      按照样板对象的属性来比较查询条件.不常用.一个典型的应用是在查询窗口中
  186.      *      让用户输入一系列的查询条件.然后返回匹配对象.
  187.      *      把所有不为null值的condition作为查询条件
  188.      */
  189.     protected List searchObject(Class clz,Object condition){
  190.         List results = null;
  191.         Session session = HibernateSessionFactory.getSession();
  192.         Transaction tx = null;
  193.         try{
  194.              tx=session.beginTransaction();
  195.              results=session.createCriteria(clz)//QBC查询
  196.                     .add(Example.create(condition))
  197.                     .list();
  198.              tx.commit();
  199.             //Example接口的静态方法create()创建一个Criterion对象,它代表
  200.             //按照样板对象的属性来比较查询条件.不常用.一个典型的应用是在查询窗口中
  201.             //让用户输入一系列的查询条件.然后返回匹配对象.
  202.         }catch(RuntimeException re){
  203.             if(null!=tx){
  204.                 tx.rollback();
  205.             }
  206.             re.printStackTrace();
  207.         }finally{
  208.             HibernateSessionFactory.closeSession();
  209.         }
  210.         return results;
  211.     }
  212.     
  213.     /**
  214.      * QBC查询
  215.      * @param clz
  216.      *      要查询的类
  217.      * @param criterion
  218.      *      查询条件
  219.      * @return
  220.      */
  221.     protected List searchObjectQBC(Class clz,Criterion criterion){
  222.         List results = null;
  223.         Session session = HibernateSessionFactory.getSession();
  224.         Transaction tx = null;
  225.         try{
  226.             tx=session.beginTransaction();
  227.             results=session.createCriteria(clz)
  228.                     .add(criterion)
  229.                     .list();
  230.             tx.commit();
  231.         }catch(RuntimeException re){
  232.             if(null!=tx){
  233.                 tx.rollback();
  234.             }
  235.             re.printStackTrace();
  236.         }finally{
  237.             HibernateSessionFactory.closeSession();
  238.         }
  239.         return results;
  240.     }
  241.     
  242.     /**
  243.      * HQL查询
  244.      * @param hql
  245.      *      HQL查询语句
  246.      * @return
  247.      */
  248.     protected List searchObjectHQL(String hql){
  249.         List results = null;
  250.         Session session=HibernateSessionFactory.getSession();
  251.         Transaction tx= null;
  252.         try {
  253.             tx=session.beginTransaction();
  254.             Query query = session.createQuery(hql);
  255.             results=query.list();
  256.             tx.commit();
  257.         } catch (Exception e) {
  258.             if(null!=tx){
  259.                 tx.rollback();
  260.             }
  261.             e.printStackTrace();
  262.         }finally{
  263.             HibernateSessionFactory.closeSession();         
  264.         }
  265.         return results;
  266.     }
  267.     /**
  268.      * HQL查询
  269.      * @param hql
  270.      *      HQL查询语句
  271.      *      带条件参数
  272.      * @return
  273.      */
  274.     protected List searchObjectHQLWithParam(String hql,Object[] conditions){
  275.         List results = null;
  276.         Session session=HibernateSessionFactory.getSession();
  277.         Transaction tx= null;
  278.         try {
  279.             tx=session.beginTransaction();
  280.             Query query = session.createQuery(hql);
  281.     
  282.                 for (int i = 0; i < conditions.length; i++) {
  283.                     query.setParameter(i, conditions[i]);
  284.                 }
  285.             
  286.             results=query.list();
  287.             tx.commit();
  288.         } catch (Exception e) {
  289.             if(null!=tx){
  290.                 tx.rollback();
  291.             }
  292.             e.printStackTrace();
  293.         }finally{
  294.             HibernateSessionFactory.closeSession();         
  295.         }
  296.         return results;
  297.     }
  298.     
  299.     /**
  300.      * SQL查询
  301.      * @param sql
  302.      *      SQL查询语句
  303.      * @return
  304.      */ 
  305.     protected  List searchObjectSQL(String sql) {
  306.         List results = null;
  307.         Session session=HibernateSessionFactory.getSession();
  308.         Transaction tx= null;
  309.         try {
  310.             tx=session.beginTransaction();
  311.             Query query = session.createSQLQuery(sql);
  312.             results=query.list();
  313.             tx.commit();
  314.         } catch (Exception e) {
  315.             if(null!=tx){
  316.                 tx.rollback();
  317.             }
  318.             e.printStackTrace();
  319.         }finally{
  320.             HibernateSessionFactory.closeSession();         
  321.         }
  322.         return results;     
  323.     }
  324.     
  325.     /**
  326.      * 执行hql语句,得到单个值
  327.      * @param hql
  328.      *      hql语句
  329.      * @return
  330.      */
  331.     protected Object getUniqueObjectHQL(String hql,Object[] conditions ){
  332.         Object results = null;
  333.         Session session=HibernateSessionFactory.getSession();
  334.         Transaction tx= null;
  335.         try {
  336.             tx=session.beginTransaction();
  337.             Query query = session.createQuery(hql);
  338.             
  339.             for (int i = 0; i < conditions.length; i++) {
  340.                 query.setParameter(i, conditions[i]);
  341.             }       
  342.             
  343.             results=query.uniqueResult();
  344.             tx.commit();
  345.         } catch (Exception e) {
  346.             if(null!=tx){
  347.                 tx.rollback();
  348.             }
  349.             e.printStackTrace();
  350.         }finally{
  351.             HibernateSessionFactory.closeSession();         
  352.         }
  353.         return results; 
  354.     }
  355.     
  356.     /**
  357.      * 执行sql语句,得到单个值
  358.      * @param sql
  359.      *      sql语句
  360.      * @return
  361.      */
  362.     protected Object getUniqueObjectSQL(String sql){
  363.         Object results = null;
  364.         Session session=HibernateSessionFactory.getSession();
  365.         Transaction tx= null;
  366.         try {
  367.             tx=session.beginTransaction();
  368.             Query query = session.createSQLQuery(sql);
  369.             results=query.uniqueResult();
  370.             tx.commit();
  371.         } catch (Exception e) {
  372.             if(null!=tx){
  373.                 tx.rollback();
  374.             }
  375.             e.printStackTrace();
  376.         }finally{
  377.             HibernateSessionFactory.closeSession();         
  378.         }
  379.         return results; 
  380.     }
  381. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值