Spring Data JPA

Spring Data JPA

This Blog describes how you can create crud application using JPA repositories without writing any boilerplate code.

Using Springboot,Spring Data JPA, and Hibernate.

This spring data JPA is like magic which we don’t want to write any code in doing crud operation because it has some inbuilt method just we need to call it.

Findall()
Findbyid()
Save()
Deletebyid()
Update
Etc..

Let see the example for creating curd application with springboot and spring data JPA.

Spring Data JPA Code

For saving data

Html form

<%@ page language=“java” contentType=“text/html; charset=ISO-8859-1”
pageEncoding=“ISO-8859-1”%>
<!DOCTYPE html>
<html>
<head>
<meta charset=“ISO-8859-1”>
<title>Insert title here</title>
<link rel=“stylesheet” href=“https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css”>
<script src=“https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js”></script>
<script src=“https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js”></script>
<script src=“https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js”></script>
<style>
.bg
{
background:url(“yellow-bokeh-photo-949587.jpg”) no-repeat;
width: 100%;
height: 100vh;
}
.main-css{
width:400px;
background: #fff;
margin:auto;
border-radius:10px;
box-shadow:0px 0px 10px 0px;
padding:30px;
margin-top:15px;
}

</style>
</head>
<body class=“bg”>
<div class=“container “>
<form action=“savePatient” method=“post” class=“main-css” >
<div class=“form-group”>
<label for=“num”>Patient Name</label>
<input name=“name” id=“num” class = “form-control”>
</div>
<div class=“form-group”>
<label for=“num”>Patient Username Name</label>
<input name=“username” id=“num” class = “form-control”>
</div>
<div class=“form-group”>
<label for=“num”>Password</label>
<input name=“password” id=“num” class = “form-control”>
</div>
<div class=“form-group”>
<label for=“ac”>Age</label>
<input name=“age” id=“ac” class = “form-control”>
</div>
<div class=“form-group”>
<label for=“dd”>Gender</label>
<select class = “form-control” name=“gender”>
<option>select</option>
<option value=“male”>male</option>
<option value=“female”>female</option>
</select>
</div>
<div class=“form-group”>
<label for=“oa”>Address</label>
<input name=“address” id=“oa” class = “form-control”>
</div>
<div class=“form-group”>
<label for=“oa”>Mobile Number</label>
<input name=“mobile” id=“oa” class = “form-control”>
</div>
<div class=“form-group”>

<input type=“submit” name=“pwd” value=“Save” class=“btn btn-primary btn-block”>
</div>
</form>
</div>
</body>
</html>

Controller Code

@Autowired
Patientrepo ps;

@GetMapping(“/userRegister”)
public String userRegister() {
return “register”;

}

//Saving data

@PostMapping(“/savePatient”)
public String savePatient(Patient patient) {
System.out.println(patient);
ps.save(patient);

return “login”;

}

//Fetching data

@GetMapping(“/dept”)
public String Department(@RequestParam(“id”) int theid,Model model) {
System.out.println(theid);
List<Doctors> dlist = dr.findByFlag(theid);
model.addAttribute(“doctorlist”, dlist);

return “doctorlist”;

}

Entity Creation

@Entity
@Table(name=”patient”)
public class Patient {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)

private int id;
private String name;
private String gender;
private String age;
private String address;
private String mobile;
private String username;
private String password;

……………….

…………….

Repository Creation

import org.springframework.data.jpa.repository.JpaRepository;
import com.abd.hospitalmgt.entity.Patient;

public interface Patientrepo extends JpaRepository<Patient, Integer>{

}

Spread the love