Simple Spring Boot CRUD App - Student Management
This is a basic Spring Boot application that performs Create, Read, Update, and Delete
(CRUD) operations
on a Student entity using an in-memory H2 database. It uses Spring Web and Spring Data
JPA.
1. [Link] - Project Configuration
Add these dependencies in your [Link] file to use Spring Boot, Web, JPA, and H2 database:
- spring-boot-starter-web (for building REST APIs)
- spring-boot-starter-data-jpa (for database interaction)
- h2 (in-memory database for testing)
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
2. Main Class ([Link])
This is the entry point of the Spring Boot application.
@SpringBootApplication
public class StudentCrudApplication {
public static void main(String[] args) {
[Link]([Link], args);
}
}
3. Student Entity ([Link])
This class defines a Student with ID, name, and course. It maps to the database table.
@Entity
public class Student {
@Id @GeneratedValue
private Long id;
private String name;
private String course;
// getters and setters here
}
4. Repository ([Link])
This interface allows Spring Data JPA to manage Student data without writing SQL.
public interface StudentRepository extends JpaRepository<Student, Long> {}
5. Controller ([Link])
This class handles API calls like GET, POST, PUT, and DELETE for students.
@RestController
@RequestMapping("/students")
public class StudentController {
@Autowired StudentRepository repo;
@GetMapping public List<Student> getAll() { return [Link](); }
@PostMapping public Student create(@RequestBody Student s) { return [Link](s); }
@PutMapping("/{id}") public Student update(@PathVariable Long id, @RequestBody
Student s) {
Student exist = [Link](id).orElseThrow();
[Link]([Link]()); [Link]([Link]());
return [Link](exist);
}
@DeleteMapping("/{id}") public String delete(@PathVariable Long id) {
[Link](id); return "Deleted";
}
}
6. [Link] - Config File
[Link]=true
[Link]=jdbc:h2:mem:testdb
[Link]=[Link]
[Link]=sa
[Link]=
[Link]-platform=[Link].H2Dialect
[Link]-auto=update