【问题标题】:How do I use custom SQL in sprint data repository?如何在 Spring 数据存储库中使用自定义 SQL?
【发布时间】:2019-10-29 13:53:23
【问题描述】:

我正在使用这样的存储库:

public interface UserRepository extends Repository<User, Long> {

  List<User> findByEmailAddressAndLastname(String emailAddress, String lastname);
}

但我需要的是执行我自己的sql statement

select u.* from users u where exists ( select 1 from expires_users where users_id = u.id )

请在答案中引用过去的链接。

【问题讨论】:

    标签: spring-boot jpa spring-data


    【解决方案1】:

    我认为您可以按照documentation of Spring Data JPA 中的说明进行操作。

    这是一个例子:

    public interface UserRepository extends JpaRepository<User, Long> {
    
      @Query("select u.* from User u 
              where exists(select 1 from ExpiredUser e where e.id = u.id)")
      Page<User> findExpiredUsers(Pageable pageable);
    }
    

    PagePageable 部分用于分页结果,假设此查询可能返回比您希望一次处理的结果多得多的结果。更多分页结果信息请见here

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 2015-04-10
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 2019-09-13
      • 2016-03-27
      相关资源
      最近更新 更多