Skip to content

jmoreno-dev/first-crud-springboot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

firstcrud

Simple Spring Boot application to manage students (CRUD) using Spring Data JPA.

Description

Demo project that exposes a REST API to create, read, update and delete Student entities.

Requirements

  • Java 17
  • Maven (the wrapper mvnw / mvnw.cmd is included)
  • MySQL running (or change the configuration to another database)

Configuration

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.

Relevant structure

  • com.example.firstcrud.entity.Student — JPA entity with fields:

    • studentId (Long, @Id, auto-generated)
    • firstName (String)
    • lastname (String)
    • email (String) — column email_address, unique and not null
  • com.example.firstcrud.controller.StudentController — REST endpoints under api/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
  • com.example.firstcrud.service.StudentService — simple logic delegating to StudentRepository (Spring Data JPA)

  • com.example.firstcrud.repository.StudentRepository — extends JpaRepository<Student, Long>

Build and run

In Windows PowerShell (from the project root C:\MyDevs\firstcrud):

  1. Build and package:
.\mvnw.cmd clean package -DskipTests
  1. Run the generated JAR:
java -jar target\firstcrud-0.0.1-SNAPSHOT.jar

Alternatively run in development mode with the wrapper:

.\mvnw.cmd spring-boot:run

Run tests

.\mvnw.cmd test

API usage examples

Using 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"}'

Notes and considerations

  • 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-drop will delete data when the app stops. To persist data change it to update or manage schema migrations with Flyway/Liquibase.
  • Update database credentials and URL in application.properties before running.
  • Default port: 8080 (you can change it with server.port in application.properties).

Contributing

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.

Contact

If you need help running the project, share the error you get and your database configuration.

About

This repository showcases my first CRUD using Java Spring Boot

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages