Spring DAO vs Spring ORM vs Spring JDBC [英] Spring DAO vs Spring ORM vs Spring JDBC

查看:27
本文介绍了Spring DAO vs Spring ORM vs Spring JDBC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览 Spring 支持的数据访问技术,我注意到它提到了多个选项,我不确定它们之间的区别:

I was going through data access technologies supported by Spring, and I noticed that it mentions multiple options and I am not sure about the difference among them:

据我了解,Spring JDBC 提供了用于减少样板代码的模板,以便通过简单的旧方式访问数据库——您可以编写自己的 SQL 查询.

As I understand, Spring JDBC provides templates for reducing boilerplate code for accessing a database through plain old way - you write your own SQL queries.

Spring-ORM 提供了通过 ORM 技术访问数据库的简化模板,例如 Hibernate、My(i)Batis 等.

Spring-ORM provides simplified templates for accessing databases through ORM technologies, such as Hibernate, My(i)Batis etc.

Spring-DAO 根据 Spring 的网站:

Spring-DAO as per Spring's website:

Spring 中的数据访问对象 (DAO) 支持旨在使其易于使用 JDBC、Hibernate 或 JDO 等数据访问技术以一致的方式

The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way

我对 ORM 与 JDBC 有点清楚,因为它们针对的是访问数据库的不同方式.但是 Spring-DAO 简直令人困惑!

I am a bit clear about ORM vs JDBC as they are aimed at different ways of accessing the DB. But Spring-DAO is just plain confusing!

谁能澄清这三者之间究竟有什么区别?在哪些场景下应该首选哪个?

Could anyone please clarify what exactly are the differences among these three? Which should be preferred in which scenarios?

