Spring Boot View Hospital Patient Data | CSE JFSD Skill 9

Published on November 02, 2021
Last updated November 02, 2021

The chairperson of the K L Hospital wants to manage the data of the hospital in such way i.e., which patients are consulting which doctor, what prescription is been giving to them, when doctors are coming and leaving the hospital. Along with this store the complete patient details like Full Name, Age, Gender, Any Previous Health Issues, Address. And when it comes to doctor details to be stored are Full Name, Graduation Details, Age, Gender, Address. Develop a spring framework application which implements setter-based dependency injection using bean factory as Spring IoC Container to display all the details of the hospital and its staff. (Use Setter and Constructor Dependency Injection)

In this exercise we need to read patient and hospital related data from a database and display it to the user.

As an exercise we will only be reading patient details for now.

Get your starter spring project from the following link: https://start.spring.io/

for dependencies add the following:

  1. Spring-data-jpa
  2. Your database driver I will be using oracle for this exercise.
  3. Spring web

Download the zip and extract it to your local folder.

open the project in your IDE.

Goto application.properties and add the following lines:


Don’t forget to change your password.

The above lines are for oracle database.If you are using any other database change the driver class name and database url accordingly.

Create the patient pojo class with the following fields:

  1. id
  2. name
  3. age
  4. gender
  5. previousHealthIssues

and generate getters and setters


package com.skill9.hospital;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

public class Patient {
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String name;

    private int age;

    private String gender;

    private String previousIssues;

    // generate getters and setters
    public Integer getId() {
        return id;

    public void setId(Integer id) {
        this.id = id;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public int getAge() {
        return age;

    public void setAge(int age) {
        this.age = age;

    public String getGender() {
        return gender;

    public void setGender(String gender) {
        this.gender = gender;

    public String getPreviousIssues() {
        return previousIssues;

    public void setPreviousIssues(String previous) {
        this.previousIssues = previous;


Create a patient repository class


package com.skill9.hospital;

import org.springframework.data.repository.CrudRepository;

// import com.skill9.hospital.Patient;

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete

public interface PatientRepository extends CrudRepository<Patient, Integer> {


Finally we will create a controller class to handle the requests. Let’s call it MainController.java

MainController will have two methods one to add a patient and other to display the existing patient information.

we will have a post method to add a patient this method will take the patient details as parameters and save it in the database.

Our get method will display all the patients in the database.


package com.skill9.hospital;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;

public class MainController {
    private PatientRepository patientRepository;

    @PostMapping(path = "/add") // Map ONLY POST Requests
    public @ResponseBody String addNewUser(@RequestParam String name, @RequestParam int age,
            @RequestParam String gender, @RequestParam String previousIssues) {
        // @ResponseBody means the returned String is the response, not a view name
        // @RequestParam means it is a parameter from the GET or POST request

        Patient n = new Patient();
        return "Saved";

    @GetMapping(path = "/patients") // Map ONLY GET Requests
    public @ResponseBody Iterable<Patient> getAllUsers() {
        // This returns a JSON or XML with the users
        return patientRepository.findAll();

Tags :