Hibernate多表聯合查詢 並且封裝Vo物件 日期格式轉換
阿新 • • 發佈:2019-02-17
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; }