Simple Spring Boot application to manage students (CRUD) using Spring Data JPA.
Demo project that exposes a REST API to create, read, update and delete Student entities.
- Java 17
- Maven (the wrapper
mvnw/mvnw.cmdis included) - MySQL running (or change the configuration to another database)
Default configuration is in src/main/resources/application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/first_crud spring.datasource.username=root spring.datasource.password=
Create the first_crud database in your MySQL server or adjust the URL/credentials to match your environment.
The application uses spring.jpa.hibernate.ddl-auto=create-drop (it creates tables on startup and drops them on shutdown). Change this for production.
-
com.example.firstcrud.entity.Student— JPA entity with fields:studentId(Long, @Id, auto-generated)firstName(String)lastname(String)email(String) — columnemail_address, unique and not null
-
com.example.firstcrud.controller.StudentController— REST endpoints underapi/v1/students:- GET
/api/v1/students— get all students - GET
/api/v1/students/{studentId}— get a student by id - POST
/api/v1/students— create or update a student (send JSON with entity fields) - DELETE
/api/v1/students/{studentId}— delete by id
- GET
-
com.example.firstcrud.service.StudentService— simple logic delegating toStudentRepository(Spring Data JPA) -
com.example.firstcrud.repository.StudentRepository— extendsJpaRepository<Student, Long>
In Windows PowerShell (from the project root C:\MyDevs\firstcrud):
- Build and package:
.\mvnw.cmd clean package -DskipTests- Run the generated JAR:
java -jar target\firstcrud-0.0.1-SNAPSHOT.jarAlternatively run in development mode with the wrapper:
.\mvnw.cmd spring-boot:run.\mvnw.cmd testUsing curl (examples):
- Get all students:
curl -X GET http://localhost:8080/api/v1/students- Get a student by id (e.g. id=1):
curl -X GET http://localhost:8080/api/v1/students/1- Create or update a student (POST) — example JSON:
curl -X POST http://localhost:8080/api/v1/students \
-H "Content-Type: application/json" \
-d '{"firstName":"Juan","lastname":"Pérez","email":"juan.perez@example.com"}'If you prefer PowerShell you can use Invoke-RestMethod:
Invoke-RestMethod -Method Post -Uri http://localhost:8080/api/v1/students -ContentType 'application/json' -Body '{"firstName":"Juan","lastname":"Pérez","email":"juan.perez@example.com"}'- Lombok is included to generate getters/setters automatically; if your IDE flags the annotations, install the Lombok plugin or enable annotation processing.
- The property
spring.jpa.hibernate.ddl-auto=create-dropwill delete data when the app stops. To persist data change it toupdateor manage schema migrations with Flyway/Liquibase. - Update database credentials and URL in
application.propertiesbefore running. - Default port: 8080 (you can change it with
server.portinapplication.properties).
This project is a simple example. If you want to improve validation, error handling, add DTOs, tests or authentication, create a fork/branch and open a PR.
If you need help running the project, share the error you get and your database configuration.