一个hibernate动态查询

//@author lzj
//lzj:根据条件动态查询
  public List findPhotoAllDyn(String isshow,String classname,String userid,String start, String end,String phototypeid,String pagenum){
  StringBuffer hqlStr = new StringBuffer("select pt.STypename,pc.IPhotoclass,pc.SNames,pc.SUsername,pc.IStatus,pc.IShow ,pc.DAdddate ,pt.ITypeid,count(pr) from TPhototype pt right outer join pt.TPhotoclasses pc left join ");
    try{
     if(Integer.parseInt(isshow)==0)hqlStr.append("pc.TPicturescores pr Where pc.IShow=0 ");
     if(Integer.parseInt(isshow)==1)hqlStr.append("pc.TShowopuses pr Where pc.IShow=1 ");
     if(Integer.parseInt(isshow)==-1)hqlStr.append("pc.TPicturescores pr Where pc.IShow is not null ");
     }catch(Exception e){
      hqlStr.append("pc.TPicturescores pr Where pc.IShow=0 ");
     }
    if(classname!="")hqlStr.append("and pc.SNames=:sn "); 
    if(userid!="")hqlStr.append("and pc.SUsername =:SUsername ");
    if(start!="")hqlStr.append("and pc.DAdddate between :Start ");
    if(end!="")hqlStr.append("and :End ");
    if(phototypeid!="")hqlStr.append("and pt.ITypeid = :ITypeid ");
    hqlStr.append("group by pt.STypename,pc.IPhotoclass,pc.SNames,pc.SUsername,pc.IStatus,pc.IShow ,pc.DAdddate ,pt.ITypeid order by count(pr) desc ");
    Session session =this.getSession();
   Query query = session.createQuery(hqlStr.toString());
   if(classname!="")query.setString("sn",classname);
   if(userid!="")query.setString("SUsername",userid);
   if(phototypeid!="")query.setString("ITypeid",phototypeid);
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
   if(start!="")
    try {
     query.setDate("Start",sdf.parse(start));
    } catch (ParseException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   if(end!="")
    try {
     query.setDate("End",sdf.parse(end));
    } catch (ParseException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   query.setCacheable(true);
   if (pagenum == null){
        /*如果pagenum是空,则数据从第一条开始*/
          query.setFirstResult(0);
        /*设置查询开始的第几条数据,这里是从第1条开始*/
          query.setMaxResults(PageSize);
        /*设置查询数据条数*/
          }else{
           try{
           Integer p = (Integer.valueOf(pagenum)-1) * PageSize;
           query.setFirstResult(p);
           query.setMaxResults(PageSize);
           }catch (Exception e){
            query.setFirstResult(0);
         query.setMaxResults(PageSize);  
           }
          }
   List result = query.list();
      List templist = new ArrayList();
      for (Iterator it = result.iterator();it.hasNext();){
       Map mm = new HashMap();
       Object[] row=(Object[])it.next();
        Query query2 =session.createQuery("from TPopularshow ps where ps.SUpid = :s");
        query2.setString("s", ((Long)row[1]).toString());
        query2.setCacheable(true);
        List result2 = query2.list();
        Iterator it2 = result.iterator();
         if (query2.list().size()>0){
          mm.put("ps", "yes");
         } else{
          mm.put("ps", "");
         }
       mm.put("STypename", (String)row[0]);
       mm.put("IPhotoclass", (Long)row[1]);
       mm.put("SNames", (String)row[2]);
       mm.put("SUsername", (String)row[3]);
       //mm.put("IStatus",(Long)row[4]);
       if((Long)row[4]==null){
        mm.put("IStatus2","");
       }else if((Long)row[4]==1){
        mm.put("IStatus","半开放");
        mm.put("IStatus2",(Long)row[4]);
       }else if((Long)row[4]==2){
       mm.put("IStatus","开放");
       mm.put("IStatus2",(Long)row[4]);
       }else if((Long)row[4]==0){
        mm.put("IStatus","不开放");
        mm.put("IStatus2",(Long)row[4]);
       }
       if((Long)row[5]==0){
        mm.put("IShow", "普通相册");
        mm.put("ptype", 1);
        mm.put("IShowb", 3);
       }else if((Long)row[5]==1){
        mm.put("IShow", "特殊相册");
        mm.put("ptype", 2);
        mm.put("IShowb", 4);
       }
       //mm.put("IShow", (Long)row[5]);
       mm.put("DAdddate", (Date)row[6]);
       mm.put("Count", (Long)row[8]);
       templist.add(mm);
      }
      session.close();
      return templist;
     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值