Skip to content

Latest commit

 

History

History

spring-boot-jooq

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

jOOQ quickstart

jOOQ is a Java database-mapping library that generates Java classes from SQL databases to allow running queries in a type-safe fashion using a fluent API.

This Maven project shows the minimal configuration needed to connect to MariaDB databases using jOOQ in a Spring Boot application.

LT;DR (kinda)

Add the MariaDB JDBC Driver and jOOQ dependencies:

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

<dependency>
    <groupId>org.mariadb.jdbc</groupId>
    <artifactId>mariadb-java-client</artifactId>
    <scope>runtime</scope>
</dependency>

Configure the database connection in the application.properties file:

spring.datasource.url=jdbc:mariadb://localhost:3306/jooq_demo
spring.datasource.username=user
spring.datasource.password=password

Autowire a DSLContext and use it to run SQL statements:

List<ProgrammingLanguageRecord> programmingLanguages = dslContext
        .select(PROGRAMMING_LANGUAGE.NAME, PROGRAMMING_LANGUAGE.RATING)
        .from(PROGRAMMING_LANGUAGE)
        .where(PROGRAMMING_LANGUAGE.RATING.greaterThan(3))
        .orderBy(PROGRAMMING_LANGUAGE.RATING.desc())
        .fetchInto(ProgrammingLanguageRecord.class);

Requirements

  • Java 21 or later. Previous versions should work (update the version in the pom.xml file). Apache Maven
  • MariaDB server
  • An SQL client tool like mariadb, DBeaver, or an SQL integration for your IDE

Preparing the database

See the instructions here.

Running the app

Run the following in the command line:

git clone [email protected]:mariadb-developers/java-quickstart.git
cd java-quickstart/spring-boot-jooq/
mvn package
java -jar target/spring-boot-jooq-0.0.1-SNAPSHOT.jar

Check the output

You should see the output in the terminal.

You can also connect to the database and see the data in the programming_language table:

mariadb-shell --dsn mariadb://user:'Password123!'@127.0.0.1

Run the following query:

SELECT * FROM demo.programming_languages;

Webinar

Watch the webinar Type-Safe SQL with jOOQ.

Also check out this repository which uses the Sakila database and adds FlyWay.