* 查看 */ public List browseMer(int pageSize, int pageNo, int cateId,boolean isSpecial) throws Exception { Session session = MySessionFactory.getSession(); Transaction tx = null; List list = null; try{ String hql = "from Merchandise as a"; if (isSpecial){ hql = hql + " where a.special=1"; }else{ hql = hql + " where a.special=0"; } if (cateId!=0){ hql = hql + " and a.category.id="+cateId; } hql = hql + " order by a.id desc"; Query query = session.createQuery(hql); query.setMaxResults(pageSize); query.setFirstResult((pageNo-1)*pageSize); tx = session.beginTransaction(); list = query.list(); tx.commit(); if (!Hibernate.isInitialized(list))Hibernate.initialize(list); }catch(Exception ex){ if(tx!=null)tx.rollback(); logger.info("MerServiceImpl browseMer异常?\n"); ex.printStackTrace(); }finally{ MySessionFactory.closeSession(); } return list; } public List browseMer(int pageSize, int pageNo, String hql) throws Exception { Session session = MySessionFactory.getSession(); Transaction tx = null; List list = null; try{ Query query = session.createQuery(hql); query.setMaxResults(pageSize); query.setFirstResult((pageNo-1)*pageSize); tx = session.beginTransaction(); list = query.list(); tx.commit(); if (!Hibernate.isInitialized(list))Hibernate.initialize(list); }catch(Exception ex){ if(tx!=null)tx.rollback(); logger.info("MerServiceImpl browseMer异常?\n"); ex.printStackTrace(); }finally{ MySessionFactory.closeSession(); } return list; } public int countRecord(String hql) throws Exception { Session session = MySessionFactory.getSession(); Transaction tx = null; int count = 0; try{ tx = session.beginTransaction(); Query query = session.createQuery(hql); query.setMaxResults(1); count = ((Integer)query.uniqueResult()).intValue(); tx.commit(); }catch(Exception ex){ if(tx!=null)tx.rollback(); logger.info("MerServiceImpl countRecord异常?\n"); ex.printStackTrace(); }finally{ MySessionFactory.closeSession(); } return count; }
时间: 2023-06-20 18:07:19 浏览: 54
这段代码实现了浏览商品的功能,其中 `browseMer` 方法分为两个重载版本,一个是根据商品分类和是否特价进行查询,另一个是根据自定义的 HQL 语句进行查询。`countRecord` 方法用于计算符合条件的记录数量。
具体来说,`browseMer` 方法中通过拼接 HQL 语句来构造查询条件,如果需要查询特价商品,则通过 `where a.special=1` 来进行限制;如果需要查询某个分类下的商品,则通过 `and a.category.id=cateId` 来进行限制。最后通过 `order by a.id desc` 来按照商品 ID 的逆序排列结果。然后使用 `Query` 对象将 HQL 语句发送到数据库进行查询,并设置查询的页码和每页显示的记录数。最后,通过 `query.list()` 方法获取查询结果。
在 `countRecord` 方法中,也是通过执行 HQL 语句来获取符合条件的记录数量,只不过这里使用了 `uniqueResult` 方法获取结果并转换为整型。
相关问题
int PageCount = Math.Ceiling(PageSize / books.Count());报错原因
出现错误的原因可能是变量类型不匹配。在上面的代码中,PageSize和books.Count()都是整数类型,而Math.Ceiling()方法返回的是双精度浮点数类型。因此,需要将结果转换为整数类型,可以使用强制类型转换或者Math.Round()方法来实现。下面是两种修改后的代码示例:
1. 使用强制类型转换:
int PageCount = (int)Math.Ceiling((double)PageSize / books.Count());
2. 使用Math.Round()方法:
int PageCount = (int)Math.Round((double)PageSize / books.Count(), MidpointRounding.AwayFromZero);
其中,MidpointRounding.AwayFromZero表示四舍五入时采用远离零的方向。这两种方法都可以将结果转换为整数类型,避免了类型不匹配的错误。
public List<Books> findAll(@PathVariable int pageNum, @PathVariable int pageSize){
这是一个基于Mybatis的DAO实现的方法,它的作用是查询所有的Books并进行分页展示。其中pageNum和pageSize分别表示当前页码和每页展示的数据条数。在这个方法中,可以使用pageHelper分页插件来实现分页功能。具体实现可以参考以下步骤:
1. 在pom.xml文件中添加pageHelper依赖。
2. 在Mybatis的配置文件中配置pageHelper插件。
3. 在DAO层的方法中使用PageHelper.startPage(pageNum, pageSize)方法来开启分页功能。
4. 在查询语句中使用limit关键字来限制查询结果的数量。
5. 返回查询结果即可。
代码示例:
```
public List<Books> findAll(@PathVariable int pageNum, @PathVariable int pageSize){
PageHelper.startPage(pageNum, pageSize);
List<Books> booksList = booksMapper.selectAll();
return booksList;
}
```