如何使用springboot findAll()方法不写子句

时间:2019-08-31 06:23:29

标签: java spring spring-boot jpa spring-data-jpa

我尝试使用 spring boot JPA和CRUD存储库findAll()方法(使用not in子句)从数据库中获取数据,但找不到任何位置。有任何解决方案都可以使用findAll()方法进行动态查询。

示例:

hql="select * from urlMaster urlmast where urlmast.urlcd not in (select distinct acturl.acuCd from ActionUrl acturl) order by urlmast.urlcd";

当前,我只是从urlMaster数据中获取数据。但是我想要 acturl.acuCd not in ActionUrl表。

存储库类

public interface ActionUrlRepository extends CrudRepository<urlMaster, Long>,JpaRepository<urlMaster, Long> {

}

服务实施:

actionurlrepository.findAll();

是否有规定?请指导我。谢谢!

1 个答案:

答案 0 :(得分:1)

您可以将查询字符串放在@Query注释中,如https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query

中所述

像这样的东西:

public interface ActionUrlRepository extends JpaRepository<urlMaster, Long> {

    @Query("select * from urlMaster urlmast where urlmast.urlcd not in (select distinct acturl.acuCd from ActionUrl acturl) order by urlmast.urlcd")
    List<urlMaster> findByNotIn();
}

顺便说一句,声明中不需要CrudRepository,因为JpaRepository已经从它扩展了(通过PagingAndSortingRepository)。