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" \

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/ file and configure the MySQL database connection properties. Replace the placeholders with your MySQL database information:

# DataSource configuration

# Hibernate ORM configuration

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;

public class YourEntity {
    @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;

public class YourEntityResource {

    YourEntityRepository repository;

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

    public YourEntity create(YourEntity 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.