Spring Boot Support for jOOQ – 对jOOQ的Spring Boot支持

最后修改: 2016年 6月 8日


1. Overview


This tutorial is a follow-up to the Introduction to jOOQ with Spring article, covering the ways that jOOQ can be used within a Spring Boot application.

本教程是Introduction to jOOQ with Spring文章的后续,涵盖了在Spring Boot应用程序中使用jOOQ的方式。

If you have not gone through that tutorial, please take a look at it and follow the instructions in section 2 on Maven Dependencies and in section 3 on Code Generation. This will generate source code for Java classes representing tables in the sample database, including Author, Book and AuthorBook.

如果你没有看过该教程,请看一看,并按照第2节 “Maven依赖 “和第3节 “代码生成 “中的说明操作。这将为代表样本数据库中表格的Java类生成源代码,包括AuthorBookAuthorBook

2. Maven Configuration


In addition to the dependencies and plugins as in the previous tutorial, several other components need to be included in the Maven POM file to make jOOQ work with Spring Boot.

除了前一个教程中的依赖和插件外,还需要在Maven POM文件中包含其他几个组件,以使jOOQ与Spring Boot一起工作。

2.1. Dependency Management


The most common way to make use of Spring Boot is to inherit from the spring-boot-starter-parent project by declaring it in the parent element. However, this method is not always suitable as it imposes an inheritance chain to follow, which may not be what users want in many cases.

利用Spring Boot的最常见方法是通过在parent元素中声明,从spring-boot-starter-parent项目中继承。然而,这种方法并不总是合适的,因为它强加了一个需要遵循的继承链,这在很多情况下可能不是用户所希望的。

This tutorial uses another approach: delegating the dependency management to Spring Boot. To make it happen, just adds the following dependencyManagement element to the POM file:

本教程使用另一种方法:将依赖性管理委托给Spring Boot。要做到这一点,只需在POM文件中添加以下dependencyManagement元素。


2.2. Dependencies


In order for Spring Boot to control jOOQ, a dependency on the spring-boot-starter-jooq artifact needs to be declared:

为了让Spring Boot控制jOOQ,需要声明对spring-boot-starter-jooq工件的依赖。


Note that this article focuses on the open-source distribution of jOOQ. If you want to work with the commercial distributions, check out the Guide to Using jOOQ’s Commercial Distributions with Spring Boot on the official blog.

请注意,本文主要关注jOOQ的开源发行版。如果您想使用商业发行版,请查看官方博客上的Guide to Using jOOQ’s Commercial Distributions with Spring Boot

3. Spring Boot Configuration

3.Spring Boot配置

3.1. Initial Boot Config


Before we get to the jOOQ support, we’re going to start preparing things with Spring Boot.

在我们进入jOOQ支持之前,我们要开始用Spring Boot做准备。

First, we’re going to take advantage of the persistence support and improvements in Boot and our data access information in the standard application.properties file. That way, we can skip over defining the beans and making these configurable via a separate properties file.

首先,我们要利用 Boot 中的持久性支持和改进,以及我们在标准 application.properties 文件中的数据访问信息。这样,我们就可以跳过定义Bean,并通过一个单独的属性文件使这些可配置。

We’ll add the URL and credentials here to define our embedded H2 database:



We’re also going to define a simple Boot application:


public class Application {

We’ll leave this one simple and empty and we’ll define all other bean declarations in another configuration class – InitialConfiguration.


3.2. Bean Configuration


Let’s now define this InitialConfiguration class:


public class InitialConfiguration {
    // Other declarations

Spring Boot has automatically generated and configured the dataSource bean based on properties set in the application.properties file, so we do not need to register it manually. The following code lets the auto-configured DataSource bean to be injected into a field, and shows how this bean is used:

Spring Boot已经根据application.properties文件中设置的属性自动生成并配置了dataSourcebean,因此我们不需要手动注册。下面的代码让自动配置的DataSource bean被注入到一个字段中,并展示了如何使用这个bean。

private DataSource dataSource;

public DataSourceConnectionProvider connectionProvider() {
    return new DataSourceConnectionProvider
      (new TransactionAwareDataSourceProxy(dataSource));

Since a bean named transactionManager has also been automatically created and configured by Spring Boot, we do not need to declare any other bean of the DataSourceTransactionManager type as in the previous tutorial to take advantage of Spring transaction support.

由于Spring Boot已经自动创建并配置了一个名为transactionManager的Bean,因此我们不需要像之前的教程那样声明任何其他DataSourceTransactionManager类型的Bean来利用Spring事务支持。

A DSLContext bean is created in the same way as in the PersistenceContext class of the preceding tutorial:

一个DSLContext Bean的创建方式与前面教程中的PersistenceContext类相同。

public DefaultDSLContext dsl() {
    return new DefaultDSLContext(configuration());

Lastly, a Configuration implementation needs to be provided to DSLContext. Since Spring Boot is able to recognize the SQL dialect in use through the existence of H2 artifact on the classpath, a dialect configuration is no longer necessary:

最后,需要向DSLContext提供一个Configuration实现。由于Spring Boot能够通过classpath上H2工件的存在来识别正在使用的SQL方言,因此不再需要方言配置。

public DefaultConfiguration configuration() {
    DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
      .set(new DefaultExecuteListenerProvider(exceptionTransformer()));

    return jooqConfiguration;

4. Using Spring Boot With jOOQ

4.使用Spring Boot与jOOQ

To make the demonstration of Spring Boot support for jOOQ easier to be followed, the test cases in the prequel of this tutorial are reused with a little change to its class-level annotations:

为了使Spring Boot对jOOQ的支持更容易被理解,本教程前传中的测试案例在对其类级注释稍作改动后被重新使用。

public class SpringBootTest {
    // Other declarations

It is clear that rather than adopting the @ContextConfiguration annotation, Spring Boot uses @SpringApplicationConfiguration to take advantage of SpringApplicationContextLoader context loader to test applications.

很明显,Spring Boot没有采用@ContextConfiguration注解,而是使用@SpringApplicationConfiguration来利用SpringApplicationContextLoader上下文装载器来测试应用程序。

Test methods for inserting, updating and deleting data are exactly the same as in the previous tutorial. Please have a look at section 5 of that article on Using jOOQ with Spring for more information. All the tests should be successfully executed with the new configuration, proving that jOOQ is fully supported by Spring Boot.

插入、更新和删除数据的测试方法与上一篇教程中的完全相同。请看那篇关于使用jOOQ与Spring的文章的第5节以了解更多信息。所有的测试都应该在新的配置下成功执行,证明JOOQ完全被Spring Boot支持。

5. Conclusion


This tutorial dug deeper into the use of jOOQ with Spring. It introduced the ways for a Spring Boot application to take advantage of jOOQ to interact with a database in a type-safe manner.

本教程更深入地探讨了jOOQ与Spring的使用。它介绍了Spring Boot应用程序如何利用jOOQ以类型安全的方式与数据库进行交互。

The implementation of all these examples and code snippets can be found in a GitHub project.