当前位置:   article > 正文

Hibernate 分页的两种方式_hibernate 内存分页

hibernate 内存分页

第一种:hql分页(不推荐)需要手动关闭session连接

//PageBean.java
@SuppressWarnings("hiding")
public class PageBean<T> {
    private int currPage;//当前页数
    private int pageSize;//每页显示的记录数
    private int total; //总计录数
    private int totalPage;//总页数
    private List<T> rows;//每页显示的数据

    public int getCurrPage() {
        return currPage;
    }
    public void setCurrPage(int currPage) {
        this.currPage = currPage;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getTotal() {
        return total;
    }
    public void setTotal(int total) {
        this.total = total;
    }
    public int getTotalPage() {
        return totalPage;
    }
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public List<T> getRows() {
        return rows;
    }
    public void setRows(List<T> rows) {
        this.rows = rows;
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
    //DaoImpl
    //分页展示User
    @SuppressWarnings("unchecked")
    @Override
    public List<User> getUserByPage(User user, int begin, int pageSize) {
        String hql = "from User where isdeleted=0 ";
        //拼装查询条件
        hql = getHqlWhere(user, hql);
        hql+=" order by modifytime desc";

        Session session = this.getHibernateTemplate().getSessionFactory().openSession();
        Query query = session.createQuery(hql);
        query.setFirstResult(begin);
        query.setMaxResults(pageSize);
        List<User> userList = query.list();
        releaseSession(session);

        return userList;
    }

    public int getUserCount(User user){
        String hql = "select count(*) from User where isdeleted=0 ";
        //拼装查询条件
        hql = getHqlWhere(user, hql);
        List<Long> list = this.getHibernateTemplate().find(hql);
        if(list.size()>0){
            return list.get(0).intValue();
        }
        return 0;
    }

    //hql查询条件拼装方法
    private String getHqlWhere(User user,String hql){
        if(user!=null){
            if(user.getUsername()!=null&&!user.getUsername().isEmpty()){
                hql+=" and username like '%"+user.getUsername()+"%'";
            }
            if(user.getZsxm()!=null&&!user.getZsxm().isEmpty()){
                hql+=" and zsxm like '%"+user.getZsxm()+"%'";
            }
            if(user.getFjid()!=null&&!user.getFjid().isEmpty()&&!user.getFjid().equals("0")){
                hql+=" and fjid='"+user.getFjid()+"'";
            }
        }
        return hql;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
    //serverImpl
    @Override
    /**
     *  user分页查询方法
     * @param user   存储查询条件
     * @param currPage 当前页数
     * @param pageSize 每页显示多少行
     * @return
     */
    public PageBean<User> getUserByPage(User user, Integer currPage, Integer pageSize) {

        PageBean<User> pageBean = new PageBean<User>();
        pageBean.setCurrPage(currPage);//当前页数
        pageBean.setPageSize(pageSize);//每页显示多少行
        //总记录数
        int totalCount = userDao.getUserCount(user);
        pageBean.setTotal(totalCount);
        //总页数
        double tc = totalCount;
        Double num = Math.ceil(tc/pageSize);//存在小数位直接进一
        pageBean.setTotalPage(num.intValue());
        //封装每页显示的数据
        int begin = (currPage-1)*pageSize;
        List<User> userList = userDao.getUserByPage(user, begin, pageSize);
        pageBean.setRows(userList);

        return pageBean;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

//Action();
public void list() {
        PageBean<User> pageBean = userService.getUserByPage(user, page, rows);
        Gson gson = new Gson();
        try {
            System.out.println(gson.toJson(pageBean));
//json格式输出分页查询数据
            Utils.sendMsg(gson.toJson(pageBean));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

第二种:DetachedCriteria (推荐使用)

    //分页展示User
    @SuppressWarnings("unchecked")
    @Override
    public List<User> getUserByPage(User user, int begin, int pageSize) {
        DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
        if(user!=null){
            if(user.getUsername()!=null&&!user.getUsername().isEmpty()){
                DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
        if(user!=null){
            if(user.getUsername()!=null&&!user.getUsername().isEmpty()){
                //criteria.add(Restrictions.like("username", "%"+user.getUsername()+"%"));
                 criteria.add(Restrictions.like("username", user.getUsername(),MatchMode.ANYWHERE));
            }
            if(user.getZsxm()!=null&&!user.getZsxm().isEmpty()){
                criteria.add(Restrictions.like("zsxm", "%"+user.getZsxm()+"%"));
            }
            if(user.getFjid()!=null&&!user.getFjid().isEmpty()&&!user.getFjid().equals("0")){
                criteria.add(Restrictions.eq("fjid", user.getFjid()));
            }
        }
        List<User> userList = this.getHibernateTemplate().findByCriteria(criteria, begin, pageSize);

        return userList;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

备注:若DetachedCriteria where 后面多个条件是or

/*
                 *  Hibernate 查询MatchMode的四种模式
                    MatchMode.START:字符串在最前面的位置.相当于"like 'key%'"
                    MatchMode.END:字符串在最后面的位置.相当于"like '%key'"
                    MatchMode.ANYWHERE:字符串在中间匹配.相当于"like '%key%'"
                    MatchMode.EXACT:字符串精确匹配.相当于"like 'key'"
                 */
                //sql where or
                Disjunction disjunction = Restrictions.disjunction();  
                disjunction.add(Restrictions.like("tel",contacts.getSearchTel(),MatchMode.ANYWHERE));
                disjunction.add(Restrictions.like("mphone",contacts.getSearchTel(),MatchMode.ANYWHERE));
                disjunction.add(Restrictions.like("ephone",contacts.getSearchTel(),MatchMode.ANYWHERE));
                criteria.add(disjunction);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/529035
推荐阅读
相关标签
  

闽ICP备14008679号