Getting Started
Spring Data JDBC 模块的主要优点之一是它支持多种不同的关系数据库,包括 DB2、H2、HSQLDB、MariaDB、Microsoft SQL Server、MySQL、Oracle 和 Postgres。它还提供了一个简单的编程模型,使开发者能够轻松使用熟悉和标准的 Java 语言特性来处理数据库操作。
启动工作环境的简单方法是在 Spring Tools 中或从 Spring Initializr 中创建一个基于 Spring 的项目。
An easy way to bootstrap setting up a working environment is to create a Spring-based project in Spring Tools or from Spring Initializr.
首先,你需要设置一个正在运行的数据库服务器。有关如何为 JDBC 访问配置数据库的详细说明,请参阅供应商文档。
First, you need to set up a running database server. Refer to your vendor documentation on how to configure your database for JDBC access.
Requirements
Spring Data JDBC 需要 Spring Framework {springVersion} 及更高版本。
Spring Data JDBC requires Spring Framework {springVersion} and above.
就数据库而言,Spring Data JDBC 需要 jdbc.dialects 来抽象针对特定于供应商的风格的常见的 SQL 功能。Spring Data JDBC 直接支持以下数据库:
In terms of databases, Spring Data JDBC requires a jdbc.dialects to abstract common SQL functionality over vendor-specific flavours. Spring Data JDBC includes direct support for the following databases:
-
DB2
-
H2
-
HSQLDB
-
MariaDB
-
Microsoft SQL Server
-
MySQL
-
Oracle
-
Postgres
如果你使用其他数据库,那么你的应用将无法启动。jdbc.dialects 部分包含了有关在这种情况下如何继续进行的更多详细信息。
If you use a different database then your application won’t start up. The jdbc.dialects section contains further detail on how to proceed in such case.
Hello World
如何在 STS 中创建 Spring 项目:
To create a Spring project in STS:
-
Go to File → New → Spring Template Project → Simple Spring Utility Project, and press Yes when prompted. Then enter a project and a package name, such as
org.spring.jdbc.example
. -
Add the following to the
pom.xml
filesdependencies
element: -
Add the following to the pom.xml files
dependencies
element:[source, xml]
<dependencies> <!-- other dependency elements omitted --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jdbc</artifactId> <version>{version}</version> </dependency> </dependencies>
-
Change the version of Spring in the pom.xml to be[source, xml]
<spring.version>{springVersion}</spring-framework.version>
-
Add the following location of the Spring Milestone repository for Maven to your
pom.xml
such that it is at the same level as your<dependencies/>
element:[source, xml]
<repositories> <repository> <id>spring-milestone</id> <name>Spring Maven MILESTONE Repository</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories>
存储库也是 browseable here。
The repository is also browseable here.
Logging
Spring Data JDBC 自身几乎不会进行日志记录。相反,JdbcTemplate
发出 SQL 语句的机制提供了日志记录。因此,如果您想检查运行了哪些 SQL 语句,请为 Spring 的 {spring-framework-docs}/data-access.html#jdbc-JdbcTemplate[NamedParameterJdbcTemplate
] 或 MyBatis 激活日志记录。
Spring Data JDBC does little to no logging on its own.
Instead, the mechanics of JdbcTemplate
to issue SQL statements provide logging.
Thus, if you want to inspect what SQL statements are run, activate logging for Spring’s {spring-framework-docs}/data-access.html#jdbc-JdbcTemplate[NamedParameterJdbcTemplate
] or MyBatis.
您可能还想将日志级别设置为 DEBUG
以查看一些其他信息。为此,请编辑 application.properties
文件,使文件具有以下内容:
You may also want to set the logging level to DEBUG
to see some additional information.
To do so, edit the application.properties
file to have the following content:
logging.level.org.springframework.jdbc=DEBUG
Examples Repository
有一个 GitHub repository with several examples 供您下载和使用,以便了解该库的工作原理。
There is a GitHub repository with several examples that you can download and play around with to get a feel for how the library works.
Configuration
Spring Data JDBC 存储库支持可以通过 Java 配置的注释激活,如下面的示例所示:
The Spring Data JDBC repositories support can be activated by an annotation through Java configuration, as the following example shows:
@Configuration
@EnableJdbcRepositories (1)
class ApplicationConfig extends AbstractJdbcConfiguration { (2)
@Bean
DataSource dataSource() { (3)
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.HSQL).build();
}
@Bean
NamedParameterJdbcOperations namedParameterJdbcOperations(DataSource dataSource) { (4)
return new NamedParameterJdbcTemplate(dataSource);
}
@Bean
TransactionManager transactionManager(DataSource dataSource) { (5)
return new DataSourceTransactionManager(dataSource);
}
}
1 | @EnableJdbcRepositories creates implementations for interfaces derived from Repository |
2 | AbstractJdbcConfiguration provides various default beans required by Spring Data JDBC |
3 | Creates a DataSource connecting to a database.
This is required by the following two bean methods. |
4 | Creates the NamedParameterJdbcOperations used by Spring Data JDBC to access the database. |
5 | Spring Data JDBC utilizes the transaction management provided by Spring JDBC. |
前面示例中的配置类使用 spring-jdbc
的 EmbeddedDatabaseBuilder
API 设置了一个嵌入式 HSQL 数据库。DataSource
接着被用来设置 NamedParameterJdbcOperations
和 TransactionManager
。我们最终通过使用 @EnableJdbcRepositories
激活 Spring Data JDBC 存储库。如果没有配置基包,则它将使用配置类所在的包。扩展 AbstractJdbcConfiguration
可以确保注册各种 bean。覆盖它的方法可以用来定制设置(见下文)。
The configuration class in the preceding example sets up an embedded HSQL database by using the EmbeddedDatabaseBuilder
API of spring-jdbc
.
The DataSource
is then used to set up NamedParameterJdbcOperations
and a TransactionManager
.
We finally activate Spring Data JDBC repositories by using the @EnableJdbcRepositories
.
If no base package is configured, it uses the package in which the configuration class resides.
Extending AbstractJdbcConfiguration
ensures various beans get registered.
Overwriting its methods can be used to customize the setup (see below).
可以使用 Spring Boot 进一步简化此配置。使用 Spring Boot,在将 starter spring-boot-starter-data-jdbc
包含在依赖项中后,DataSource
就足够了。其他所有事情都由 Spring Boot 完成。
This configuration can be further simplified by using Spring Boot.
With Spring Boot a DataSource
is sufficient once the starter spring-boot-starter-data-jdbc
is included in the dependencies.
Everything else is done by Spring Boot.
在此设置中,有几件事可能需要定制。
There are a couple of things one might want to customize in this setup.
Dialects
Spring Data JDBC 使用界面 Dialect
的实现来封装特定于数据库或其 JDBC 驱动程序的行为。默认情况下,AbstractJdbcConfiguration
尝试通过获取连接并注册正确的 Dialect
从数据库配置确定方言。你可以覆盖 AbstractJdbcConfiguration.jdbcDialect(NamedParameterJdbcOperations)
来定制方言选择。
Spring Data JDBC uses implementations of the interface Dialect
to encapsulate behavior that is specific to a database or its JDBC driver.
By default, the AbstractJdbcConfiguration
attempts to determine the dialect from the database configuration by obtaining a connection and registering the correct Dialect
.
You override AbstractJdbcConfiguration.jdbcDialect(NamedParameterJdbcOperations)
to customize dialect selection.
如果你使用没有可用方言的数据库,那么你的应用将无法启动。在这种情况下,你必须要求供应商提供 Dialect
实现。作为替代,你可以实现你自己的 Dialect
。
If you use a database for which no dialect is available, then your application won’t start up.
In that case, you’ll have to ask your vendor to provide a Dialect
implementation.
Alternatively, you can implement your own Dialect
.
方言由 {spring-data-jdbc-javadoc}/org/springframework/data/jdbc/repository/config/DialectResolver.html[ Dialects are resolved by {spring-data-jdbc-javadoc}/org/springframework/data/jdbc/repository/config/DialectResolver.html[
|