Spring boot

Spring boot jpa search criteria example

This tutorial will teach how to make a search criteria using Spring boot Application. In this tutorial explain simple example of search. Enter the Employee Id on the input field click search button relevant employee name will be displayed on the below input filed.

Domain

First you must Create the package com.example.SearchEmployee.domain inside the package you have to create the class.

Employee.java

@Entity
public class Employee {
 
 @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;
    private String ename;
    private int mobile;
    private int salary;

    public Employee() {
 
 }
 public Employee(Long id, String ename, int mobile, int salary) {
 
  this.id = id;
  this.ename = ename;
  this.mobile = mobile;
  this.salary = salary;
 }

 
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 public String getEname() {
  return ename;
 }
 public void setEname(String ename) {
  this.ename = ename;
 }
 public int getMobile() {
  return mobile;
 }
 public void setMobile(int mobile) {
  this.mobile = mobile;
 }
 public int getSalary() {
  return salary;
 }
 public void setSalary(int salary) {
  this.salary = salary;
 }

 @Override
 public String toString() {
  return "Employee [id=" + id + ", ename=" + ename + ", mobile=" + mobile + ", salary=" + salary + "]";
 }
}

after that you have to Create the package com.example.SearchEmployee.Controller . inside the package you have to create the class

EmployeeController .java

@Controller
public class EmployeeContoller {
 
    @Autowired
    private EmployeeService service;

    @GetMapping("/")
    public String add(Model model) {
     List<Employee> listemployee = service.listAll();
        model.addAttribute("employee", new Employee());
        return "index";
    }

    
    @PostMapping("/search")
     public String doSearchEmployee(@ModelAttribute("employeeSearchFormData") Employee formData, Model model) {
            Employee emp = service.get(formData.getId());
            model.addAttribute("employee", emp);
            return "index";            
     }

}

First you must Create the package com.example.SearchEmployee.Service . inside the package you have to create the class

EmployeeService.java

@Service
public class EmployeeService {
 
 
 @Autowired
    private EmployeeRepository repo;
 
 public List<Employee> listAll() {
        return repo.findAll();
    }
     
    
     
    public Employee get(long id) {
        return repo.findById(id).get();
    }

}

First you must Create the package com.example.SearchEmployee.Repository inside the package you have to create the Interface here.

EmployeeRepository.java

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{

}

After that you must set database path and project configuration in application.properties file.

 

 

 

admin

Recent Posts

Laravel 11 CRUD Application

In this tutorial will teach Laravel 11 CRUD Application step by step. Laravel  11 CRUD…

3 weeks ago

How to make Times Table in React

in this tutorials we will be talk about how to make a times table in…

4 weeks ago

Laravel Tutorial: How to Add Numbers Easily Laravel 10

In this tutorials will teach How to add two numbers in Laravel 10. (more…)

1 month ago

Build Full-Stack Node.js MongoDB CRUD App with JWT Authentication

In this tutorial, we will teach the process of building a full-stack application using Node.js,…

2 months ago

Hospital Management System using OOP Java and MySQL

In this tutorial, we will explin you through the process of building a Hospital Management…

3 months ago

Mastering JavaScript OOP Inheritance

Inheritance in JavaScript using classes. You have a Person class as the superclass, an Employee…

3 months ago