在hibernate JPA中自定义分页并根据id倒序排序,在接口的实现类中编写如下内容
@PersistenceContext private EntityManager em;
@Override public List<Weibo> findAllByLimit(Integer page, Integer size) { CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery<Weibo> criteriaQuery = criteriaBuilder.createQuery(Weibo.class); Root<Weibo> root = criteriaQuery.from(Weibo.class); CriteriaQuery<Weibo> select = criteriaQuery.select(root); criteriaQuery.orderBy(criteriaBuilder.desc(root.get("id"))); TypedQuery<Weibo> typedQuery = em.createQuery(select); typedQuery.setFirstResult((page - 1) * size); typedQuery.setMaxResults(size); return (List<Weibo>) typedQuery.getResultList(); }