Skip to main content

spring boot data jpa custom finder methods

 spring boot data jpa finder methods



For explanation Watch video :



Directory Structure




dependencies::


lombok
Spring data JPA
Mysql Driver

application.properties


#datasource properties
spring.datasource.url=jdbc:mysql://localhost:3306/new
spring.datasource.username=root
spring.datasource.password=root

#jpa
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true


SpringBootDataJpaFinderApplication 


package com.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootDataJpaFinderApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootDataJpaFinderApplication.class, args);
}

}


Employee.java


package com.app.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
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
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "eid")
private Integer empId;
@Column(name = "ename")
private String empName;
@Column(name = "dept")
private String empDept;
@Column(name = "sal")
private Double empSal;

public Employee(String empName, String empDept, Double empSal) {
super();
this.empName = empName;
this.empDept = empDept;
this.empSal = empSal;
}
}



EmployeeRepository 


package com.app.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import com.app.model.Employee;

public interface EmployeeRepository extends JpaRepository<Employee, Integer>{

// select * from emptab where ename = name
public List<Employee> findByEmpName(String name);
// select * from emptab where dept = name
public List<Employee> findByEmpDept(String name);
// select * from emptab where ename like 'initChars%';
public Iterable<Employee> findByEmpNameStartingWith(String initChars);
// select * from emptab where ename like '%chars';
public Iterable<Employee> findByEmpNameEndingWith(String chars);
// select * from emotab where ename like '%a%'
public Iterable<Employee> findByEmpNameContaining(String chars);
// select * from emptab where ename like '%r%'
public Iterable<Employee> findByEmpNameLike(String chars);// pass valid wild chars

// select * from emptab where empId>? and empSal<?
public Iterable<Employee> findByEmpIdGreaterThanAndEmpSalLessThan(int start, Double sal);
// select * from emptab where empId>? and empSal>?
public Iterable<Employee> findByEmpIdGreaterThanAndEmpSalGreaterThan(int start, Double sal);
// select * from emptab where empName like ? or (empSal between ? and ?);
public Iterable<Employee> findByEmpNameStartingWithOrEmpSalBetween(String nameChar, Double start, Double end);
// select * from emptab where empName like ? or (empSal between ? and ?);
public Iterable<Employee> findByEmpNameStartingWithAndEmpSalBetween(String nameChar, Double start, Double end);
//select * from emptab where dept in(?,?,?) or ename like '?' and sal between ? and ?
public Iterable<Employee> findByEmpDeptInOrEmpNameLikeAndEmpSalBetween(List<String> dept,String ch,Double s,Double e);
//select * from emptab where ename not like '?' and sal(?,?) order by ename asc
public Iterable<Employee> findByEmpNameNotLikeAndEmpSalInOrderByEmpNameAsc(String letters,List<Double> l);
}


TestRunner 


package com.app.runner;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.app.repository.EmployeeRepository;

@Component
public class TestRunner implements CommandLineRunner{
@Autowired
private EmployeeRepository repo;
@Override
public void run(String... args) throws Exception {
//repo.findByEmpDept("BA").forEach(System.out::println);
//repo.findByEmpNameStartingWith("SA").forEach(System.out::println);
//repo.findByEmpNameEndingWith("la").forEach(System.out::println);
//repo.findByEmpNameContaining("j").forEach(System.out::println);
//repo.findByEmpNameLike("%la").forEach(System.out::println);
//repo.findByEmpIdGreaterThanAndEmpSalLessThan(5,6000.0).forEach(System.out::println);
//repo.findByEmpIdGreaterThanAndEmpSalGreaterThan(5,6000.0).forEach(System.out::println);
//repo.findByEmpNameStartingWithOrEmpSalBetween("A",6000.0,16000.0).forEach(System.out::println);
//repo.findByEmpNameStartingWithAndEmpSalBetween("A",6000.0,16000.0).forEach(System.out::println);
//repo.findByEmpDeptInOrEmpNameLikeAndEmpSalBetween(List.of("DEV","BA"),"k%",5000.0,6000.0).forEach(System.out::println);
repo.findByEmpNameNotLikeAndEmpSalInOrderByEmpNameAsc("S%",List.of(5000.0,6000.0)).forEach(System.out::println);
}
}





Comments

Popular posts from this blog

Servlet Project Book Shop Application in eclipse

 Servlet Project Book Shop Application in eclipse  For explanation watch video::: Note :: In this Project you must configure web server (for example tomcat) with eclipse ide Download Bootstrap  from ::         https://getbootstrap.com/docs/4.3/getting-started/download/ Download mysql jar file from :: https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.22 adding MySQL Connector/J jar file in eclipse ide for jdbc video :: video link : https://youtu.be/4Fyd-k3eG_I Directory Structure:: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">   <welcome-file-list>     <welcome-file>home.html</welcome-file>   </welcome-file-list>   <display-...

JDBC basic example For Select Query

JDBC basic example For Select Query  For explanation watch video:  For Creating Table:: SQL> create table emp60(srno int,fname varchar2(10)); Table created. SQL> desc emp60;  Name                                      Null?    Type  ----------------------------------------- -------- ----------------------------  SRNO                                               NUMBER(38)  FNAME                                              VARCHAR2(10) SQL> insert into emp60 values(1,'allu'); 1 row created. SQL> insert into emp60 values(2,'vijay'); 1 row created. SQL> insert into emp60 values(3,'rajni'); 1 row created. SQL...

JDBC Program to access table data from mysql database

 import java.sql.*; class MysqlCon  { public static void main(String[] args)  { try{ Connection con = DriverManager.getConnection("jdbc:mysql:///new","root","root"); Statement st = con.createStatement(); String query = "select * from login"; ResultSet rs = st.executeQuery(query); while(rs.next()){ System.out.println(rs.getString(1)+" "+rs.getString(2)); } con.close(); }catch(SQLException e){ System.out.println("Error"); }catch(Exception e){ } } }