Typesafe SQL using DSL API
Typesafe database object referencing using code generation
Easy-to-use API for querying and data fetching
SQL logging and debugging
This chapter covers using the Spring Boot JOOQ Starter, using the JOOQ Maven Codegen plugin to generate code from the database schema, and performing various operations.
Introduction to JOOQ
JOOQ is a Java persistence framework that provides a Fluent API to write typesafe SQL queries. JOOQ provides code generation tools to generate code based on the database schema, and you can use that generated code to build typesafe queries.
Using the JOOQ DSL API
This code snippet created a database named Connection and instantiated the DSLContext object, which is the entry point for using JOOQ QueryDSL. Using the DSLContext object, it queried the POST table and iterated through the resultset.
You can integrate JOOQ with the Spring Framework so that you don't have to manually create a connection and instantiate DSLContext. See www.jooq.org/doc/3.17/manual-single-page/#code-generation learn how to integrate Spring with JOOQ.
Spring Boot provides JOOQ Starter to get up and running with JOOQ quickly. You do this by leveraging its autoconfiguration mechanism.
Using Spring Boot’s JOOQ Starter
Spring Boot provides a starter called spring-boot-starter-jooq , which allows you to integrate with JOOQ quickly. This section shows how to use spring-boot-starter-jooq via a step-by-step approach.
Configuring Spring Boot JOOQ Starter
This example uses the H2 in-memory database first. Later you will see how to use MySQL.
Database Schema
The src/main/resources/data.sql File
Code Generation Using the JOOQ Maven Codegen Plugin
The jooq-codegen-maven Plugin Configuration in the pom. xml File
This example configures two profiles (h2 and mysql) with the appropriate JDBC configuration parameters. It generates the code artifacts and places them in the com.apress.demo.jooq.domain package within the gensrc/main/java directory.
Using JOOQ DSL
DSLContext is the main entry point for the JOOQ DSL API. You will see how to implement the data persistence methods using the JOOQ DSL API.
com.apress.demo.repository.PostRepository.java
So now that you have viewed the SQL query, let’s see how the JOOQ DSL API correlates with the above query.
The SELECT command operation is taken care of by the dslContext.select() method from the JOOQ DSL API, followed by the from() and where() method calls to denote the rest of the SQL query. Finally, you use the fetchOne() method to fetch the result set from the database, and in the getPostEntity() method, you map the result set values from the database to your Post Entity.
The example uses the dsl.insertInto() method to insert a new record and specifies the returning() method to return the auto-generated primary key column value. It also calls the fetchOne() method to return the newly inserted record.
Similar to the fineOnePost() method, the findAllPosts() method fetches all rows from the POSTS table using dsl.select().from(), which returns Result<Record>. The example loops through Result<Record> and converts the record into a Post domain object using the getPostEntity() utility method you created earlier.
You don’t need to make any changes to the PostService.java class, as all the changes are done inside the PostRepository.java class methods.
You have learned how to perform various operations, like inserting new records and querying and deleting records using JOOQ DSL.
After adding the above properties, start the application and go to http://localhost:8080/ and add some new posts. You should see that the data is persisted successfully using JOOQ.
PostRepositoryJooqTest.java
The PostRepositoryJooqTest.java test class spins up the Spring application context with beans required to test the JOOQ-related logic, with the help of the @JooqTest. This way, you don’t need to load the whole application context to test your Repository layer. These are called test slices. You will learn more about them in Chapter 14.
Use the correct SQL dialect for the database; otherwise, you may get SQL syntax errors at runtime.
For more info on JOOQ, visit www.jooq.org/learn/ .
Summary
This chapter explained how to use the JOOQ framework by using the Spring Boot JOOQ Starter. The next chapter explains how to work with JPA (with the Hibernate implementation) in your Spring Boot applications.