Introduction to Spring Data JDBC – Spring Data JDBC简介

最后修改: 2020年 8月 9日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Spring Data JDBC is a persistence framework that is not as complex as Spring Data JPA. It doesn’t provide cache, lazy loading, write-behind, or many other features of JPA. Nevertheless, it has its own ORM and provides most of the features we’re used with Spring Data JPA like mapped entities, repositories, query annotations, and JdbcTemplate.

Spring Data JDBC是一个持久化框架,它不像Spring Data JPA那样复杂。它不提供缓存、懒惰加载、写后台或JPA的许多其他功能。然而,它有自己的ORM,并提供了我们在Spring Data JPA中使用的大部分功能,如映射的实体、资源库、查询注释和JdbcTemplate

An important thing to keep in mind is that Spring Data JDBC doesn’t offer schema generation. As a result, we are responsible for explicitly creating the schema.

需要记住的一件事是,Spring Data JDBC并不提供模式生成。因此,我们要负责明确地创建模式。

2. Adding Spring Data JDBC to the Project

2.将Spring Data JDBC添加到项目中

Spring Data JDBC is available to Spring Boot applications with the JDBC dependency starter. This dependency starter does not bring the database driver, though. That decision must be taken by the developer. Let’s add the dependency starter for Spring Data JPA:

Spring Data JDBC可通过JDBC依赖性启动器提供给Spring Boot应用程序。不过,该依赖性启动器并不带来数据库驱动程序。这个决定必须由开发人员来做。让我们为Spring Data JPA添加依赖性启动器:

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency> 

In this example, we’re using the H2 database. As we mentioned early, Spring Data JDBC doesn’t offer schema generation. In such a case, we can create a custom schema.sql file that will have the SQL DDL commands for creating the schema objects. Automatically, Spring Boot will pick this file and use it for creating database objects.

在这个例子中,我们使用的是H2数据库。正如我们早期提到的,Spring Data JDBC不提供模式生成。在这种情况下,我们可以创建一个自定义的schema.sql文件,其中有创建模式对象的SQL DDL命令。自动地,Spring Boot会选择这个文件并使用它来创建数据库对象。

3. Adding Entities

3.添加实体

As with the other Spring Data projects, we use annotations to map POJOs with database tables. In Spring Data JDBC, the entity is required to have an @Id. Spring Data JDBC uses the @Id annotation to identify entities.

与其他Spring Data项目一样,我们使用注解来映射POJO与数据库表。在Spring Data JDBC中,实体被要求有一个@IdSpring Data JDBC使用@Id注解来识别实体。

Similar to Spring Data JPA, Spring Data JDBC uses, by default, a naming strategy that maps Java entities to relational database tables, and attributes to column names. By default, the Camel Case names of entities and attributes are mapped to snake case names of tables and columns, respectively. For example, a Java entity named AddressBook is mapped to a database table named address_book.

与Spring Data JPA类似,Spring Data JDBC默认使用一种命名策略,将Java实体映射到关系型数据库表,将属性映射到列名。默认情况下,实体和属性的Camel Case名称会分别映射到表和列的snake Case名称。例如,一个名为AddressBook的Java实体被映射到一个名为address_book的数据库表。

Also, we can map entities and attributes with tables and columns explicitly by using the @Table and @Column annotations. For example, below we have defined the entity that we’re going to use in this example:

另外,我们可以通过使用@Table@Column注解,将实体和属性与表和列显式地映射。例如,下面我们定义了我们在这个例子中要使用的实体。

public class Person {
    @Id
    private long id;
    private String firstName;
    private String lastName;
    // constructors, getters, setters
}

We don’t need to use the annotation @Table or @Column in the Person class. The default naming strategy of Spring Data JDBC does all the mappings implicitly between the entity and the table.

我们不需要在@Table@Column类中使用注解Person。Spring Data JDBC的默认命名策略在实体和表之间隐含地进行了所有的映射。

4. Declaring JDBC Repositories

4.声明JDBC存储库

Spring Data JDBC uses a syntax that is similar to Spring Data JPA. We can create a Spring Data JDBC repository by extending the Repository, CrudRepository, or PagingAndSortingRepository interface. By implementing CrudRepository, we receive the implementation of the most commonly used methods like save, delete, and findById, among others.

Spring Data JDBC使用的语法与Spring Data JPA类似。我们可以通过扩展RepositoryCrudRepository或PagingAndSortingRepository接口来创建Spring Data JDBC存储库。通过实现CrudRepository,我们可以得到最常用的方法的实现,如savedeletefindById等等。

Let’s create a JDBC repository that we’re going to use in our example:

让我们创建一个JDBC资源库,我们将在我们的例子中使用。

@Repository 
public interface PersonRepository extends CrudRepository<Person, Long> {
}

If we need to have pagination and sorting features, the best choice would be to extend the PagingAndSortingRepository interface.

如果我们需要有分页和排序功能,最好的选择是扩展PagingAndSortingRepository接口。

5. Customizing JDBC Repositories

5.定制JDBC存储库

