07-10
22

商品[x]样式总结

原始写法:
public List getCategorysForMenu(int num) {
        List MenuList = new ArrayList();
        List list = this.getCatesIndex(num);
        Iterator ci = list.iterator();
        CategoryVO cate = new CategoryVO();
        while (ci.hasNext()) {
            cate = (CategoryVO) ci.next();
            Iterator ipt = cate.getPts().iterator();
            int flag = 0; //多少个商品
            while (ipt.hasNext()) {
                ProductVO pt = (ProductVO) ipt.next();
                flag++;
            }
            cate.setPt_num(flag);
            MenuList.add(cate);
        }
        return MenuList;
    }


数据库方法:
               public List FindCateAndNum(List listCate) { //商品[x] ,根据list多少来遍历
       List list = new ArrayList();
       net.sf.hibernate.Transaction tx = null;
       TreeMap map = new TreeMap();
       Session session = HibernateUtil.getSession();
       try {
           tx = session.beginTransaction();
           Iterator ic = listCate.iterator();
           int num = 0;
           CategoryVO cate = new CategoryVO();
           while (ic.hasNext()) {
               cate = (CategoryVO) ic.next();
                String sql ="Select COUNT(*) FROM Product as p Where p.g_id="+ cate.getId() +"";
                Query query = session.createSQLQuery(sql, "p", ProductVO.class);
                Object[] ob=query.list().toArray();
                num =Integer.parseInt(ob[0].toString());
                cate.setPt_num(num);
                list.add(cate);
  
           }
          }

          tx.commit();
       } catch (HibernateException ex) {
           ex.printStackTrace();
           if (tx != null) {
               HibernateUtil.rollbackTransaction(tx);
           }
       } finally {
           if (session != null) {
               HibernateUtil.closeSession(session);
           }
       }
       return list;
   }



SQL写法
引用内容 引用内容
Select (Select COUNT(*) FROM Product Where Product.g_id=c.id) AS num,{c.*} FROM Categorys c orDER BY num DESC


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 561
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