qq_35899105
qq_35899105
采纳率0%
2016-08-17 13:16 阅读 1.6k

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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • longlovetongtong 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有反向工程的代码,很好使,建议楼主去网上查查
    
    点赞 评论 复制链接分享
  • longlovetongtong longlovetongtong 2016-08-17 13:27

    查页数的少一点,这是全的
    public int getPagesByCondition(Session s, int pageSize, HousexCondition hc) {
    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;
    }
    注意housexCondition是一个封装的条件类

    点赞 评论 复制链接分享
  • u010357879 古丽扎娜 2016-08-17 13:34

    ///////////////////////////////////////////////////////////////////////////
    package com.vfsd.dao.impl;

    import java.io.Serializable;

    import java.lang.reflect.ParameterizedType;
    import java.lang.reflect.Type;
    import java.sql.SQLException;
    import java.util.List;

    import javax.annotation.Resource;

    import org.hibernate.HibernateException;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.springframework.orm.hibernate3.HibernateCallback;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    import org.springframework.stereotype.Repository;
    import com.vfsd.util.Pager;

    @SuppressWarnings("rawtypes")
    @Repository("baseDao")
    public class BaseDaoImpl extends HibernateDaoSupport{

    @Resource(name="sessionFactory")
    private void setSf( SessionFactory sf){
    super.setSessionFactory(sf);
    }

    private Class clazz;
    {
    Type type=this.getClass().getGenericSuperclass();
    if(type instanceof ParameterizedType){
    Type types[]=((ParameterizedType) type).getActualTypeArguments();
    clazz=(Class) types[0];
    }else{
    clazz=Object.class;
    }
    }

    /**
    *
    *
    */
    @Override
    public Pager findByDynaParam(final int pageNo,final int pageSize, final String hql, final Object dto) {
        Pager pager=new Pager();
        List list= this.getHibernateTemplate().executeFind(new HibernateCallback() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException,
                    SQLException {
                Query query=null;
                    try{
                        query=session.createQuery(hql);
                }catch(Exception e){
                    e.printStackTrace();
                }
                query.setProperties(dto);
                query.setFirstResult((pageNo-1)*pageSize).setMaxResults(pageSize);
                return query.list();
            }
        });
        pager.setRows(list);
        pager.setTotal(getTotalByDynaParam(hql, dto));
        return pager;
    }
    
    private int getTotalByDynaParam(final String hql, final Object dto) {
        int index=hql.indexOf("from");
        final String countHql="select count(*)  "+hql.substring(index).replace("fetch", "");
        List list=this.getHibernateTemplate().executeFind(new HibernateCallback() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException,
                    SQLException {
                Query query=session.createQuery(countHql).setProperties(dto);
                return query.list();
            }
        });
        if(list.size()>0){
            return Integer.valueOf(list.get(0).toString());
        }else{
            return -1;
        }
    }
    

    }

    ////////////////////////////////////////////////////////////////////////////////////

    package com.vfsd.dao.impl;

    import java.util.List;

    import org.springframework.stereotype.Repository;

    import com.vfsd.util.Pager;

    /**
    *
    */
    @Repository("bbsDao")
    public class BbsDaoImpl extends BaseDaoImpl{

    //分页查询帖子
    @SuppressWarnings("unchecked")
    @Override
    public Pager<Bbs> searchAll(int page, int rows, Bbs bbs) {
        String hql=" from Bbs bbs1 where 1=1 ";
        if(bbs!=null){
            //附加条件
        }
        hql =hql+" order by bbs1.id desc ";
        return this.findByDynaParam(page, rows, hql, bbs);
    }
    

    }

    ////////////////////////////////////////////////////////////////////////////
    package com.vfsd.util;

    import java.util.List;

    public class Pager {
    private List rows;
    private int total;
    public List getRows() {
    return rows;
    }
    public void setRows(List rows) {
    this.rows = rows;
    }
    public int getTotal() {
    return total;
    }
    public void setTotal(int total) {
    this.total = total;
    }

    }

    点赞 评论 复制链接分享
  • jinwufeiyang codingjav 2016-08-17 14:50
    点赞 评论 复制链接分享
  • niaonao niaonao 2016-08-18 07:33

相关推荐