简体   繁体   English

使用带有spring Boot的Dao设计模式,而不是使用spring data jpa提供的存储库设计模式,这是个好主意吗?

[英]Is it good idea to use the Dao design pattern with spring Boot, instead of using repository design pattern provided by spring data jpa?

Actually, I'm new on spring Boot but I've been using Spring for a little while. 实际上,我是Spring Boot的新手,但我已经使用了Spring一段时间了。 With spring, I used to handle my database(MySQL) through a generic DAO with hibernate/JPA. 使用spring,我曾经通过带有hibernate / JPA的通用DAO处理我的数据库(MySQL)。 However all the tutorials I found on spring Boot use spring data jpa making, therefore, configurations easier. 但是我在spring Boot上发现的所有教程都使用了spring数据jpa,因此配置更容易。

Thus I would like to know whether it is or not a good idea to keep using my old generic DAO, as it allows me to have the full control and to customize my data access as I want. 因此,我想知道继续使用我的旧通用DAO是否是一个好主意,因为它允许我完全控制并根据需要自定义我的数据访问。 If yes, how can I proceed? 如果是,我该怎么办? If not, what can be the disadvantages? 如果没有,可能有哪些缺点?

The DAO pattern is the same as the Repository Pattern that Spring Data supports. DAO模式与Spring Data支持的存储库模式相同。 At least, it should be. 至少,它应该是。 You have one DAO (=Repository) class per entity that provides methods to query or manipulate that entity. 每个实体都有一个DAO(= Repository)类,它提供查询或操作该实体的方法。

whether It is or not a good idea to keep using my old generic DAO, as it allows me to have the a full control and to customize my data access as I want. 是否继续使用我的旧通用DAO是一个好主意,因为它允许我拥有完全控制并根据需要自定义我的数据访问。

Spring Data is flexible enough to allow full control over your queries. Spring Data足够灵活,可以完全控制您的查询。 You have the following options (code examples copied from the Spring Data reference): 您有以下选项(从Spring Data引用复制的代码示例):

  • Using method names: You can simply name your repository methods like this to let Spring Data auto-generate a query for you: 使用方法名称:您可以简单地命名这样的存储库方法,让Spring Data自动为您生成查询:

     List<User> findByEmailAddressAndLastname(String emailAddress, String lastname); 
  • Using custom queries per annotation: Provide a custom JPAQL query within a @Query annotation 每个注释使用自定义查询:@Query注释中提供自定义JPAQL查询

     @Query("select u from User u where u.emailAddress = ?1") User findByEmailAddress(String emailAddress); 
  • Using named queries: define a named JPAQL query within an xml file and reference it within the @Query annotation 使用命名查询:在xml文件中定义命名的JPAQL查询,并在@Query注释中引用它

     <named-query name="User.findByLastname"> <query>select u from User u where u.lastname = ?1</query> </named-query> @Query(name="User.findbyLastname") List<User> findByLastname(String lastname); 
  • Implement a repository method yourself: provide part of the implementation of a Spring Data repository yourself by accessing the Hibernate session (or that of another JPA provider) yourself. 自己实现一个存储库方法:通过自己访问Hibernate会话(或另一个JPA提供程序)来自己提供Spring Data存储库的部分实现。

So, to answer your question: yes, use Spring Data JPA, especially on new projects! 所以,回答你的问题:是的,使用Spring Data JPA,尤其是新项目! It does so much work for you and you can still control queries as you wish (which should only be necessary for complicated queries, and even for those I would suggest using programmatic Specifications ). 它为您做了很多工作,您仍然可以根据需要控制查询(这应该只是复杂查询所必需的,甚至是我建议使用程序化规范的那些)。

Spring Data JPA will make your life as a developer easier. Spring Data JPA将使您作为开发人员的生活更轻松。 The DAO pattern is very similar to the Repository Pattern. DAO模式与存储库模式非常相似。 The advantage of using Spring Data JPA is that you'll be writing a lot less code. 使用Spring Data JPA的优势在于您将编写更少的代码。 Spring Data JPA works a lot like Spring Integration Gateways, where you define an interface, and Spring provides the implementation at run time. Spring Data JPA很像Spring Integration Gateways,你可以在其中定义一个接口,Spring在运行时提供实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM