[spring-data-jpa]nativeQuery查询带分页

7 篇文章 0 订阅

Native queries
The @Query annotation allows to execute native queries by setting the nativeQuery flag to true.

Example 50. Declare a native query at the query method using @Query

public interface UserRepository extends JpaRepository<User, Long> {
  @Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?1", nativeQuery = true)
  User findByEmailAddress(String emailAddress);
  }

这是官方给出的使用本地查询的例子,特别说明了目前本地查询并不支持动态排序,但是可以利用本地查询进行分页
Note, that we currently don’t support execution of dynamic sorting for native queries as we’d have to manipulate the actual query declared and we cannot do this reliably for native SQL. You can however use native queries for pagination by specifying the count query yourself:
这是官方给出的本地查询的分页形式

当我使用如下语句,会报错

@Query(value = "SELECT o FROM t_d_order o WHERE o.droom_id = ?1 AND DATE_FORMAT(o.create_time,'%Y-%m') = ?2 AND o.order_status IN ?3 ORDER BY o.create_time DESC",nativeQuery = true)
    Page<OrderModel> pageByDroomIdDate(Long droomId, String datetime, List<Integer> orderStatus, Pageable pageable);

报错信息大致为:

Caused by: org.springframework.data.jpa.repository.query.InvalidJpaQueryMethodException: Cannot use native queries with dynamic sorting and/or pagination in method public abstract org.springframework.data.domain.Page com.crm.restapi.repository.OrderRepository.pageByDroomIdDate(java.lang.Long,java.lang.String,java.util.List,org.springframework.data.domain.Pageable)

解决办法如下:

@Query(value = "SELECT o FROM t_d_order o WHERE o.droom_id = ?1 AND DATE_FORMAT(o.create_time,'%Y-%m') = ?2 AND o.order_status IN ?3 ORDER BY ?#{#pageable}",nativeQuery = true)
    Page<OrderModel> pageByDroomIdDate(Long droomId, String datetime, List<Integer> orderStatus, Pageable pageable);

在语句order by之后使用占位符!

参考

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于Spring Data JPA分页查询,您可以按照以下步骤进行操作: 1. 首先,确保您的项目中已经引入了Spring Data JPA依赖,以及相关的数据库驱动依赖。 2. 创建一个继承自JpaRepository的接口,用于定义您的数据访问操作。例如: ```java import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User, Long> { // 在这里可以定义各种自定义的查询方法 } ``` 3. 在您的Service层或者Controller层注入该Repository,并使用其提供的分页方法进行查询。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserRepository userRepository; public Page<User> getUsersByPage(int pageNum, int pageSize) { PageRequest pageRequest = PageRequest.of(pageNum, pageSize); return userRepository.findAll(pageRequest); } } ``` 在上述示例中,我们通过调用`userRepository.findAll(pageRequest)`方法实现了分页查询,并传入`PageRequest`对象来指定页码和每页大小。 4. 最后,在您的Controller层中使用该Service方法来处理分页查询请求,并将结果返回给前端。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users") public Page<User> getUsersByPage(@RequestParam int pageNum, @RequestParam int pageSize) { return userService.getUsersByPage(pageNum, pageSize); } } ``` 在上述示例中,我们通过`@RequestParam`注解来接收前端传递的pageNum和pageSize参数,并调用UserService中的方法进行查询。 这样,您就可以使用Spring Data JPA进行分页查询了。希望能对您有所帮助!如果您还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值