【问题标题】:What is the difference between spring-data-jpa Repository pattern Vs Querydsl query pattern?spring-data-jpa 存储库模式与 Querydsl 查询模式有什么区别?
【发布时间】:2016-03-01 13:22:29
【问题描述】:

我正在开发Spring MVC + spring-data-jpa + Hibernate 示例。我正在使用简单的Repository (by extending JpaRepository<T, ID extends Serializable>) 模式对DataSource(DS) 执行查询并获得结果。甚至我也可以根据自己的业务需求编写任何自定义查询。

在进行研究时,我发现了"querydsl-sql" API。该API使用插件,需要使用QueryDslPredicateExecutor<T>like(by

extending JpaRepository<T, ID extends Serializable>,
QueryDslPredicateExecutor<T>)

。但在我看来,这个 API 的功能与 Repository API 的功能相同。

有人可以建议/指导两种方法有什么区别吗?一个使用简单的 Repository,另一个使用 QueryDslPredicateExecutor

List<Customer> findByCustomerNumberAndCustomerId(Integer customerNumber, Integer customerId);

查询dsl方法

    @Query("select c from Customer c where c.customerNumber=:customerNumber and c.customerId=:customerId")
    List<Customer> findByCustomerNumberAndCustomerId(@Param("customerNumber") 
Integer customerNumber, @Param("customerId") Integer customerId);

【问题讨论】:

    标签: spring-data-jpa querydsl jpa-2.1


    【解决方案1】:

    您的 Querydsl 方法示例实际上是一个 spring-data 存储库方法。

    不同之处在于 QueryDsl 提供了一种简单而美观的方式来创建对数据库的动态查询。 IE。它允许“即时”创建 SQL。 例如,当您需要使用复杂过滤器检索实体集合时,这很有用。例如。按名称、日期、成本等。生成的 SQL 应仅包含过滤器中指定的条件。

    Spring data 允许在不使用 Querydsl 的情况下使用内置的 Specifications API 来实现这一点,但 Querydsl 的方式更简单,甚至对 IDE 更友好。

    更多信息: https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/

    【讨论】:

      猜你喜欢
      • 2016-04-07
      • 2016-12-10
      • 1970-01-01
      • 2014-03-29
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      相关资源
      最近更新 更多