我的要求是根據某些條件從資料庫中獲取資料,到目前為止,我已經使用 FindAll() 和 Find() 方法完成了獲取操作,這在從資料庫中獲取所有記錄或單個記錄時很好,但是我'我無法找到任何方法來根據條件獲取資料,就像我們在休眠條件中所做的那樣。
這是我在 Spring Data JPA 中的 Fetch 操作代碼:
控制器:
package com.iep.projectentityservice.controller;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.iep.projectentityservice.DTO.ProductDto;
import com.iep.projectentityservice.service.ProductService;
@RestController
@CrossOrigin
public class ProductController
{
private static final Logger LOGGER = LogManager.getLogger();
@Autowired
ProductService productService;
@GetMapping("/fetchproductdata")
public ResponseEntity<List<ProductDto>> fetchProductData()
{
List<ProductDto> lstProduct = productService.fetchProductData();
if(lstProduct != null && lstProduct.size() > 0)
{
return new ResponseEntity<>(lstProduct,HttpStatus.OK);
}
else
{
return new ResponseEntity<>(lstProduct,HttpStatus.NOT_FOUND);
}
}
}
服務:
package com.iep.projectentityservice.service;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.iep.projectentityservice.DTO.ProductDto;
import com.iep.projectentityservice.blueprint.schema.Products;
import com.iep.projectentityservice.dao.ProductRepository;
@Transactional
@Service
public class ProductServiceImpl implements ProductService
{
/** The Constant LOGGER. */
private static final Logger LOGGER = LogManager.getLogger();
/** The project entity DAO. */
@Autowired
ProductRepository productRepository;
@Override
public List<ProductDto> fetchProductData()
{
LOGGER.info("fetchProductData service called");
List<ProductDto> lstProduct = null;
try
{
lstProduct = new ArrayList<>();
List<Products> lstProductDetails = productRepository.findAll();
if(lstProductDetails != null && lstProductDetails.size() > 0)
{
for(int i = 0 ; i < lstProductDetails.size() ; i )
{
Products product = lstProductDetails.get(i);
ProductDto productDto = new ProductDto();
productDto.setId(product.getId());
productDto.setProductName(product.getProductName());
productDto.setDescription(product.getDescription());
productDto.setPrice(product.getPrice());
productDto.setQuantity(product.getQuantity());
lstProduct.add(productDto);
}
}
}
catch (Exception e)
{
LOGGER.error("Exception in fetchProductData service");
}
return lstProduct;
}
}
存盤庫:
package com.iep.projectentityservice.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.iep.projectentityservice.blueprint.schema.Products;
public interface ProductRepository extends JpaRepository<Products, Long>{
}
物體:
package com.iep.projectentityservice.blueprint.schema;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "product_details")
public class Products
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String productName;
private String description;
private String price;
private String quantity;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
}
DTO:
package com.iep.projectentityservice.DTO;
public class ProductDto
{
private long id;
private String productName;
private String description;
private String price;
private String quantity;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
}
正如我在代碼中所做的那樣,獲取所有記錄但無法找到一種方法來添加一些限制或標準條件,例如在獲取時在 Data JPA 中休眠。
那么,無論如何要對 Spring Data JPA fetch 操作添加限制?
uj5u.com熱心網友回復:
有4個主要選項
- 查詢方法命名
- 使用 JPQL
- 使用本機查詢
- 命名查詢
在此處查找其他鏈接
(https://www.baeldung.com/spring-data-jpa-query)
(https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation)
(https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods)
public interface ProductRepository extends JpaRepository<Products, Long>{
//Method Naming convention
Products findByProductName(String productName);
//JPQL
@Query("SELECT p FROM Products p WHERE p.productName = ?1")
Collection<Products> findAllProductsByName(String productName);
//Native Query
@Query(value = "SELECT * FROM Products p WHERE p.productName = ?1",
nativeQuery = true)
Collection<Products> findProductsByName(String productName);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/508402.html