![]() |
VOOZH | about |
Spring Boot is built on the top of the spring and contains all the features of spring.Building a To-Do List API using Spring Boot and MySQL is a practical way to understand backend development and database integration. Spring Boot simplifies application setup, while MySQL ensures reliable data storage. This combination helps developers quickly create scalable RESTful services.
Prerequisites:
- Good knowledge of Java.
- Basic knowledge about Spring Boot.
- Basic knowledge about Creating REST API with SpringBoot.
To create an application in spring boot make sure you have cleared all the previously listed concepts.
Step 1: First go to spring initializer and create a new project using the following data given below:
Now Click on Generate and a .zip file will be downloaded which will be our starter project.
Step 2: Now extract the given folder and then open this project in your preferred IDE, Here I will use IntelliJ Idea Community edition for that, To open this starter project just click on open and then select the extracted folder from your files.
After clicking on OK you'll see a screen that is like the below one.
Note: If something went wrong then you can Right Click on pom.xml > maven > Reload project and after this, some processing will take place and you'll be ready to go.
Step 3: Now create 4 packages in the following folder -> src > main > java > com.example.demo Now right-click on this folder > new > package > give name > press enter
The packages will be the following:
The file tree will look like the one below after you create the above-listed packages.
Step 4: Create a new database named todolist to open MySQL Command Line Client and then execute the command
create database todolist;
After creating this database we will use it in the future.
Step 5: Now we'll configure the application.properties file and add the following information so as to establish the connection with the database which will be MySQL in our case, replace the username with the username of your MySQL(default: root) and your account's password should be written in the spring.datasource.password field
Here are the properties if you want to copy the given properties:
# This is the property to specify the database and the driver here todolist is database name and mysql is driver
spring.datasource.url=jdbc:mysql://localhost:3306/todolist
# These properties specify the data used for authentication and authorization for the databasespring.datasource.username= {use Your username here}
spring.datasource.password= {use Your password here}# This property is used to specify how we'll handle data ex:- update -> update the remaining data,
# ex: create-drop -> everytime create new tables and delete on termination of the program
spring.jpa.hibernate.ddl-auto=update# Driver Class for MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# This is used to show sql whenever any query is executed by hibernate
spring.jpa.show-sql: true
Step 6: Now that we have set up everything we are going to create a model which will help us to create a table in the database. We'll create a class in the models package and we'll name the class Task.java which will contain the following data
Step 7: Now we'll create an interface named TaskRepository inside the package repositories and It will extend the interface JPARepository<Task, Long>, and here Task is our model and Long is the primary id's datatype that we declared in the Task.java file
Step 8: Now that we have created our Repositories and models we'll create our service class and we'll implement all the business logic in this class so create a new class TaskService in the services package.
Step 9: Now at the last step we will create the controllers to specify the endpoints and then perform the tasks, Here we have performed all the CRUD applications and now we'll test that.
Step 10: Now Start the given program by opening the ToDoListApplication.java and clicking on the run button, Here We have the following endpoints to perform the following tasks also we are going to use Postman to make the requests to our server :
GET /api/v1/tasks -> returns all the tasks
POST /api/v1/tasks -> saves new Task to the database
GET /api/v1/tasks/completed -> returns list of all the completed task
GET /api/v1/tasks/incomplete -> returns list of all the incomplete task
PUT /api/v1/tasks/id -> updates the task with given id and details
DELETE /api/v1/tasks/id -> deletes the task with given id from the database
So finally we have created our To Do List application.