Micronaut Data from a Spring Boot Application
This guide shows how to use Micronaut Data from a Spring Boot application.
On this guide
In this section
What you will need
To complete this guide, you will need the following:
-
Some time on your hands
-
A decent text editor or IDE (e.g. IntelliJ IDEA)
-
JDK 21 or greater installed with
JAVA_HOMEconfigured appropriately
Introduction
In this guide, you will use Micronaut Data JDBC within a Spring Boot application.
Solution
We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.
-
Download and unzip the source
Generate a Spring Boot Application
Generate a Spring Boot application using Spring Initializr with Spring Web.
Dependencies
Add the following dependencies as described in Using the Micronaut Spring Boot Starter to use Micronaut Features within a Spring Boot application.
annotationProcessor platform("io.micronaut.platform:micronaut-platform:5.1.3")
implementation platform("io.micronaut.platform:micronaut-platform:5.1.3")
annotationProcessor("io.micronaut:micronaut-inject-java")
annotationProcessor("jakarta.annotation:jakarta.annotation-api")
implementation("jakarta.annotation:jakarta.annotation-api")
annotationProcessor("jakarta.persistence:jakarta.persistence-api")
implementation("jakarta.persistence:jakarta.persistence-api")
implementation("io.micronaut.spring:micronaut-spring-boot-starter")Moreover, add the following dependencies to use Micronaut Data:
annotationProcessor("io.micronaut.data:micronaut-data-processor")
implementation("io.micronaut.data:micronaut-data-jdbc")
implementation("io.micronaut.sql:micronaut-jdbc-hikari")
runtimeOnly("com.h2database:h2")Configuration
Add configuration for an in-memory H2 Database.
#Mon Aug 31 19:16:21 UTC 2026
datasources.default.dialect=H2
datasources.default.driverClassName=org.h2.Driver
datasources.default.password=
datasources.default.schema-generate=CREATE_DROP
datasources.default.url=jdbc\:h2\:mem\:devDb;LOCK_TIMEOUT\=10000;DB_CLOSE_ON_EXIT\=FALSE
datasources.default.username=saMapped Entities with Java Records
Create a Micronaut Data Mapped Entity
|
Note
|
Learn how to leverage Java records for immutable configuration, Micronaut Data Mapped Entities and Projection DTOs in the Micronaut Data and Java Records guide. |
JDBC Repository
Next, create a repository interface to define the operations to access the database. Micronaut Data will implement the interface at compilation time.
Controller
Create a controller which exposes a GET route at /books.
Test
The following test shows that you can use Micronaut Data JDBC as your persistence solution in a Spring Boot Application.
-
Inject a bean of type
BookRepositoryby using@Autowiredon the field definition.
Testing the Application
To run the tests:
./gradlew testNext Steps
Read more about Micronaut Spring and Micronaut Data.
Help with the Micronaut Framework
The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.
License
|
Note
|
All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images). |