原生SQL查詢 並循環獲取其中的值

     Map mailSizeInfo=new HashMap();
                try{
                                String sql =" select count(*) as mailCount,Mail_box_coding as mailCoding from oa_mail    where ISSUANCE_ID =:userId    group by Mail_box_coding";
                                SQLQuery sqlQuery = (SQLQuery) super.getSession().createSQLQuery(sql).setString("userId",userId);
                                List<Object[]> students = sqlQuery.list();
                                for (Iterator<Object[]> iterator = students.iterator(); iterator.hasNext();) {
                                Object[] mailInfo = iterator.next();
                                             logger.info("查詢出的結果mailInfo    mailCount"+mailInfo[0]+"mailCoding"+mailInfo[1]);
                                         mailSizeInfo.put(mailInfo[1],mailInfo[0]);
                                }
                }catch(Exception ex){
                        ex.printStackTrace();
                        throw new PageJumpException();
                }
將查詢結果放入到map中
到業務邏輯處理中將map中的值取出來,並組裝成需要的數據格式
     String record="";
                Iterator it = mailSizeInfo.entrySet().iterator();
                while (it.hasNext()){
                        Map.Entry pairs = (Map.Entry)it.next();
                        logger.info(pairs.getKey() + " = " + pairs.getValue());
                        String recordNode=pairs.getKey()+":'" +pairs.getValue()+"'";
                            if (record.length() == 0) {
                                        record = recordNode;
                                } else {
                                        record = record + "," + recordNode;
                                }
                }
                String recordsJson = "{"+record + "}";
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章