This project demonstrates how to create a CRUD (Create, Read, Update, Delete) application using Spring Boot, MySQL, and Spring Data JPA with a Person
entity.
Before you begin, ensure you have the following installed on your machine:
- Java 8 or higher
- Maven
- MySQL
- Postman (optional, for API testing)
-
Clone the repository:
git clone https://github.com/codingwitharmand/crud-springboot.git cd crud-springboot
-
Create a MySQL database:
CREATE DATABASE crud-springboot;
-
Configure the database connection:
Open
src/main/resources/application.properties
and configure the following properties with your MySQL credentials:spring.datasource.url=jdbc:mysql://localhost:3306/crud-springboot spring.datasource.username=<your_user> spring.datasource.password=<your_user_password> spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
-
Install dependencies and build the project:
mvn clean install
-
Run the Spring Boot application:
./mvnw spring-boot:run
-
The application will start and run on
http://localhost:8080
.
The following endpoints are available for the CRUD operations on the Person
entity:
-
GET /api/persons
- Retrieve a list of all persons.
-
GET /api/persons/{id}
- Retrieve a single person by ID.
-
POST /api/persons
- Create a new person.
- Example request body:
{ "name": "John Doe", "city": "Los Angeles", "phoneNumber" : "999-777-444" }
-
PUT /api/persons/{id}
- Update an existing person by ID.
- Example request body:
{ "city": "New York", "phoneNumber" : "999-777-444" }
-
DELETE /api/persons/{id}
- Delete a person by ID.
A Postman collection to test each endpoint is included at the root of the project folder. You can import this collection into Postman to quickly test the API.
- Spring Boot: A framework to simplify the bootstrapping and development of new Spring applications.
- Spring Data JPA: A part of the larger Spring Data family to easily implement JPA-based repositories.
- MySQL: A relational database management system.
- Maven: A build automation tool used primarily for Java projects.
Contributions are welcome! Please open an issue or submit a pull request for any improvements or suggestions.
Happy coding!