Here’s a step-by-step guide on how to use this Spring Boot application:
- Java Development Kit (JDK): Ensure you have JDK 11 or later installed.
- MySQL Database: Install MySQL and create a database. Also, add a table named
employeeto hold employee data. - Spring Boot Setup: Add the necessary dependencies to your
build.gradlefile if using Gradle.
In MySQL, create a database and an employee table:
CREATE DATABASE your_database_name;
USE your_database_name;
CREATE TABLE employee (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
department VARCHAR(100)
);Replace your_database_name with the name you want for your database.
In src/main/resources/application.properties, set your MySQL connection properties:
spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=trueReplace your_database_name, your_username, and your_password with your actual database credentials.
Navigate to your project directory and run the application using the Gradle command:
./gradlew bootRunAlternatively, you can run the Demo1Application.java class directly from your IDE (e.g., IntelliJ, Eclipse) by clicking "Run."
You can use a tool like Postman or curl to test the REST endpoints.
-
Get All Employees
- Request:
GET /api/employees - Example: Retrieve all employees.
curl -X GET http://localhost:8080/api/employees
- Request:
-
Add a New Employee
- Request:
POST /api/employees - Example: Add a new employee with name "John Doe" and department "Engineering".
curl -X POST http://localhost:8080/api/employees -H "Content-Type: application/json" -d '{"name": "John Doe", "department": "Engineering"}'
- Request:
-
Update an Employee
- Request:
PUT /api/employees - Example: Update an existing employee with
id= 1.
curl -X PUT http://localhost:8080/api/employees -H "Content-Type: application/json" -d '{"id": 1, "name": "Jane Doe", "department": "HR"}'
- Request:
-
Delete an Employee
- Request:
DELETE /api/employees/{id} - Example: Delete an employee with
id= 1.
curl -X DELETE http://localhost:8080/api/employees/1
- Request:
GET /api/employees: Retrieve a list of all employees.POST /api/employees: Add a new employee.PUT /api/employees: Update an existing employee.DELETE /api/employees/{id}: Delete an employee by ID.
- Error Handling: For a real application, you might add error handling to ensure better responses when something goes wrong.
- Testing: You can create tests in the
DemoApplicationTests.javafile to verify each component works as expected.