Despite the CrudRepository‘s built-in methods, we need to create our methods for specific cases.

尽管CrudRepository有内置的方法,我们还是需要为特定情况创建我们的方法。

Now, let’s customize our PersonRepository with a non-modifying query and a modifying query:

现在,让我们用一个非修改性查询和一个修改性查询来定制我们的PersonRepository

@Repository
public interface PersonRepository extends CrudRepository<Person, Long> {

    List<Person> findByFirstName(String firstName);

    @Modifying
    @Query("UPDATE person SET first_name = :name WHERE id = :id")
    boolean updateByFirstName(@Param("id") Long id, @Param("name") String name);
}

Since version 2.0, Spring Data JDBC supports query methods. That is, if we name our query method including the keywords, for example, findByFirstName, Spring Data JDBC will generate the query object automatically.

自2.0版本以来,Spring Data JDBC支持查询方法。也就是说,如果我们命名我们的查询方法,包括关键字,例如,findByFirstName, Spring Data JDBC将自动生成查询对象。

However, for the modifying query, we use the @Modifying annotation to annotate the query method that modifies the entity. Also, we decorate it with the @Query annotation.

然而,对于修改性查询,我们使用@Modifying注解来注释修改实体的查询方法。同时,我们用@Query注解来装饰它。

Inside the @Query annotation, we add our SQL command. In Spring Data JDBC, we write queries in plain SQL. We don’t use any higher-level query language like JPQL. As a result, the application becomes tightly coupled with the database vendor.

@Query注解中,我们添加我们的SQL命令。在Spring Data JDBC中,我们用普通的SQL编写查询。我们不使用任何高级查询语言,如JPQL。因此,应用程序变得与数据库供应商紧密耦合。

For this reason, it also becomes more difficult to change to a different database.

由于这个原因,改变到一个不同的数据库也变得更加困难。

One thing we need to keep in mind is that Spring Data JDBC does not support the referencing of parameters with index numbers. We’re able only to reference parameters by name.

我们需要记住的一点是,Spring Data JDBC不支持用索引号来引用参数我们只能通过名称来引用参数

6. Populating the Database

6.填充数据库

Finally, we need to populate the database with data that will serve for testing the Spring Data JDBC repository we created above. So, we’re going to create a database seeder that will insert dummy data. Let’s add the implementation of database seeder for this example:

最后,我们需要用数据填充数据库,这些数据将用于测试我们上面创建的Spring Data JDBC资源库。所以,我们要创建一个数据库播种器,它将插入假数据。让我们为这个例子添加数据库播种器的实现。

@Component
public class DatabaseSeeder {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void insertData() {
        jdbcTemplate.execute("INSERT INTO Person(first_name,last_name) VALUES('Victor', 'Hugo')");
        jdbcTemplate.execute("INSERT INTO Person(first_name,last_name) VALUES('Dante', 'Alighieri')");
        jdbcTemplate.execute("INSERT INTO Person(first_name,last_name) VALUES('Stefan', 'Zweig')");
        jdbcTemplate.execute("INSERT INTO Person(first_name,last_name) VALUES('Oscar', 'Wilde')");
    }
}

As seen above, we’re using Spring JDBC for executing the INSERT statements. In particular, Spring JDBC handles the connection with the database and lets us execute SQL commands using JdbcTemplates. This solution is very flexible because we have complete control over the executed queries.

如上所示,我们使用Spring JDBC来执行INSERT语句。特别是,Spring JDBC处理与数据库的连接,让我们使用JdbcTemplates执行SQL命令。这个解决方案非常灵活,因为我们可以完全控制执行的查询。

7. Conclusion

7.结语

To summarize, Spring Data JDBC offers a solution that is as simple as using Spring JDBC — there is no magic behind it. Nonetheless, it also offers a majority of features that we’re accustomed to using Spring Data JPA.

总而言之,Spring Data JDBC提供了一个与使用Spring JDBC一样简单的解决方案–背后并没有什么魔力。尽管如此,它也提供了我们使用Spring Data JPA所习惯的大部分功能。

One of the biggest advantages of Spring Data JDBC is the improved performance when accessing the database as compared to Spring Data JPA. This is due to Spring Data JDBC communicating directly to the database. Spring Data JDBC doesn’t contain most of the Spring Data magic when querying the database.

与Spring Data JPA相比,Spring Data JDBC的最大优势之一是访问数据库时的性能得到了提高。这是由于Spring Data JDBC 直接与数据库进行通信在查询数据库时,Spring Data JDBC并不包含大部分Spring Data的魔法

One of the biggest disadvantages when using Spring Data JDBC is the dependency on the database vendor. If we decide to change the database from MySQL to Oracle, we might have to deal with problems that arise from databases having different dialects.

使用Spring Data JDBC时,最大的缺点之一是对数据库供应商的依赖性。如果我们决定将数据库从MySQL改为Oracle,我们可能不得不处理因数据库的方言不同而产生的问题

The implementation of this Spring Data JDBC tutorial can be found over on GitHub.

这个Spring Data JDBC教程的实现可以在GitHub上找到