-
-
Save Danny-Hoang/aaba29d05dbfb54f44451b2c68f2087a to your computer and use it in GitHub Desktop.
Spring data Query By Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.deb.qbe.model; | |
| @Entity | |
| @Table(name = "customers") | |
| @Data | |
| @NoArgsConstructor | |
| @AllArgsConstructor | |
| @EqualsAndHashCode | |
| @ToString | |
| @Builder | |
| public class Customers implements Serializable{ | |
| private static final long serialVersionUID = 34131213L; | |
| @Id | |
| @GeneratedValue(strategy = GenerationType.AUTO) | |
| private Long id; | |
| @Column(name = "first_name", nullable = false) | |
| private String firstName; | |
| @Column(name = "last_name") | |
| private String lastName; | |
| @Column(name = "balance") | |
| private Long walletBalance; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.deb.qbe.controller; | |
| ............... | |
| @RestController | |
| @RequestMapping("/customer") | |
| @Log4j2 | |
| public class CustomerController { | |
| @Autowired | |
| private CustomerService customerService; | |
| @GetMapping("/list") | |
| public ResponseEntity<List<Customers>> getAllStates() { | |
| log.info("Request received to get all availavble customers"); | |
| return ResponseEntity.ok(customerService.getAll()); | |
| } | |
| @GetMapping("/firstname") | |
| public ResponseEntity<List<Customers>> getAllCustomersFirstNameEndsWith(@RequestParam(name = "endsWith") String endsWith) { | |
| log.info("Request received to get all customers first name ends with {}", endsWith); | |
| return ResponseEntity.ok(customerService.findByFirstNameEnding(endsWith)); | |
| } | |
| @GetMapping("/lastname") | |
| public ResponseEntity<List<Customers>> getAllCustomersLastNameEndsWith(@RequestParam(name = "endsWith") String endsWith) { | |
| log.info("Request received to get all customers last name endswith {}", endsWith); | |
| return ResponseEntity.ok(customerService.findByLastNameEnding(endsWith)); | |
| } | |
| @GetMapping("/balance") | |
| public ResponseEntity<List<Customers>> getAllCustomersLastNameEndsWith(@RequestParam(name = "balance") Long balance) { | |
| log.info("Request received to get all customers whose wallet balance is {}", balance); | |
| return ResponseEntity.ok(customerService.findByWalletBalanceEquals(balance)); | |
| } | |
| @GetMapping("/{firstName}") | |
| public ResponseEntity<List<Customers>> getCustomerByName(@PathVariable String firstName) { | |
| log.info("Request received to get all customer by name {}", firstName); | |
| return ResponseEntity.ok(customerService.findByFirstName(firstName)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.deb.qbe.service.impl; | |
| import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.*; | |
| import java.util.List; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.data.domain.Example; | |
| import org.springframework.data.domain.ExampleMatcher; | |
| import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers; | |
| import org.springframework.stereotype.Service; | |
| import com.deb.qbe.model.Customers; | |
| import com.deb.qbe.repository.CustomerRepository; | |
| import com.deb.qbe.service.CustomerService; | |
| import lombok.var; | |
| import lombok.extern.log4j.Log4j2; | |
| @Service | |
| @Log4j2 | |
| public class CustomerServiceImpl implements CustomerService{ | |
| @Autowired | |
| private CustomerRepository customerRepository; | |
| @Override | |
| public List<Customers> findByFirstNameEnding(String ending) { | |
| var customers = Customers.builder().firstName(ending).build(); | |
| var matcher = ExampleMatcher.matching() | |
| .withIgnoreNullValues() | |
| .withMatcher("firstName", match -> match.endsWith().ignoreCase(true)); | |
| var example = Example.of(customers, matcher); | |
| return (List<Customers>) customerRepository.findAll(example); | |
| } | |
| @Override | |
| public List<Customers> findByLastNameEnding(String ending) { | |
| var customers = Customers.builder().lastName(ending).build(); | |
| var matcher = ExampleMatcher.matching() | |
| .withMatcher("lastName", match -> match.endsWith().ignoreCase()); | |
| var example = Example.of(customers, matcher); | |
| return (List<Customers>) customerRepository.findAll(example); | |
| } | |
| @Override | |
| public List<Customers> getAll() { | |
| return customerRepository.findAll(); | |
| } | |
| @Override | |
| public List<Customers> findByFirstName(String firstName) { | |
| var customers = Customers.builder().firstName(firstName).build(); | |
| var matcher = ExampleMatcher.matching() | |
| .withMatcher("firstName", exact().ignoreCase()); | |
| var example = Example.of(customers, matcher); | |
| return (List<Customers>) customerRepository.findAll(example); | |
| } | |
| @Override | |
| public List<Customers> findByWalletBalanceEquals(Long balance) { | |
| var customers = Customers.builder().walletBalance(balance).build(); | |
| var matcher = ExampleMatcher.matching() | |
| .withMatcher("walletBalance", exact()); | |
| var example = Example.of(customers, matcher); | |
| return (List<Customers>) customerRepository.findAll(example); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project xmlns="http://maven.apache.org/POM/4.0.0" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| <modelVersion>4.0.0</modelVersion> | |
| <parent> | |
| <groupId>org.springframework.boot</groupId> | |
| <artifactId>spring-boot-starter-parent</artifactId> | |
| <version>2.2.6.RELEASE</version> | |
| <relativePath /> <!-- lookup parent from repository --> | |
| </parent> | |
| <groupId>com.deb.demo</groupId> | |
| <artifactId>spring-boot-qbe</artifactId> | |
| <version>0.0.1-SNAPSHOT</version> | |
| <name>spring-boot-qbe</name> | |
| <description>Spring Data JPA - Query By Example</description> | |
| <properties> | |
| <java.version>1.8</java.version> | |
| </properties> | |
| <dependencies> | |
| <dependency> | |
| <groupId>org.springframework.boot</groupId> | |
| <artifactId>spring-boot-starter-data-jpa</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.springframework.boot</groupId> | |
| <artifactId>spring-boot-devtools</artifactId> | |
| <scope>runtime</scope> | |
| <optional>true</optional> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.springframework.boot</groupId> | |
| <artifactId>spring-boot-starter-web</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.h2database</groupId> | |
| <artifactId>h2</artifactId> | |
| <scope>runtime</scope> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.projectlombok</groupId> | |
| <artifactId>lombok</artifactId> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.springframework.boot</groupId> | |
| <artifactId>spring-boot-starter-test</artifactId> | |
| <scope>test</scope> | |
| <exclusions> | |
| <exclusion> | |
| <groupId>org.junit.vintage</groupId> | |
| <artifactId>junit-vintage-engine</artifactId> | |
| </exclusion> | |
| </exclusions> | |
| </dependency> | |
| </dependencies> | |
| <build> | |
| <plugins> | |
| <plugin> | |
| <groupId>org.springframework.boot</groupId> | |
| <artifactId>spring-boot-maven-plugin</artifactId> | |
| </plugin> | |
| </plugins> | |
| </build> | |
| </project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment