Hi Welcome Readers , today we are going to understand how to do pagination and sorting by using Spring boot , here i am using Spring Tool Suit to create Spring boot project , once project is created in your IDE then please follow these steps and comment if you face any issue.
Step 1 : Add dependencies as below : Data JPA , Spring Web, Dev tools ,MySQL driver
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
Step 2: Application.properties file :
server.port=8082 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/hospital-db spring.datasource.username=root spring.datasource.password=root #spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect spring.jpa.hibernate.ddl-auto=update
Step 3: Main Class :
package com.myapp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HospitalApiApplication { public static void main(String[] args) { SpringApplication.run(HospitalApiApplication.class, args); } }
Step 4: Entity
package com.myapp.entities; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; @Entity @Table(name="patient_details") public class Patient { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Integer patientId; String patientName; String patientAddress; public Patient() { super(); } public Patient(Integer patientId, String patientName, String patientAddress) { super(); this.patientId = patientId; this.patientName = patientName; this.patientAddress = patientAddress; } public Integer getPatientId() { return patientId; } public void setPatientId(Integer patientId) { this.patientId = patientId; } public String getPatientName() { return patientName; } public void setPatientName(String patientName) { this.patientName = patientName; } public String getPatientAddress() { return patientAddress; } public void setPatientAddress(String patientAddress) { this.patientAddress = patientAddress; } @Override public String toString() { return "Patient [patientId=" + patientId + ", patientName=" + patientName + ", patientAddress=" + patientAddress+ "]"; } }
Step 5 : Repo
package com.myapp.repo; import org.springframework.data.jpa.repository.JpaRepository; import com.myapp.entities.Patient; public interface HospitalRepo extends JpaRepository<Patient, Integer> { }
Step 6 : Service
package com.myapp.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import com.myapp.entities.Patient; import com.myapp.repo.HospitalRepo; @Service public class HospitalService { @Autowired HospitalRepo hospitalRepo; public void storePatientInformation(Patient patient) { hospitalRepo.save(patient); } public Page<Patient> getAll(int pageNumber,int pageSize,String field){ Pageable p=PageRequest.of(pageNumber, pageSize).withSort(Sort.by(field)); Page<Patient> myPage= hospitalRepo.findAll(p); return myPage; } }
Step 7 : Rest Controller
package com.myapp.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.myapp.entities.Patient; import com.myapp.service.HospitalService; @RestController @RequestMapping("/hospital") public class HospitalController { @Autowired HospitalService hospitalService; @PostMapping("/save") public ResponseEntity<String> storePatientInformation(@RequestBody Patient patient) { ResponseEntity<String> msg= null; try { hospitalService.storePatientInformation(patient); msg=new ResponseEntity<String>("Patient details saved",HttpStatus.OK); } catch (Exception e) { e.printStackTrace(); msg=new ResponseEntity<String>("Failed to save ,try later",HttpStatus.INTERNAL_SERVER_ERROR); } return msg; } @GetMapping("/all") ResponseEntity<Page<Patient>> getPatients(@RequestParam(value="pageNumber" ,defaultValue = "1",required = false) int pageNumber,@RequestParam(value="pageSize" ,defaultValue = "5",required = false) int pageSize,@RequestParam(value="field" ,defaultValue = "patientId",required = false) String field){ Page<Patient> list = hospitalService.getAll(pageNumber,pageSize,field); return new ResponseEntity<Page<Patient>>(list,HttpStatus.OK); } }
Once Running Application Successfully You can save record as below using postman :
To Save Record :
http://localhost:8082/hospital/save { "patientName":"Neel", "patientAddress":"Marathi Technocrat Youtube Channel" }
To Get All Records From DB :
http://localhost:8082/hospital/all?pageNumber=1&&pageSize=10&&field=patientId
pageNumber starts with 0
pageSize you can keep any
and in field you can pass name of property by which you want to Sort the records.
Happy Learning..