分页查询

spring data jpa 分页查询,springjpa

法一(本地sql查询,注意表名啥的都用数据库中的名称)

public interface UserRepository extends JpaRepository<User, Long> {

  @Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1",
    countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
    nativeQuery = true)
  Page<User> findByLastname(String lastname, Pageable pageable);
}

 

法二(jpa已经落到实处的分页接口)

public interface PagingAndSortingRepository<T, ID extends Serializable>
  extends CrudRepository<T, ID> {

  Iterable<T> findAll(Sort sort);

  Page<T> findAll(Pageable pageable);
}

Accessing the second page of User by a page size of 20 you could simply do something like this:

PagingAndSortingRepository<User, Long> repository = // … get access to a bean
Page<User> users = repository.findAll(new PageRequest(1, 20));

User findFirstByOrderByLastnameAsc();

User findTopByOrderByAgeDesc();

Page<User> queryFirst10ByLastname(String lastname, Pageable pageable);

Slice<User> findTop3ByLastname(String lastname, Pageable pageable);

List<User> findFirst10ByLastname(String lastname, Sort sort);

List<User> findTop10ByLastname(String lastname, Pageable pageable);

 

法三(Query注解,jpa语局)

 @Query(value = "select b.roomUid from RoomBoard b where b.userId=:userId and b.lastBoard=true order by  b.createTime desc")
    Page<String> findRoomUidsByUserIdPageable(@Param("userId") long userId, Pageable pageable);

Pageable pageable = new PageRequest(pageNumber,pageSize);
Page<String> page = this.roomBoardRepository.findRoomUidsByUserIdPageable(userId,pageable);
List<String> roomUids = page.getContent();

可以自定义整个实体(Page<User>),也能够查询某几个字段(Page<Object[]>),和原生sql差十分少一模二样灵活。

 

法四(扩充findAll)

Page<User> findAll(Specification<User> spec, Pageable pageable);

 

data jpa 分页查询,springjpa
法一(本地sql查询,注意表名啥的都用数据库中的名称) public interface
UserRepository extends JpaRepositoryUser, Long {…

法一(本地sql查询,注意表名啥的都用数据库中的名称,适用于特定数据库的查询)

public interface UserRepository extends JpaRepository<User, Long> {

  @Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1",
    countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
    nativeQuery = true)
  Page<User> findByLastname(String lastname, Pageable pageable);
}

 

法二(jpa已经落到实处的分页接口,适用于轻便的分页查询)

public interface PagingAndSortingRepository<T, ID extends Serializable>
  extends CrudRepository<T, ID> {

  Iterable<T> findAll(Sort sort);

  Page<T> findAll(Pageable pageable);
}

Accessing the second page of User by a page size of 20 you could simply do something like this:

PagingAndSortingRepository<User, Long> repository = // … get access to a bean
Page<User> users = repository.findAll(new PageRequest(1, 20));

User findFirstByOrderByLastnameAsc();

User findTopByOrderByAgeDesc();

Page<User> queryFirst10ByLastname(String lastname, Pageable pageable);

Slice<User> findTop3ByLastname(String lastname, Pageable pageable);

List<User> findFirst10ByLastname(String lastname, Sort sort);

List<User> findTop10ByLastname(String lastname, Pageable pageable);

 

//service
 Sort sort = new Sort(Sort.Direction.DESC,"createTime"); //创建时间降序排序
 Pageable pageable = new PageRequest(pageNumber,pageSize,sort);
 this.depositRecordRepository.findAllByUserIdIn(userIds,pageable);

//repository
Page<DepositRecord> findAllByUserIdIn(List<Long> userIds,Pageable pageable);

永利澳门游戏网站, 

 

法三(Query表明,hql语局,适用于查询内定条件的数码)

发表评论

电子邮件地址不会被公开。 必填项已用*标注