How to connect to MySQL 8 in a Micronaut project?

This article shows how to connect to MySQL 8 in a Micronaut project.

Posted on

To connect to a MySQL 8 database in a Micronaut project, you can follow these step-by-step instructions. Micronaut simplifies the process of connecting to databases by providing a variety of database connection configurations.

Step 1: Create a Micronaut Project

If you don’t already have a Micronaut project, you can create one using the Micronaut command-line tool or your preferred IDE. For this tutorial, we’ll use the command-line tool:

mn create-app com.example.micronautmysqldemo --features=hibernate-jpa

This command creates a basic Micronaut project with Hibernate JPA support.

Step 2: Configure the Database Connection

In your Micronaut project, open the src/main/resources/application.yml (or application.properties) file and configure the MySQL database connection properties. Replace the placeholders with your MySQL database information:

datasources:
  default:
    url: jdbc:mysql://your_database_host:3306/your_database_name
    driverClassName: com.mysql.cj.jdbc.Driver
    username: your_username
    password: your_password
    dialect: org.hibernate.dialect.MySQL8Dialect
    pooled: true
    maximumPoolSize: 5

Step 3: Create an Entity Class

Create a Java entity class that represents a table in your database. Annotate the class with @Entity and use JPA annotations to map the class to the database table. For example:

import io.micronaut.data.annotation.AutoPopulated;
import io.micronaut.data.annotation.GeneratedValue;
import io.micronaut.data.annotation.Id;
import io.micronaut.data.annotation.MappedEntity;

@MappedEntity
public class YourEntity {
    @Id
    @GeneratedValue
    @AutoPopulated
    private Long id;

    private String name;

    // Getters and setters
}

Step 4: Create a Repository Interface

Create a repository interface for your entity by extending the CrudRepository provided by Micronaut Data. For example:

import io.micronaut.data.repository.CrudRepository;

public interface YourEntityRepository extends CrudRepository<YourEntity, Long> {
}

Step 5: Create a Controller

Create a controller to expose endpoints for interacting with your database. You can use Micronaut’s @Controller and @Get, @Post, @Put, and @Delete annotations to define your RESTful endpoints. Here’s an example:

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;

@Controller("/your-entity")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class YourEntityController {

    private final YourEntityRepository repository;

    public YourEntityController(YourEntityRepository repository) {
        this.repository = repository;
    }

    @Get("/{id}")
    public YourEntity findById(Long id) {
        return repository.findById(id).orElse(null);
    }

    @Post
    public YourEntity save(YourEntity entity) {
        return repository.save(entity);
    }

    @Put("/{id}")
    public YourEntity update(Long id, YourEntity entity) {
        return repository.update(entity);
    }

    @Delete("/{id}")
    public void delete(Long id) {
        repository.deleteById(id);
    }
}

Step 6: Run Your Micronaut Application

Run your Micronaut application using the following command:

./gradlew run

Your Micronaut application will start, and you can access the RESTful endpoints you defined, which will interact with your MySQL 8 database.

This step-by-step tutorial should help you get started with connecting to a MySQL 8 database in a Micronaut project. Be sure to replace the placeholders in the configuration properties with your actual database information. You can also extend your application by adding more entities, repositories, and endpoints as needed.