qq_35899105 2016-08-17 13:16 采纳率: 100%
浏览 1653
已采纳

SSH如何实现分页查询和动态查询?

北大青鸟学员一枚,最近做了几次测试。都是有关于分页查询和动态查询的题目。可是我实在不会。请问大神,SSH中如何实现分页查询和动态查询呢?

  • 写回答

5条回答 默认 最新

  • longlovetongtong 2016-08-17 13:26
    关注

    首先是动态查询总页数
    String hql = "select count(h.id) from Housex h where 1=1";
    if(hc.getDid()!=null){
    hql+=" and h.streetx.districtx.id=:did";
    }
    if(hc.getId()!=null){
    hql+=" and h.id=:id;";
    }
    if(hc.getLowfloorage()!=null){
    hql+=" and h.floorage>=:lowfloorage";
    }
    if(hc.getLowprice()!=null){
    hql+=" and h.pricex>=:lowprice";
    }
    if(hc.getSid()!=null){
    hql+=" and h.streetx.id=:sid";
    }
    if(hc.getTid()!=null){
    hql+=" and h.typex.id=:tid";
    }
    if(hc.getTitle()!=null){
    hql+=" and h.titlex like :title";
    }
    if(hc.getUid()!=null){
    hql+=" and h.usersx.id=:uid";
    }
    if(hc.getUpfloorage()!=null){
    hql+=" and h.floorage<:upfloorage";
    }
    if(hc.getUpprice()!=null){
    hql+=" and h.pricex<:upprice";
    }
    Query q = s.createQuery(hql);
    q.setProperties(hc);
    int tiao = Integer.parseInt(q.list().get(0).toString());
    return tiao%pageSize==0?tiao/pageSize:tiao/pageSize+1;

        然后是动态查询list
        public List<Housex> selectHousexByCondition(Session s, HousexCondition hc,
            int pageNo, int pageSize) {
        String hql = "from Housex h inner join fetch h.usersx u inner join fetch h.streetx s inner join fetch h.typex t inner join fetch s.districtx d where 1=1";
        if(hc.getDid()!=null){
            hql+=" and d.id=:did";
        }
        if(hc.getId()!=null){
            hql+=" and h.id=:id;";
        }
        if(hc.getLowfloorage()!=null){
            hql+=" and h.floorage>=:lowfloorage";
        }
        if(hc.getLowprice()!=null){
            hql+=" and h.pricex>=:lowprice";
        }
        if(hc.getSid()!=null){
            hql+=" and s.id=:sid";
        }
        if(hc.getTid()!=null){
            hql+=" and t.id=:tid";
        }
        if(hc.getTitle()!=null){
            hql+=" and h.titlex like :title";
        }
        if(hc.getUid()!=null){
            hql+=" and u.id=:uid";
        }
        if(hc.getUpfloorage()!=null){
            hql+=" and h.floorage<:upfloorage";
        }
        if(hc.getUpprice()!=null){
            hql+=" and h.pricex<:upprice";
        }
        Query q = s.createQuery(hql);
        q.setProperties(hc);
        q.setFirstResult((pageNo-1)*pageSize);
        q.setMaxResults(pageSize);
        return q.list();
    }
    这是hibernate的做法,如果是mybatis得自己写查询语句,但是mybatis有反向工程的代码,很好使,建议楼主去网上查查
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 已采纳回答 1月17日

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常