另外,还有另一个项目 Spring-DATA 也可用(http://projects.spring.io/spring-data/) 现在,它是 Spring 支持的所有数据访问技术的父项目,还是只是 Spring-DAO 的新名称?

Also, there is another project Spring-DATA also available (http://projects.spring.io/spring-data/) Now, is it kind of a parent project for all data access techs supported by Spring or is it just a new name for Spring-DAO?

推荐答案

这里是每个提到的技术的介绍.

Here is an introduction to each mentioned technology.

Spring-DAO

Spring-DAO 不是严格意义上的 spring 模块,而是应该要求你编写 DAO 并写好它们的约定.因此,它既不提供接口也不提供实现或模板来访问您的数据.编写 DAO 时,应使用 @Repository 对其进行注释,以便将与底层技术(JDBC、Hibernate、JPA 等)相关的异常一致地转换为正确的 DataAccessException 子类.

Spring-DAO is not a spring module in a strict sense, but rather conventions that should dictate you to write DAO, and to write them well. As such, it does neither provide interfaces nor implementations nor templates to access your data. When writing a DAO, you should annotate them with @Repository so that exceptions linked to the underlying technology (JDBC, Hibernate, JPA, etc.) are consistently translated into the proper DataAccessException subclass.

例如,假设您现在使用 Hibernate,并且您的服务层捕获 HibernateException 以便对其做出反应.如果您更改为 JPA,您的 DAO 接口不应更改,并且服务层仍将使用捕获 HibernateException 的块进行编译,但您将永远不会进入这些块,因为您的 DAO 现在正在抛出 JPA 持久性异常.通过在 DAO 上使用 @Repository,链接到底层技术的异常被转换为 Spring DataAccessException;您的服务层捕获这些异常,如果您决定更改持久性技术,仍然会抛出相同的 Spring DataAccessExceptions,因为 spring 已经翻译了本机异常.

As an example, suppose you're now using Hibernate, and your service layer catches HibernateException in order to react to it. If you change to JPA, your DAOs interfaces should not change, and the service layer will still compile with blocks that catches HibernateException, but you will never enter these blocks as your DAOs are now throwing JPA PersistenceException. By using @Repository on your DAO, the exceptions linked to the underlying technology are translated to Spring DataAccessException; your service layer catches these exceptions and if you decide to change the persistence technology, the same Spring DataAccessExceptions will still be thrown as spring have translated native exceptions.

但请注意,由于以下原因,它的使用受到限制:

Note however that this has limited usage for the following reasons:

  1. 您通常不应捕获持久性异常,因为提供者可能已回滚事务(取决于确切的异常子类型),因此您不应使用替代路径继续执行.
  2. 提供程序中的异常层次结构通常比 Spring 提供的更丰富,并且没有从一个提供程序到另一个提供程序的明确映射.依赖它是危险的.然而,使用 @Repository 注释 DAO 是个好主意,因为扫描过程会自动添加 bean.此外,Spring 可能会在注释中添加其他有用的功能.
  1. Your should usually not catch persistence exceptions, as the provider may have rolled back the transaction (depending on the exact exception subtype), and thus you should not continue the execution with an alternative path.
  2. The hierarchy of exceptions is usually richer in your provider than what Spring provides, and there's no definitive mapping from one provider to the other. Relying on this is hazardous. This is however a good idea to annotate your DAOs with @Repository, as the beans will be automatically added by the scan procedure. Further, Spring may add other useful features to the annotation.

Spring-JDBC

Spring-JDBC 提供了 JdbcTemplate 类,该类删除了管道代码并帮助您专注于 SQL 查询和参数.您只需要使用 DataSource 对其进行配置,然后您就可以编写如下代码:

Spring-JDBC provides the JdbcTemplate class, that removes plumbing code and helps you concentrate on the SQL query and parameters. You just need to configure it with a DataSource, and you can then write code like this:

int nbRows = jdbcTemplate.queryForObject("select count(1) from person", Integer.class);

Person p = jdbcTemplate.queryForObject("select first, last from person where id=?", 
             rs -> new Person(rs.getString(1), rs.getString(2)), 
             134561351656L);

Spring-JDBC 还提供了一个 JdbcDaoSupport,你可以扩展它来开发你的 DAO.它基本上定义了 2 个属性:一个 DataSource 和一个 JdbcTemplate,它们都可用于实现 DAO 方法.它还提供了一个从 SQL 异常到 Spring DataAccessExceptions 的异常转换器.

Spring-JDBC also provides a JdbcDaoSupport, that you can extend to develop your DAO. It basically defines 2 properties: a DataSource and a JdbcTemplate that both can be used to implement the DAO methods. It also provides an exceptions translator from SQL exceptions to spring DataAccessExceptions.

如果您打算使用纯 jdbc,这是您需要使用的模块.

If you plan to use plain jdbc, this is the module you will need to use.

Spring-ORM

Spring-ORM 是一个涵盖许多持久性技术的伞形模块,即 JPA、JDO、Hibernate 和 iBatis.对于每一种技术,Spring 都提供了集成类,使每一种技术都可以按照 Spring 的配置原则使用,并与 Spring 事务管理平滑集成.

Spring-ORM is an umbrella module that covers many persistence technologies, namely JPA, JDO, Hibernate and iBatis. For each of these technologies, Spring provides integration classes so that each technology can be used following Spring principles of configuration, and smoothly integrates with Spring transaction management.

对于每种技术,配置基本上包括将 DataSource bean 注入某种 SessionFactoryEntityManagerFactory 等 bean.对于纯 JDBC,不需要这样的集成类(除了 JdbcTemplate),因为 JDBC 只依赖于 DataSource.

For each technology, the configuration basically consists in injecting a DataSource bean into some kind of SessionFactory or EntityManagerFactory etc. bean. For pure JDBC, there's no need for such integration classes (apart from JdbcTemplate), as JDBC only relies on a DataSource.

如果你打算使用 JPA 或 Hibernate 之类的 ORM,则不需要 spring-jdbc,而只需要这个模块.

If you plan to use an ORM like JPA or Hibernate, you will not need spring-jdbc, but only this module.

Spring-Data

Spring-Data 是一个伞形项目,它提供了一个通用 API 来定义如何以更通用的方式访问数据(DAO + 注释),涵盖 SQL 和 NOSQL 数据源.

Spring-Data is an umbrella project that provides a common API to define how to access data (DAO + annotations) in a more generic way, covering both SQL and NOSQL data sources.

最初的想法是提供一种技术,以便开发人员以与技术无关的方式编写 DAO(查找器方法)和实体类的接口,并且仅基于配置(DAO 和实体上的注释 + spring配置,无论是基于xml还是java),决定实现技术,无论是JPA(SQL)还是redis,hadoop等(NOSQL).

The initial idea is to provide a technology so that the developer writes the interface for a DAO (finder methods) and the entity classes in a technology-agnostic way and, based on configuration only (annotations on DAOs & entities + spring configuration, be it xml- or java-based), decides the implementation technology, be it JPA (SQL) or redis, hadoop, etc. (NOSQL).

如果您遵循 spring 为查找器方法名称定义的命名约定,那么对于最简单的情况,您甚至不需要提供与查找器方法对应的查询字符串.对于其他情况,您必须在 finder 方法的注释中提供查询字符串.

If you follow the naming conventions defined by spring for the finder method names, you don't even need to provide the query strings corresponding to finder methods for the most simple cases. For other situations, you have to provide the query string inside annotations on the finder methods.

当应用程序上下文被加载时,spring 为 DAO 接口提供代理,其中包含与数据访问技术相关的所有样板代码,并调用配置的查询.

When the application context is loaded, spring provides proxies for the DAO interfaces, that contain all the boilerplate code related to the data access technology, and invokes the configured queries.

Spring-Data 专注于非 SQL 技术,但仍为 JPA(唯一的 SQL 技术)提供模块.

Spring-Data concentrates on non-SQL technologies, but still provides a module for JPA (the only SQL technology).

接下来是什么

了解了这一切之后,您现在必须决定选择什么.这里的好消息是,您无需为该技术做出明确的最终选择.这实际上是 Spring 的强大之处:作为开发人员,您在编写代码时专注于业务,如果您做得好,更改底层技术就是一个实现或配置细节.

Knowing all this, you have now to decide what to pick. The good news here is that you don't need to make a definitive final choice for the technology. This is actually where Spring power resides : as a developer, you concentrate on the business when you write code, and if you do it well, changing the underlying technology is an implementation or configuration detail.

  1. 使用实体的 POJO 类定义数据模型,并使用 get/set 方法来表示实体属性以及与其他实体的关系.您当然需要根据技术对实体类和字段进行注释,但目前,POJO 已经足够开始了.现在只关注业务需求.
  2. 为您的 DAO 定义接口.1 个 DAO 恰好涵盖 1 个实体,但您当然不需要为每个实体创建一个 DAO,因为您应该能够通过导航关系来加载其他实体.按照严格的命名约定定义查找器方法.
  3. 基于此,其他人可以开始在服务层上工作,并为您的 DAO 模拟.
  4. 您学习不同的持久性技术(sql、no-sql)以找到最适合您需求的技术,然后从中选择一种.在此基础上,您注释实体并实现 DAO(如果您选择使用 spring-data,则让 spring 为您实现它们).
  5. 如果业务需求不断发展,而您的数据访问技术不足以支持它(例如,您开始使用 JDBC 和一些实体,但现在需要更丰富的数据模型,而 JPA 是更好的选择),您将不得不更改 DAO 的实现,在实体上添加一些注释并更改 spring 配置(添加 EntityManagerFactory 定义).您的业​​务代码的其余部分不应受到您的更改的其他影响.

注意:事务管理

Spring 提供了一个用于事务管理的 API.如果您打算使用 spring 进行数据访问,您还应该使用 spring 进行事务管理,因为它们很好地集成在一起.对于spring支持的每一种数据访问技术,都有一个匹配的本地事务的事务管理器,或者如果需要分布式事务可以选择JTA.它们都实现了相同的 API,因此(再次)技术选择只是一个可以更改而不会对业务代码产生进一步影响的配置.

Spring provides an API for transaction management. If you plan to use spring for the data access, you should also use spring for transaction management, as they integrate together really well. For each data access technology supported by spring, there is a matching transaction manager for local transactions, or you can choose JTA if you need distributed transactions. All of them implement the same API, so that (once again) the technology choice is just a matter a configuration that can be changed without further impact on the business code.

注意:Spring 文档

您提到的 Spring 文档的链接相当陈旧.这是最新版本(4.1.6,涵盖所有主题)的文档:

The links to Spring documentation that you mentioned are rather old. Here is the documentation of the latest release (4.1.6, covering all topics) :

Spring-data 不是 Spring 框架的一部分.您应该首先阅读一个通用模块以习惯这些原则.文档可以在这里找到:

Spring-data is not part of the Spring framework. There is a common module that you should first read to get used to the principles. Documentation can be found here:

这篇关于Spring DAO vs Spring ORM vs Spring JDBC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