Hibernate.gethibernatetemplate 实现增、删、改、查 方法

  1. 1. find(String hql);  //普通查询

       示例:this.gethibernateTemplate().find("from User");

     

    2. find(String hql,Object value);//一个查询条件

       示例:this.gethibernateTemplate().find("from User u where u.name=?","test");

     

    3. find(String hql,Object[] values);// 多个查询条件

       示例:this.gethibernateTemplate().find("from User u where u.name=? and u.pwd=?",new String[]{"test","123"});

     

    4. findByExample(Object exampleEntity,int firstResult, int maxResults)//分页使用

       示例:

       User user= new User(); u.setActive("Active");

       List list=this.getHibernateTemplate().findByExample(user,firstResult,maxResults);  

       查询结果:状态为Active的用户(对象从0到20 计数)  

     

    5. findByNamedParam(String hql,String paramName,Object value); //一个查询条件

       示例:

       hql="from User u where u.name=:parName ";

       paramName= "parName";

       value="bb"

       List list = this.getHibernateTemplate.findByNamedParam(hql,paramName,value);

       查询结果:姓名为bb的用户

     

    6. findByNamedParam(String queryString , String[] paramName , Object[] value) //多个查询条件

       示例:

       hql="from User u where u.name=:myname and u.pwd =:mypwd ";

       String[] paramName= new String[]{"myname","mypwd"};

       Sring[] value=new Strign[]{"bb","123"};

       List list = this.getHibernateTemplate.findByNamedParam(hql,paramName,value);

       查询结果:姓名为bb密码为123的用户

     

    7.分页HQL示例

       public List excuteHqlPage(final String hqlStr, final int startRow,final int rowCount) throws DaoException {

       List<Object[]> list;

       try {

           list = getHibernateTemplate().executeFind(new HibernateCallback() {

           public Object doInHibernate(Session session)

           throws HibernateException, SQLException {

           org.hibernate.Query query = (org.hibernate.Query) session.createQuery(hqlStr);

           query.setFirstResult(startRow);// 定义从第几条开始查询

           query.setMaxResults(rowCount);// 定义返回的记录数

           List list = query.list();

           return list;

         }

        });

       } catch (Exception e) {

       throw new DaoException(DaoException.ERRORCODE_EXCUTEHQL);

       }

    return list;

    }

     

    8. 根据HQL/SQL 查询

       public List queryByHql(final String hql, final Object[] prams,final String sql) {

         return (List)  getHibernateTemplate().execute(new HibernateCallback(){

           public Object doInHibernate(Session session)

             throws HibernateException, SQLException {

                if(hql!=null && hql.length()>0){

               Query query=session.createQuery(hql);

               if(prams!=null && prams.length>0){

               for(int i=0;i<prams.length;i++){

               query.setParameter(i,prams[i]);

             }

          }

         return query.list();

       }else{

       SQLQuery sqlquery=session.createSQLQuery(sql);

       return sqlquery.list();

     }

    }

    });

    }

     

    9. 保存/更新

       public String saveOrUpdateObject(ISuperVO vo) throws DaoException {

         try {

             String id = null;

             if (StringUtil.isEmpty(vo.getPid())) {

                getHibernateTemplate().save(vo);

            } else {

               getHibernateTemplate().merge(vo);

           }

            id = vo.getPid();

            return id;

         } catch (Exception e) {

              e.printStackTrace();

    }

    }

     

    10. getHibernateTemplate().delete(vo);  //删除

     

    11. 根据条件删除

       public Integer deleteObjectsByWherePart(final Class voClass,final String wherePart, final Object[] parmaters)throws DaoException {

       try {

           Integer count = (Integer) getHibernateTemplate().execute( new HibernateCallback() {

          public Object doInHibernate(Session session)throws HibernateException, SQLException {

          Integer coun = null;

          String hql = "delete from " + voClass.getName()+ " where 1=1 ";

          if (wherePart != null && wherePart.trim().length() > 0) {

              hql = hql + " and " + wherePart;

          }

          Query query = session.createQuery(hql);

          Object obj = null;

          if (parmaters != null && parmaters.length > 0) {

              for (int i = 0; i < parmaters.length; i++) {

                 obj = parmaters[i];

                query.setParameter(i, obj);

             }

           }

          coun = query.executeUpdate();

          return coun;

         }

        });

        return count;

       } catch (Exception e) {

       e.printStackTrace();

    }

    }

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值