How to connect to MySQL 8 in a Quarkus project?

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

Posted on

To connect to a MySQL 8 database in a Quarkus project, you can follow these step-by-step instructions. Quarkus simplifies the process of connecting to databases by providing various extensions, including the Hibernate ORM extension for JPA.

Step 1: Create a Quarkus Project

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

mvn io.quarkus:quarkus-maven-plugin:2.7.0.Final:create \
    -DprojectGroupId=com.example \
    -DprojectArtifactId=quarkus-mysql-demo \
    -DclassName="com.example.HelloResource" \
    -Dpath="/hello"

This command creates a basic Quarkus project with a simple REST endpoint.

Step 2: Configure the Database Connection

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

# DataSource configuration
quarkus.datasource.url=jdbc:mysql://your_database_host:3306/your_database_name
quarkus.datasource.driver=com.mysql.cj.jdbc.Driver
quarkus.datasource.username=your_username
quarkus.datasource.password=your_password

# Hibernate ORM configuration
quarkus.hibernate-orm.database.generation=update
quarkus.hibernate-orm.database.default-schema=public

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 javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class YourEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    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 PanacheRepository provided by Quarkus. For example:

import io.quarkus.hibernate.orm.panache.PanacheRepository;

public interface YourEntityRepository extends PanacheRepository<YourEntity> {
}

Step 5: Create a REST Endpoint

Create a RESTful endpoint to interact with your database. You can do this by creating a resource class with JAX-RS annotations. Here’s an example:

import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

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

    @Inject
    YourEntityRepository repository;

    @GET
    public Iterable<YourEntity> list() {
        return repository.listAll();
    }

    @POST
    @Transactional
    public YourEntity create(YourEntity entity) {
        repository.persist(entity);
        return entity;
    }
}

Step 6: Run Your Quarkus Application

Run your Quarkus application using the following command:

./mvnw quarkus:dev

Your Quarkus 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 Quarkus 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.