Spring Boot Fetch Custom column using Interface
Purpose: In this post, we will learn how we can fetch custom columns from a database using an entity. The steps are given below.
1. Setup and Create Project: Visit here. Spring Boot CRUD Operation using in-memory database example.
Step: No new dependency is required.
Next Step. Create a new Interface only for required fields.
package com.bce.model;public interface EmployeeCustom {public Long getId();public String getFirstname();}
Next Step. Write a query in the repository.
package com.bce.repository;import java.util.List;import org.springframework.data.jpa.repository.Query;import org.springframework.data.repository.CrudRepository;import org.springframework.stereotype.Repository;import com.bce.model.Employee;import com.bce.model.EmployeeCustom;@Repositorypublic interface EmployeeRepositoory extends CrudRepository<Employee, Long> {// Using interface@Query("select e from #{#entityName} e")List<EmployeeCustom> findAllCustomEmloyee1();}
Next Step. In service add highlighted method.
package com.bce.service;import java.util.List;import org.springframework.stereotype.Service;import com.bce.model.Employee;@Servicepublic interface EmployeeService {void saveOrUpdate(Employee employee);List<Employee> getAllEmployee();Employee getEmployeeById(long id);void delete(long id);List<EmployeeCustom> getAllCustomerEmployeeData1();}
Next Step. Add below-highlighted method in service implementation.
package com.bce.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.bce.model.Employee;import com.bce.repository.EmployeeRepositoory;import com.bce.service.EmployeeService;@Servicepublic class ServiceImpl implements EmployeeService {@AutowiredEmployeeRepositoory employeeRepositoory;@Overridepublic void saveOrUpdate(Employee employee) {employeeRepositoory.save(employee);}@Overridepublic List<Employee> getAllEmployee() {return (List<Employee>) employeeRepositoory.findAll();}@Overridepublic Employee getEmployeeById(long id) {return employeeRepositoory.findById(id).get();}@Overridepublic void delete(long id) {employeeRepositoory.deleteById(id);}@Overridepublic List<EmployeeCustom> getAllCustomerEmployeeData1() {return employeeRepositoory.findAllCustomEmloyee1();}}
Next Step. Add below-highlighted method in Rest Controller.
package com.bce.controller;import java.time.LocalDate;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.DeleteMapping;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.PutMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RestController;import com.bce.exceptions.EmployeeNotFoundException;import com.bce.model.Employee;import com.bce.service.EmployeeService;@RestControllerpublic class EmployeeController {@AutowiredEmployeeService employeeService;@PostMapping("/employee")public void saveEmployee(@RequestBody Employee employee) {employeeService.saveOrUpdate(employee);}@GetMapping("/employee")public List<Employee> findAll() {return employeeService.getAllEmployee();}@GetMapping("/employeecustom1")public List<EmployeeCustom> findAllCustomEmployeeData1() {return employeeService.getAllCustomerEmployeeData1();}}
Next Step. Server Port: The default server port is 8080. Wish you want to change open application.properties and add the below line.
server.port=8081
Next Step: Test API
Hit GET URL in the browser: http://localhost:8080/employeecustom1
Download Code from GitHub. Source Code
No comments:
Post a Comment