北大青鸟学员一枚,最近做了几次测试。都是有关于分页查询和动态查询的题目。可是我实在不会。请问大神,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有反向工程的代码,很好使,建议楼主去网上查查
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
- ¥88 找成都本地经验丰富懂小程序开发的技术大咖
- ¥15 如何处理复杂数据表格的除法运算
- ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
- ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
- ¥200 uniapp长期运行卡死问题解决
- ¥15 latex怎么处理论文引理引用参考文献
- ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
- ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
- ¥15 乘性高斯噪声在深度学习网络中的应用