Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring Boot - Read data from Database with Spring Data JPA


In the previous tutorial we have seen, how we can insert data to the H2 database. In this tutorial we will see, how we can read data from the database.


Below are the preliminary steps, we will follow :

  1. Write the 'pom.xml',


    <project xmlns="http://maven.apache.org/POM/4.0.0"
    		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    		xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>org.example</groupId>
    	<artifactId>springbootproject</artifactId>
    	<version>1.0-SNAPSHOT</version>
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.4.1</version>
    	</parent>
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-data-jpa</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>com.h2database</groupId>
    			<artifactId>h2</artifactId>
    			<scope>runtime</scope>
    		</dependency>
    	</dependencies>
    </project>

  2. Create the 'Student' class and annotate it with @Entity and @Id annotation, so that Spring Boot can understand that the contents of 'Student' class needs to be saved to the database.



    @Entity
    public class Student {
    	@Id
    	int roll;
    	String name;
    	int age;
    	int className;
    }

    Spring Boot - Read data from Database with Spring Data JPA

  3. Create the controller class in Spring Boot. And along with 'insertStudent()' method, create a method called 'readStudent(...)' that reads the student details calling 'StudentService'.



    @RestController
    public class HelloWorldController {
    	@Autowired
    	StudentService studentService;
    	@RequestMapping(method = RequestMethod.POST, value = "/students")
    	public void insertStudent(@RequestBody Student student) {
    		studentService.insertStudentDetails(student);
    	}
    	@RequestMapping(method = RequestMethod.GET, value = "/students")
    	public List readStudent() {
    		return studentService.readStudentDetails();
    	}
    }

    Spring Boot - Read data from Database with Spring Data JPA

  4. Next, in the 'StudentService' class, create the method 'readStudentDetails()' that reads the student details from H2 database.



    @Service
    public class StudentService {
    	@Autowired
    	StudentRepository studentRepository;
    	public void insertStudentDetails(Student student) {
    		studentRepository.save(student);
    	}
    	public List readStudentDetails() {
    		List studentList = (List) studentRepository.findAll();
    		return studentList;
    	}
    }

    Spring Boot - Read data from Database with Spring Data JPA


    Now, in the readStudentDetails() method,

    public List readStudentDetails() {
    	List studentList = (List) studentRepository.findAll();
    	return studentList;
    }


    We have used the findAll() method provided by Spring Data JPA to get all the details of the Student table from the H2 Database.

    List studentList = (List) studentRepository.findAll();


    And to get the findAll() method, we have taken the help of StudentRepository Interface that performs the CRUD operations.

    StudentRepository

    public interface StudentRepository extends CrudRepository<Student, Integer> {
    }

    Spring Boot - Read data from Database with Spring Data JPA

Now, that we are done with the coding (In Spring Boot). Let us start Spring Boot application.


Then open Postman and try adding the details of John,


{"name": "John", "roll": 1, "age": 8, "className": 5}

To the endpoint


localhost:8080/students

Using the method as POST

Spring Boot - Read data from Database with Spring Data JPA

And once the details of John is added. Let us try to retrieve the details of John from the H2 database.


Read the details from H2 Database


To retrieve the details of John from the H2 database, let us change the method to GET and hit the same endpoint (i.e. localhost:8080/students).


And we are able to retrieve the details of John from the database.

Spring Boot - Read data from Database with Spring Data JPA