Hibernate多表联合查询 并且封装Vo对象 日期格式转换

来源:互联网 发布:37少年群侠传进阶数据 编辑:程序博客网 时间:2024/06/05 22:39

  Hibernate多表联合查询,下面是一个多表颁查询的方法示例:


 多表查询

publicList<AccountNewVO> getProvinceAccountNewVOAlarmList(AccountVO account,intpageNo, int pageSize){        StringBuffer sb = newStringBuffer();       List params = newArrayList();       sb.append(" selectd.event_add ,( case  u.cert_type when 1then '身份证' when 2 then'军官证' when 3 then'护照' end)cert_type ,u.age  ,b.name ,d.event_owner,");       sb.append(" d.event_code,to_char(d.event_time,'yyyy-mm-ddHH24:mi:ss'),d.event_contactor,d.fund_identify ,d.fund_identify_code ");       sb.append(",l.type_code,l.notified,l.id   ");       sb.append(" fromt_exception_detail d ,t_user u,t_branch b,t_exception_log l ");       sb.append(" whered.event_usercode = u.user_code ");       sb.append(" andu.province_code = b.code ");       sb.append(" andd.exception_log_id = l.id ");       sb.append(" andb.id = ? ");       sb.append("    and (l.type_code = ? ");       sb.append("    or l.type_code = ?) ");       params.add(account.getProvince().getId());       params.add(CommExceptionType.MINOR);       params.add(CommExceptionType.DIFFERENT_PROVINCE_REG);        sb.append("  order by l.notified ,l.type_code ");       List list = null;       try{            list = hibernateDao.queryBySqlWithPage(sb.toString(),pageNo, pageSize,params.toArray());              }catch(Exceptione){           e.printStackTrace();       }       List<AccountNewVO> accounts = newArrayList<AccountNewVO>();       for(Iterator it = list.iterator(); it.hasNext();) {           Object[] objs = (Object[]) it.next();           AccountNewVO actnVO = newAccountNewVO();    //         System.out.println("date:"+objs[6]);//         System.out.println(objs[10].toString());                 ////////////////////////////////////////           actnVO.setEventAdd(objs[0].toString());           actnVO.setCertType(objs[1].toString());           actnVO.setAge(objs[2].toString());           actnVO.setName(objs[3].toString());           actnVO.setEventOwner(objs[4].toString());           actnVO.setEventCode(objs[5].toString());           try {              actnVO.setEventTime(DateUtils.parseDate(objs[6].toString(),"yyyy-MM-ddHH:mm:ss"));           } catch(ParseException e) {              System.err.println("日期类型转换失败");              e.printStackTrace();           }                    actnVO.setEventContactor(objs[7].toString());           actnVO.setFundIdentify(objs[8].toString());                    actnVO.setFundIdentifyCode(objs[9].toString());           //根据不同预警类型为status赋值                 if(CommExceptionType.MINOR.equals(objs[10].toString())){              actnVO.setStatus(CommExceptionType.STR_MINOR);           }           if(CommExceptionType.DIFFERENT_PROVINCE_REG.equals(objs[10].toString())){               actnVO.setStatus(CommExceptionType.STR_DIFFERENT_PROVINCE_REG);                         }           if(CommExceptionType.FREQUENTWIN.equals(objs[10].toString())){              actnVO.setStatus(CommExceptionType.STR_FREQUENTWIN);           }             actnVO.setNotified(objs[11].toString());           actnVO.setLogId(objs[12].toString());                        accounts.add(actnVO);       }       //返回vo列表       returnaccounts;    }


//不用分页的查询

/**     * 测试用的返回给PDF子系统运行状态报表的数据     * @return     */    public List<SubsysStatusVO> getSubsysDataForPDF(){         StringBuffer sb = newStringBuffer();         List params = newArrayList();         sb.append(" select pb.name pname,b.name subname ,t.name warntype, l.yyyymmddwarntime,");         sb.append(" (case l.notified when 1 then '已处理' when 0 then '未处理' end ) stauts,");         sb.append("  l.proc_decr note ");         sb.append(" from t_exception_log l, t_branch b, t_branch pb, t_exception_typet ");         sb.append(" where b.id = l.branch_id and l.type_code = t.code and b.parent_id= pb.id ");         sb.append(" and l.type_code in (?, ?) ");         sb.append(" order by l.yyyymmdd desc,pname,subname,warntype ");         params.add(5010);          params.add(5012);         List list = newArrayList();         List<SubsysStatusVO> volist = new ArrayList<SubsysStatusVO>();          try{                       list = hibernateDao.queryBySqlWithParams(sb.toString(),params.toArray());                       }catch(Exception e){e.printStackTrace();}          try{             for(Iterator it = list.iterator();it.hasNext();){                Object [] objs =  (Object[])it.next();                SubsysStatusVO vo = newSubsysStatusVO();                vo.setProvinceName(objs[0].toString());                vo.setSubsysName(objs[1].toString());                vo.setWarnTypeName(objs[2].toString());                vo.setWarnTime(objs[3].toString());                String stauts = null== objs[4] ? "" : objs[4].toString();                vo.setWarnStatus(stauts);                String note = null== objs[5] ? "" : objs[5].toString();                vo.setBakInfo(note);                                volist.add(vo);             }         }catch(Exception e){e.printStackTrace();}                  return volist;    }



0 0
原创粉丝点击