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 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料