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.
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);
- 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
See the instructions here.
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
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;
Watch the webinar Type-Safe SQL with jOOQ.
Also check out this repository which uses the Sakila database and adds FlyWay.