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

查看:111
本文介绍了Spring DAO与Spring ORM与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-DAO (http://docs.spring.io/spring/docs/2.0.8/reference/dao.html)
  • Spring-ORM (http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/orm.html)
  • Spring-JDBC (http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html)

据我所知,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提供了简化的模板,用于通过Hibernate,My(i)Batis等ORM技术访问数据库.

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 PersistenceException.通过在DAO上使用@Repository,链接到基础技术的异常将转换为Spring DataAccessException.您的服务层捕获了这些异常,如果您决定更改持久性技术,则当Spring转换了本机异常时,仍将抛出相同的Spring DataAccessExceptions.

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仅依赖于数据源.

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是一个伞形项目,它提供了一个通用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和实体+ spring的注释)编写DAO(查找器方法)和实体类的接口.配置(基于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方法名称定义的命名约定,则在最简单的情况下,甚至不需要提供与finder方法相对应的查询字符串.对于其他情况,您必须在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 power所在的位置:作为开发人员,您在编写代码时会专注于业务,如果做得好,更改底层技术就是实现或配置细节.

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类的数据模型,并获取/设置方法以表示实体属性以及与其他实体的关系.当然,您将需要基于该技术对实体类和字段进行注释,但是从现在开始,POJO就足够了.暂时只关注业务需求.
  2. 为您的DAO定义接口. 1个DAO恰好覆盖1个实体,但是您肯定不需要为每个实体创建DAO,因为您应该能够通过导航关系来加载其他实体.遵循严格的命名约定定义finder方法.
  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) :

  • Single html page: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/
  • PDF: http://docs.spring.io/spring/docs/current/spring-framework-reference/pdf/spring-framework-reference.pdf

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:

  • Single html page: http://docs.spring.io/spring-data/data-commons/docs/1.9.2.RELEASE/reference/html/
  • PDF: http://docs.spring.io/spring-data/data-commons/docs/1.9.2.RELEASE/reference/pdf/spring-data-commons-reference.pdf

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

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