07-10
22
商品[x]样式总结
作者:Java伴侣 日期:2007-10-22
原始写法:
数据库方法:
SQL写法
复制内容到剪贴板 程序代码
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;
}
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;
}
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
评论: 0 | 引用: 0 | 查看次数: 597
发表评论