Date : 27-01-2023
Spring Boot and Microservices
6AM | Mr. Raghu | (ASHOK IT)
---------------------------------------------------------------------
findBy:-
=> This is a new way of writing query.
=> No Need to write manual JPQL/HQL or SQL query.
=> Just define one abstract method using "findBy" Syntax,
that generates SELECT SQL query at runtime.
=> This is Limited, we can not define complex SELECT operations.
=> We have to follow some reserved words and syntax we need to follow same.
--Examples------------------------------------------------------
Entity : Employee (empId: Integer, empName:String, empSal: Double)
Req#1 Select * from Employee where ename=?
findBy:-
//syntax: findByVariableKeyword(DataType parameter..)
List<Employee> findByEmpName(String empName);
List<Employee> findByEmpNameIs(String empName);
List<Employee> findByEmpNameEquals(String empName);
Req#2 select * from employee where esal<=?
List<Employee> findByEmpSalLessThanEqual(Double empSal);
Req#3 select * from employee where eid!=? or ename is not null;
List<Employee> findByEmpIdNotOrEmpNameIsNotNull(Integer empId);
Req#4 select * from employee where ename like 'A%';
List<Employee> findByEmpNameLike(String input);
=====code================================================================
1. Entity
package com.app.raghu.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="emptab")
public class Employee {
@Id
@Column(name="eid")
private Integer empId;
@Column(name="ename")
private String empName;
@Column(name="esal")
private Double empSal;
@Column(name="edept")
private String empDept;
2. Repository Interface
package com.app.raghu.repo;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import com.app.raghu.entity.Employee;
public interface EmployeeRepository
extends JpaRepository<Employee, Integer>{
//SQL: select * from emptab where esal<?
List<Employee> findByEmpSalLessThan(Double esal);
//sql: select * from emptab where ename IS NOT NULL
List<Employee> findByEmpNameIsNotNull();
//sql: select * from emptab where dept=?
List<Employee> findByEmpDept(String dept);
List<Employee> findByEmpDeptIs(String dept);
List<Employee> findByEmpDeptEquals(String dept);
//sql: select * from emptab where ename like 'input'
List<Employee> findByEmpNameLike(String input);
//sql: select * from emptab where eid!=? or ename is not null;
List<Employee> findByEmpIdNotOrEmpNameIsNotNull(Integer empId);
}
3. Runner class
package com.app.raghu.runner;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.app.raghu.entity.Employee;
import com.app.raghu.repo.EmployeeRepository;
@Component
public class TestOprRunner implements CommandLineRunner {
@Autowired
private EmployeeRepository repo;
public void run(String... args) throws Exception {
repo.saveAll(Arrays.asList(
new Employee(10, "AA", 200.0, "DEV"),
new Employee(11, "AB", 300.0, "DEV"),
new Employee(12, null, 400.0, "QA"),
new Employee(13, "AD", 500.0, "QA")
));
//repo.findByEmpSalLessThan(400.0).forEach(System.out::println);
//repo.findByEmpNameIsNotNull().forEach(System.out::println);
//repo.findByEmpDept("DEV").forEach(System.out::println);
//repo.findByEmpDeptIs("DEV").forEach(System.out::println);
//repo.findByEmpDeptEquals("DEV").forEach(System.out::println);
//repo.findByEmpNameLike("A%").forEach(System.out::println);
//repo.findByEmpNameLike("A%").forEach(System.out::println);
repo.findByEmpIdNotOrEmpNameIsNotNull(10).forEach(System.out::println);
}
}
4. Properties file
#Connection Properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/boot6am
spring.datasource.username=root
spring.datasource.password=root
#ORM Properties
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
==================================================
Camel Case Rule:
=> Start method name with lower case
=> next word first letter uppercase
isMyAppWorkingStarted()