更新时间:2023-09-12 19:56:04
以下是对每种提到的技术的介绍.
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:
@Repository
注释DAO是一个好主意,因为Bean将由扫描过程自动添加.此外,Spring可能会在注释中添加其他有用的功能.@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注入某种SessionFactory
或EntityManagerFactory
等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.
注意:交易管理
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: