关于criteria分页中获取总记录数异常问题

一、情景在线

  运维同事反馈了一个问题,说咱们查询分机号码的api 接口返回数据异常,具体的情况时是:

  1.查询时第一页信息所返回的总记录数正确,数据正确。
  2.查询第二页总记录数为 “0” ,数据为对应的第二页数据。 what!!!

二、为题排查

  按照同事的提供的线索,在本地复现发现-->果然如此! 传入当前页 pageNow>1 时 总数totel 将为零,且必现!

  多次测试发现:
  传入pageNow=1 控制台打印的sql
  select count(*) as y0_ from xtboss.biz_api_extensions this_ where this_.number400=? and this_.isdeleted=? limit ?
  传入pageNow>1   打印 sql
  select count(*) as y0_ from xtboss.biz_api_extensions this_ where this_.number400=? and this_.isdeleted=? limit ?, ?。

  于是我翻看了代码,

看了一下我们同事写的接口发现。
  @Override
  //根据Criteria获取记录数
  public Object getCountByCriteria(Criteria criteria, Map<String, String> map, int pageSize,
  int pageNo) {
    criteria.setProjection(Projections.rowCount());
    List<?> ls=this.findByCriteria(criteria, null, 0, 0);
    //改参数设置为空后这句的作用是将原来设置Projection(投影,投影图)的清空,否则只能查 到满足条件的总记录数而criteria.list()将没有记录
    criteria.setProjection(null);
    if(CommonUtil.notEmpty(ls)){
    return ls.get(0);
    }
    return 0;
  同事封装的底层方法,中将criteria.setProjection(null); 将Projection(投影,投影图)的清空 ,后criteria.list()将没有记录数。
  于是我该部分代码给修改了一下:
 
 public Object getCountByCriteria(Criteria criteria, Map<String, String> map, int pageSize,
  int pageNo) {
    Integer total=(Integer)criteria.setProjection(Projections.rowCount()).uniqueResult();
    criteria.setProjection(Projections.rowCount());
      //List<?> ls=this.findByCriteria(criteria, null, 0, 0);
    //改参数设置为空后这句的作用是将原来设置Projection(投影,投影图)的清空,否则只能查 到满足条件的总记录数而criteria.list()将没有记录
   // criteria.setProjection(null);
   // if(CommonUtil.notEmpty(ls)){
   // return ls.get(0);
   / }
    if(total=null){
    return 0;}
    return total;
  问题完美解决!!!!
 

看文章的时候如果,对你有那么一点点的启发,记得要鼓励一下我啊,因为我是一个爱古力少年。。
原文地址:https://www.cnblogs.com/xingtangxublog/p/8735191.html