public pageBean seachProductByBrank(int pageSize, int currentLPage,
String product_Brank, String product_itemName) {
final String hql = "from " + OgrilProducts.class.getName()
+ " where product_Brank='" + product_Brank
+ "' and product_itemName='" + product_itemName
+ "' order by product_registerDate desc";
int allRow = this.getAllRowCount(hql);// 总记录数
int totalPage = pageBean.countTotalpage(pageSize, allRow);// 总页数
final int offset = pageBean.countOffset(pageSize, currentLPage);// 当前页开始记录
final int length = pageSize;
final int currentPage = pageBean.countCurrentPage(currentLPage);
List list = this.queryForPage(hql, offset, length);// 记录
// 把分页信息保存到Bean中
pageBean pagebean = new pageBean();
pagebean.setPageSize(pageSize);
pagebean.setCurrentPage(currentPage);
pagebean.setAllRow(allRow);
pagebean.setTotalPage(totalPage);
pagebean.setList(list);
return pagebean;
}
以上是查询分页的具体代码。 HQL 如何消除重复项?
你是指有重复数据重复么?那么distinc下。或者使用Set集合
List<OgrilProducts> list = this.queryForPage(hql, offset, length)
Set<OgrilProducts> sets = new HashSet<OgrilProducts>();
for(OgrilProducts op : list){
sets.add(op);
}
還有一個非DATABASE的解決方案那就是如樓上老兄所止的SET
用set是由前提的。那就算对象所存数据一样。既然你无法使用,证明你只是想让重复的货号取一个。那你就使用distinct吧。将你的HQL改动下。 select id, distinct(商品货号),商品品牌from OgrilProducts 不过这时候查出来的List是 List<Object[]>
后面的自己写写看了,思路就这样。
* 功能:根据品牌查询
*
* @param pageSize
* 每页大小
* @param currentLPagem
* 当前第几页
* @param productBlack
* 产品品牌
* @param productBlackItem
* 产品分类
* @param productSize
* 产品尺码
* @param keyWord
* 搜索关键字
* @return
*/
public pageBean seachProductByBrank(int pageSize, int currentLPage,
String product_Brank, String product_itemName) {
// final String hql = "from " + OgrilProducts.class.getName()
// + " where product_Brank='" + product_Brank
// + "' and product_itemName='" + product_itemName
// + "' order by product_registerDate desc";
final String hql = " from "
+ OgrilProducts.class.getName()
+ " as product where product.productItemName='"
+ product_itemName
+ "' and productBrank='"
+ product_Brank
+ "' and not exists( from "
+ OgrilProducts.class.getName()
+ " where productItemName='"
+ product_itemName
+ "' and productBrank='"
+ product_Brank
+ "' and productItemNumber=product.productItemNumber and productId<product.productId )";
int allRow = this.getAllRowCount(hql);// 总记录数
int totalPage = pageBean.countTotalpage(pageSize, allRow);// 总页数
final int offset = pageBean.countOffset(pageSize, currentLPage);// 当前页开始记录
final int length = pageSize;
final int currentPage = pageBean.countCurrentPage(currentLPage);
List list = this.queryForPage(hql, offset, length);// 记录
// 把分页信息保存到Bean中
pageBean pagebean = new pageBean();
pagebean.setPageSize(pageSize);
pagebean.setCurrentPage(currentPage);
pagebean.setAllRow(allRow);
pagebean.setTotalPage(totalPage);
pagebean.setList(list);
return pagebean;
}
注释的是以前分页的方法 改动了一下 问题解决了
现在结贴.....