How to Create a Simple Quarkus Project with Database Integration
Quarkus is a modern Java framework that is designed for building cloud-native applications. It offers fast startup times, low memory usage, and great developer productivity. In this tutorial, we will learn how to create a simple Quarkus project with database integration.
Step 1: Setting up the Development Environment
Before we start, we need to set up the development environment. You will need the following software installed:
- Java JDK 11 or later
- Apache Maven 3.6.2 or later
Step 2: Creating a New Quarkus Project
To create a new Quarkus project, open a command prompt or terminal window and navigate to the directory where you want to create the project. Then, run the following command:
mvn io.quarkus:quarkus-maven-plugin:2.3.2.Final:create \
-DprojectGroupId=com.example \
-DprojectArtifactId=quarkus-demo \
-DclassName="com.example.GreetingResource" \
-Dpath="/hello"
This command creates a new Quarkus project with a sample REST endpoint.
Step 3: Adding Database Integration
To add database integration, we need to add the following dependencies to the pom.xml file:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
These dependencies add support for the PostgreSQL database and the Panache ORM library.
Step 4: Configuring the Database Connection
Next, we need to configure the database connection. Open the application.properties file located in the src/main/resources directory and add the following lines:
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/quarkus_demo
quarkus.datasource.username=postgres
quarkus.datasource.password=secret
Step 5: Creating a Data Entity
To store data in the database, we need to create a data entity. Create a new Java class in the src/main/java directory with the following code:
package com.example;
import javax.persistence.*;
@Entity
@Table(name = "persons")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
This code defines a data entity called Person with three properties: id, name, and age. The @Entity annotation marks the class as a data entity and the @Table annotation specifies the database table name.
Step 6: Creating a Repository
To access the database, we need to create a repository. Create a new Java interface in the src/main/java directory with the following code:
package com.example;
import io.quarkus.hibernate.orm.panache.PanacheRepository;
public interface PersonRepository extends PanacheRepository<Person> {}
This code defines a repository interface called PersonRepository that extends the PanacheRepository interface from the Panache ORM library.
Step 7: Implementing the REST Endpoint
Now that we have set up the database integration, we can implement the REST endpoint. Open the GreetingResource.java file located in the src/main/java directory and replace the code with the following:
package com.example;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;
@Path("/persons")
public class PersonResource {
@Inject
PersonRepository repository;
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Person> list() {
return repository.listAll();
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void add(Person person) {
repository.persist(person);
}
}
This code defines a new REST endpoint at the /persons URL path. The endpoint has two methods: list() and add(). The list() method returns a list of all persons in the database, and the add() method adds a new person to the database.
Step 8: Testing the Application
To test the application, run the following command:
mvn compile quarkus:dev
This command compiles the application and starts a development mode with live reloading. Open a web browser and go to the http://localhost:8080/persons URL. You should see an empty JSON array. Use a tool like a curl or Postman to add a new person to the database:
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","age":30}' \
http://localhost:8080/persons
Refresh the web browser, and you should see the newly added person in the JSON array.
Conclusion
In this tutorial, we learned how to create a simple Quarkus project with database integration. We added support for the PostgreSQL database and the Panache ORM library. We created a data entity, a repository, and a REST endpoint. Finally, we tested the application and verified that it worked as expected. Quarkus is a powerful framework that can help you build cloud-native applications quickly and efficiently.