Configure jOOQ with Spring Boot and MySQL
jOOQ is a library that will let you create Java Classes such as POJOs by reading your database and lets you write type-safe SQL queries.
In this tutorial, we won’t go deep into its working instead help you to configure and implement jOOQ into your spring boot application.

If you are using PostgreSQL refer to this story: https://readmedium.com/configure-jooq-with-spring-boot-and-postgresql-c362e41722b9
Prerequisite:
- Knowledge of basic spring boot
- knowledge of MySQL, make sure you have done installation
Step 1: Add jOOQ & MySQL dependency
<!-- JOOQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>Step 2: Add jOOQ plugin to pom.xml
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<executions>
<execution>
<id>generate-mysql</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<!--Insert your DB configuration-->
<jdbc>
<driver>com.mysql.cj.jdbc.Driver</driver>
<url>jdbc:mysql://localhost:3306/jooq</url>
<user>root</user>
<password>root</password>
</jdbc>
<generator>
<database>
<name>org.jooq.meta.mysql.MySQLDatabase</name>
<includes>.*</includes>
<excludes></excludes>
<inputSchema>jooq</inputSchema>
</database>
<generate>
<pojos>true</pojos>
<pojosEqualsAndHashCode>
true
</pojosEqualsAndHashCode>
<javaTimeTypes>true</javaTimeTypes>
<fluentSetters>true</fluentSetters>
</generate>
<target>
<packageName>
com.tej.JooQDemo.jooq.sample.model
</packageName>
<directory>
target/generated-sources/jooq
</directory>
</target>
</generator>
</configuration>
</execution>
</executions>
</plugin>Step 3: Configure your database
Make sure you have your database & schema created, I won’t go into database and schema creation.
For testing jOOQ please make sure you have at least one table created.
You can run following script in your MySQL workbench
CREATE TABLE book (
id int not null auto_increment primary key,
title varchar(255),
author varchar(255)
);or
you can use flyway to migrate tables.If you choose to use flyway follow this storyhttps://medium.com/@tejozarkar/configure-flyway-with-spring-boot-9493aebf336botherwise continueNow add the following configuration in your application.properties file
spring.datasource.url=jdbc:mysql://localhost:3306/jooq
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.DriverNow just run
mvn clean install
If it runs successfully, you have configured jOOQ and database correctly.
POJOs and other jOOQ classes will be generated in directory /target/generated-sources/jooq
Step 4: Using jOOQ
In your spring boot project, create a new class called BookService
import com.tej.JooQDemo.jooq.sample.model.tables.pojos.Book;
@Service
public class BookService {
@Autowired
DSLContext context;
public List<Book> getBooks(){
return context
.selectFrom(Tables.BOOK)
.fetchInto(Book.class);
}
public void insertBook(Book book){
context
.insertInto(Tables.BOOK, Tables.BOOK.AUTHOR,
Tables.BOOK.AUTHOR)
.values(book.getTitle(), book.getAuthor())
.execute();
}
}And create a new controller named BookController
import com.tej.JooQDemo.jooq.sample.model.tables.pojos.Book;
@RestController
public class BookController {
@Autowired
BookService bookService;
@GetMapping
public List<Book> getBooks(){
return this.bookService.getBooks();
}
@PostMapping
public void postBook(@RequestBody Book book){
this.bookService.insertBook(book);
}
}That’s it, run your project, and check your POST & GET API.
Find the full source code on https://github.com/Tejas-Ozarkar/JooqSpringBootMySQLDemo





