jpa的分页和条件查询分页
1.只有排序的分页
public Page<User> find(Integer page, Integer size) { if (null == page) { page = 0; } if (size==0) { size = 10; } PageRequest pageable = PageRequest.of(page-1, size, Sort.Direction.DESC, "id"); Page<User> users = userDao.findAll(pageable); return users; }
2.带有查询条件的分页
public Page<User> findByPageAndParams(Integer page, Integer size, User param) { if (null == page) { page = 0; } if (size == 0) { size = 10; } Pageable pageable = new PageRequest(page - 1, size,Sort.Direction.DESC, "id"); //分页信息 Specification<User> spec = new Specification<User>(){ //查询条件构造 @Override public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Path<String> name = root.get("name"); Predicate p1 = cb.like(name, "%"+param.getName()+"%"); Predicate p = cb.and(p1); return p; } }; Page<User> users = userDao.findAll(spec, pageable); return users; }
3.dao层需要继承两个文件
public interface UserDao extends JpaSpecificationExecutor<User>,JpaRepository<User,Long> { @Query(value = "select * from t_user where name like %?1% order by id desc ", nativeQuery = true) List<User> getUserByLikeUserName(String userName); @Query(value = "select * from t_user where name = ?1", nativeQuery = true) List<User> getUserByUserName(String userName); @Query(value = "select * from t_user where id = ?1", nativeQuery = true) List<User> getUserByUserId(Integer id); @Query(value = "select * from t_user order by id ", nativeQuery = true) List<User> getUserList(); //只有排序的分页查询 Page<User> findAll(Specification<User> spec, Pageable pageable); }
这样就可以是先分页查询。
package com.example.react.service; import com.example.react.model.User; import com.example.react.dao.UserDao; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.*; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import javax.persistence.criteria.*; import java.util.List; import org.springframework.data.jpa.domain.*; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; public User getOne(long id) { User user = new User(); user.setId(id); Example<User> userExample = Example.of(user); return userDao.findOne(userExample).orElse(null); } @Override public List<User> all() { return userDao.findAll(); } @Override public Page<User> find(Integer page, Integer size) { if (null == page) { page = 0; } if (size==0) { size = 10; } PageRequest pageable = PageRequest.of(page-1, size, Sort.Direction.DESC, "id"); Page<User> users = userDao.findAll(pageable); return users; } @Override public Page<User> findByPageAndParams(Integer page, Integer size, User param) { if (null == page) { page = 0; } if (size == 0) { size = 10; } Pageable pageable = new PageRequest(page - 1, size,Sort.Direction.DESC, "id"); //分页信息 Specification<User> spec = new Specification<User>(){ //查询条件构造 @Override public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Path<String> name = root.get("name"); Predicate p1 = cb.like(name, "%"+param.getName()+"%"); Predicate p = cb.and(p1); return p; } }; Page<User> users = userDao.findAll(spec, pageable); return users; } }
返回的结果:
{ "content": [ { "id": 13, "name": "孙文", "phone": "374771", "telPhone": "www" }, { "id": 11, "name": "张君宝", "phone": "122", "telPhone": "011112" }, { "id": 10, "name": "朱熹", "phone": "1112", "telPhone": "3334" }, { "id": 9, "name": "朱常洛", "phone": " 1992933", "telPhone": "234444" }, { "id": 8, "name": "张珊珊", "phone": "18037022643", "telPhone": "367742" } ], "pageable": { "sort": { "sorted": true, "unsorted": false }, "offset": 0, "pageNumber": 0, "pageSize": 5, "paged": true, "unpaged": false }, "last": false, "totalPages": 3, "totalElements": 11, "size": 5, "number": 0, "sort": { "sorted": true, "unsorted": false }, "numberOfElements": 5, "first": true
}