0% found this document useful (0 votes)
65 views8 pages

Spring Boot With Spring Data POC

The document provides an example of using Spring Boot with Spring Data to create an Employee entity and its corresponding repository. It includes the implementation of the Employee class, EmployeeRepository interface with various query methods, and the EmployeeService class that connects to the repository for CRUD operations. The service class also demonstrates dynamic querying using Criteria API and pagination features.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views8 pages

Spring Boot With Spring Data POC

The document provides an example of using Spring Boot with Spring Data to create an Employee entity and its corresponding repository. It includes the implementation of the Employee class, EmployeeRepository interface with various query methods, and the EmployeeService class that connects to the repository for CRUD operations. The service class also demonstrates dynamic querying using Criteria API and pagination features.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

This document will give example with Spring Boot using Spring Data

The below class will give Employee entity

package [Link];

import [Link];

import [Link];

import [Link];

import [Link];

@Entity

@Table(name="employee")

public class Employee {

@Id

@Column(name="emp_id")

private Integer empId;

@Column(name="first_name")
private String firstName;

@Column(name="last_name")

private String lastName;

public Integer getEmpId() {

return empId;

public void setEmpId(Integer empId) {

[Link] = empId;

public String getFirstName() {

return firstName;

public void setFirstName(String firstName) {

[Link] = firstName;

public String getLastName() {

return lastName;

public void setLastName(String lastName) {

[Link] = lastName;

}
EmployeeRepository—this class is Repository

package [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public interface EmployeeRepository extends JpaRepository<Employee,Integer> {

@Query("FROM Employee where firstName=?1")

List<Employee> findByFirstName(String firstName);

@Query("FROM Employee where firstName=?1 and lastName=?2")

List<Employee> findByFirstAndLastName(String firstName,String lastName);

@Query("FROM Employee WHERE firstName=:fName "

+ "and lastName=:lName")

List<Employee> findByFirstAndLastNameUsingNamedParam(

@Param("lName") String lastName,

@Param("fName") String firstName);

@Query("SELECT [Link], [Link] FROM Employee emp "

+ "WHERE firstName=:fName "

+ "and lastName=:lName")
List<Object[]> getIdFromFirstAndLastName(

@Param("fName") String firstName,

@Param("lName") String lastName);

@Query(value="select * FROM Employee WHERE firstName=?1 "

+ "and lastName=?2"

,nativeQuery=true)

List<Employee> findByFirstAndLastNameUsingNativeSQL(String firstName,

String lastName);

@Query("FROM Employee WHERE firstName = ?1")

List<Employee> findByFirstName(String firstName, Pageable pageable);

The service class will connect to Repository

package [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];
import [Link];

import [Link];

import [Link];

import [Link];

@Service

@Transactional

public class EmployeeService {

@Autowired

EmployeeRepository employeeRepository;

@Autowired

EntityManager entityManager;

public Employee getEmployeeById(int id) {

return [Link](id).get();

public List<Employee> getAllEmployee() {

return [Link]();

public void saveEmployee(Employee employee) {

[Link](employee);

[Link]("employee saved");

}
public void deleteEmployee(int empId) {

Employee employee = [Link](empId).get();

[Link](employee);

public List<Employee> findByFirstName(String firstName) {

return [Link](firstName);

public List<Employee> findByFirstAndLastName(String firstName,

String lastName) {

return [Link](firstName,

lastName);

public List<Employee> findByFirstAndLastNameUsingNamedParam(

String lastName,

String firstName) {

return [Link](lastName,

firstName);

public List<Object[]> getIdFromFirstAndLastName(

String firstName,

String lastName) {

return [Link](firstName,

lastName);

}
public List<Employee> findByFirstAndLastNameUsingNativeSQL(String firstName,

String lastName) {

return [Link](

firstName,

lastName);

public List<Employee> getEmployeeData(int offset, int limit) {

/*Pageable pageable = [Link](offset,

limit);*/

OffsetBasedPageRequest pageable = new

OffsetBasedPageRequest(limit,offset);

return [Link](pageable).getContent();

public List<Employee> getDataWithSorted() {

return [Link]([Link]([Link],"firstName"));

public List<Employee> getEmployeeDataDynamically() {

CriteriaBuilder criteriaBuilder = [Link]();

CriteriaQuery<Employee> criteriaQuery =
[Link]([Link]);

Root<Employee> employeeRoot = [Link]([Link]);

[Link](employeeRoot);

//adding where

int id=4;
//[Link]([Link]([Link]("empId"),id));

//this can be considered as Predicate .


[Link]([Link]("empId"),id)

//Predicate predicate = [Link]([Link]("empId"),id);

//[Link](predicate);

//Predicate predicate = [Link]([Link]("empId"),id);

//[Link](predicate);

String firstName = "mohan";

String lastName = "babu";

Predicate predicateFirstName =
[Link]([Link]("firstName"),firstName);

Predicate predicateLastName =
[Link]([Link]("lastName"),lastName);

//Predicate prediacateFirstNameOrLastName =
[Link](predicateFirstName,predicateLastName);

//[Link](prediacateFirstNameOrLastName);

Predicate prediacateFirstNameAndLastName =
[Link](predicateFirstName,predicateLastName);

[Link](prediacateFirstNameAndLastName);

TypedQuery<Employee> typedQuery = [Link](criteriaQuery);

List<Employee> employeeList = [Link]();

return employeeList;

You might also like