tsx81428 发表于 4 天前

spring Data JPA详细介绍。

Spring Data JPA 极大地简化了数据访问层的开发,它通过约定优于设置的方式,以及对 JPA 规范的抽象,让开发者能够专注于业务逻辑而不是繁琐的 ORM 设置。下面我们将详细探讨 Spring Data JPA 是如何实现查询的,以及如何处理惩罚复杂的 SQL 和带条件的 SQL,并提供详细的分析和代码示例。
Spring Data JPA 查询实现机制

Spring Data JPA 实现查询的核心在于其对 Repository 接口的扩展和署理。它通过以下几种方式实现查询:

[*] 方法名分析 (Method Name Query Resolution): 这是 Spring Data JPA 最强大和最常用的查询方式。你只需要在 Repository 接口中界说一个符合特定定名约定的方法,Spring Data JPA 就会主动分析方法名,并根据方法名天生对应的 JPA QL (JPQL) 或 Criteria API 查询。

[*]原理: Spring Data JPA 在应用程序启动时会扫描全部继续自 Repository 或其子接口 (如 JpaRepository) 的接口。对于这些接口中界说的方法,假如方法名符合特定的模式(比方 findByLastNameAndFirstName),Spring Data JPA 会在运行时动态天生一个署理实现类,并在该实现类中构建对应的 JPQL 查询。
[*]支持的操作: findBy, readBy, getBy, countBy, deleteBy, existsBy, top, first 等前缀,联合实体属性名和操作符(And, Or, Between, LessThan, GreaterThan, Like, IsNull, IsNotNull, StartingWith, EndingWith, Containing, IgnoreCase 等)。
[*]优点: 简朴、直观、无需编写 JPQL 或原生 SQL,代码整洁。
[*]缺点: 对于非常复杂的查询,方法名可能变得非常长且难以阅读;不支持动态列选择;不支持复杂的聚合函数。

[*] @Query 注解: 当方法名分析无法满意需求时,你可以利用 @Query 注解直接在 Repository 方法上编写 JPQL 或原生 SQL。

[*]原理: Spring Data JPA 会分析 @Query 注解中提供的 JPQL 或原生 SQL 字符串,并将其作为查询语句执行。
[*]JPQL (Java Persistence Query Language): JPQL 是一种面向对象的查询语言,类似于 SQL,但操作的是实体对象及其属性,而不是数据库表和列。它与数据库无关,由 JPA 规范界说。
[*]原生 SQL (Native SQL): 当 JPQL 无法表达某些数据库特定的功能(比方,某些特别的函数、存储过程调用或复杂的联合查询)时,你可以利用 nativeQuery = true 属性来执行原生 SQL。
[*]优点: 提供了更大的机动性,可以编写恣意复杂的 JPQL 或原生 SQL;支持参数绑定。
[*]缺点: 需要手动编写查询语句,存在拼写错误或逻辑错误的风险;原生 SQL 会失去数据库无关性。

[*] Criteria API: JPA 规范提供了一个类型安全的 Criteria API,用于以编程方式构建查询。Spring Data JPA 也可以与 Criteria API 联合利用。

[*]原理: Criteria API 允许你通过 Java 代码动态构建查询,这在需要根据不同条件动态天生查询时非常有效。
[*]优点: 类型安全,避免了字符串拼接错误;更易于动态构建查询;支持复杂的逻辑组合。
[*]缺点: 语法相对复杂,编写起来比较冗长;对于简朴的查询,不如方法名分析或 @Query 简洁。

[*] Query by Example (QBE): Spring Data JPA 提供了 Query by Example 功能,允许你利用一个实体对象作为查询示例,举行简朴的等值查询。

[*]原理: QBE 会根据提供的实体对象中的非空属性主动构建查询条件。
[*]优点: 适用于简朴的等值查询,代码简洁。
[*]缺点: 不支持复杂的查询条件(如 LIKE, IN, OR 等),只支持 AND 毗连。

[*] Specification (高级查询): Spring Data JPA 联合 JPA Criteria API 提供了一个 Specification 接口,用于构建可组合的、可重用的查询条件。

[*]原理: Specification 接口允许你将查询条件封装成独立的单元,然后通过逻辑运算符(and, or, not)将它们组合起来。这在处理惩罚复杂且可变的查询条件时非常有效。
[*]优点: 极大地进步了查询的可维护性和可重用性;支持动态构建复杂查询。
[*]缺点: 相对于简朴查询,需要更多的代码。

处理惩罚复杂的 SQL 和带条件的 SQL

接下来,我们将通过详细的代码示例来展示如何处理惩罚复杂的 SQL 和带条件的 SQL。
假设我们有一个 Product 实体:
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Entity
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String description;
    private BigDecimal price;
    private Integer stockQuantity;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;

    // Getters and Setters
    public Long getId() {
      return id;
    }

    public void setId(Long id) {
      this.id = id;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public String getDescription() {
      return description;
    }

    public void setDescription(String description) {
      this.description = description;
    }

    public BigDecimal getPrice() {
      return price;
    }

    public void setPrice(BigDecimal price) {
      this.price = price;
    }

    public Integer getStockQuantity() {
      return stockQuantity;
    }

    public void setStockQuantity(Integer stockQuantity) {
      this.stockQuantity = stockQuantity;
    }

    public LocalDateTime getCreatedAt() {
      return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
      this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
      return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
      this.updatedAt = updatedAt;
    }
}
以及对应的 ProductRepository 接口:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
    // Other methods will be added here
}
1. 利用方法名分析处理惩罚带条件的查询

场景: 查找全部价格在某个范围内的产品。
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.math.BigDecimal;
import java.util.List;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {

    // 查找价格在给定范围内的产品
    List<Product> findByPriceBetween(BigDecimal minPrice, BigDecimal maxPrice);

    // 查找名称包含某个字符串且库存大于0的产品 (忽略大小写)
    List<Product> findByNameContainingIgnoreCaseAndStockQuantityGreaterThan(String name, Integer stockQuantity);

    // 查找创建时间在某个时间点之后的产品,并按创建时间降序排列
    List<Product> findByCreatedAtAfterOrderByCreatedAtDesc(LocalDateTime createdAt);
}
利用示例:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductService {

    @Autowired
    private ProductRepository productRepository;

    public List<Product> getProductsInPriceRange(BigDecimal min, BigDecimal max) {
      return productRepository.findByPriceBetween(min, max);
    }

    public List<Product> searchProducts(String name, Integer minStock) {
      return productRepository.findByNameContainingIgnoreCaseAndStockQuantityGreaterThan(name, minStock);
    }

    public List<Product> getNewProducts(LocalDateTime afterDateTime) {
      return productRepository.findByCreatedAtAfterOrderByCreatedAtDesc(afterDateTime);
    }
}
2. 利用 @Query 注解处理惩罚复杂的 SQL 和带条件的 SQL

场景:


[*]计算全部产品的均匀价格。
[*]查询库存不足的产品(库存小于某个阈值)。
[*]根据多个可选条件举行查询。
[*]执行一个复杂的原生 SQL 查询。
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {

    // 使用 JPQL 计算所有产品的平均价格
    @Query("SELECT AVG(p.price) FROM Product p")
    BigDecimal calculateAveragePrice();

    // 使用 JPQL 查询库存不足的产品
    @Query("SELECT p FROM Product p WHERE p.stockQuantity < :threshold")
    List<Product> findProductsWithLowStock(@Param("threshold") Integer threshold);

    // 使用 JPQL 进行多条件查询,条件可以是可选的
    // 注意:在 JPQL 中,LIKE 操作符需要百分号,并且 CONCAT 函数用于拼接字符串
    @Query("SELECT p FROM Product p WHERE " +
         "(:name IS NULL OR p.name LIKE %:name%) AND " +
         "(:minPrice IS NULL OR p.price >= :minPrice) AND " +
         "(:maxPrice IS NULL OR p.price <= :maxPrice)")
    List<Product> findProductsByOptionalCriteria(
            @Param("name") String name,
            @Param("minPrice") BigDecimal minPrice,
            @Param("maxPrice") BigDecimal maxPrice);

    // 使用原生 SQL 查询,例如,连接多张表并进行聚合
    // 假设我们有一个 OrderItem 实体,并且要查询每个产品被订购的总数量
    @Query(value = "SELECT p.id, p.name, SUM(oi.quantity) as totalOrdered " +
                   "FROM product p JOIN order_item oi ON p.id = oi.product_id " +
                   "GROUP BY p.id, p.name " +
                   "HAVING SUM(oi.quantity) > :minTotalOrdered",
            nativeQuery = true)
    List<Object[]> findProductsAndTotalOrderedQuantity(@Param("minTotalOrdered") Integer minTotalOrdered);

    // 使用原生 SQL 进行更新操作 (需要 @Modifying 注解)
    @Query(value = "UPDATE product SET stock_quantity = stock_quantity - :quantity WHERE id = :productId", nativeQuery = true)
    void decreaseStockQuantity(@Param("productId") Long productId, @Param("quantity") Integer quantity);
}
利用示例:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.List;

@Service
public class ProductService {

    @Autowired
    private ProductRepository productRepository;

    public BigDecimal getAverageProductPrice() {
      return productRepository.calculateAveragePrice();
    }

    public List<Product> getLowStockProducts(Integer threshold) {
      return productRepository.findProductsWithLowStock(threshold);
    }

    public List<Product> searchProductsWithDynamicCriteria(String name, BigDecimal minPrice, BigDecimal maxPrice) {
      // 在实际应用中,你可能需要根据传入的参数构建查询条件,例如:
      // if (name != null) name = "%" + name + "%"; // 如果需要模糊查询
      return productRepository.findProductsByOptionalCriteria(name, minPrice, maxPrice);
    }

    public List<Object[]> getProductsWithHighOrderVolume(Integer minOrders) {
      return productRepository.findProductsAndTotalOrderedQuantity(minOrders);
    }

    @Transactional // 对于更新操作,通常需要事务支持
    public void deductProductStock(Long productId, Integer quantity) {
      productRepository.decreaseStockQuantity(productId, quantity);
    }
}
3. 利用 Criteria API 实现动态查询


[*] Criteria API 是 JPA 规范中用于构建动态查询的一种方法。在 Spring Data JPA 中也可以利用它。下面是一个简朴的示例:
public List<User> findUsersDynamically(String name, Integer age) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> cq = cb.createQuery(User.class);
    Root<User> userRoot = cq.from(User.class);
    List<Predicate> predicates = new ArrayList<>();

    if (name != null) {
      predicates.add(cb.like(userRoot.get("name"), "%" + name + "%"));
    }
    if (age != null) {
      predicates.add(cb.greaterThan(userRoot.get("age"), age));
    }

    cq.where(predicates.toArray(new Predicate));
    return entityManager.createQuery(cq).getResultList();
} 这段代码起首创建了一个 CriteriaBuilder 和 CriteriaQuery 对象,通过 Root 对象指定查询的实体类。然后根据条件动态地添加查询谓词(Predicate),末了构建查询并执行。这种方式适用于构建复杂的、动态变化的查询条件
4. 利用 Query by Example (QBE)

场景: 举行简朴的等值查询。
Java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ProductService {

    @Autowired
    private ProductRepository productRepository;

    public List<Product> findProductsByExample(String name, BigDecimal price) {
      Product product = new Product();
      product.setName(name);
      product.setPrice(price);

      // ExampleMatcher 用于配置匹配行为,例如忽略某些属性、字符串匹配方式等
      ExampleMatcher matcher = ExampleMatcher.matching()
                .withIgnorePaths("description", "createdAt", "updatedAt", "id", "stockQuantity") // 忽略某些属性
                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains().ignoreCase()); // 名称包含且忽略大小写

      Example<Product> example = Example.of(product, matcher);

      return productRepository.findAll(example);
    }
}
5. 利用 Specification 处理惩罚动态和可组合的查询

Specification 是处理惩罚复杂、动态和可重用查询条件的最佳选择。
起首,ProductRepository 需要继续 JpaSpecificationExecutor:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long>, JpaSpecificationExecutor<Product> {
    // ...
}
接下来,界说一些 Specification:
import jakarta.persistence.criteria.Predicate;
import org.springframework.data.jpa.domain.Specification;
import java.math.BigDecimal;
import java.time.LocalDateTime;

public class ProductSpecifications {

    public static Specification<Product> hasNameLike(String name) {
      return (root, query, criteriaBuilder) -> {
            if (name == null || name.isEmpty()) {
                return criteriaBuilder.conjunction(); // 返回一个永真条件
            }
            return criteriaBuilder.like(criteriaBuilder.lower(root.get("name")), "%" + name.toLowerCase() + "%");
      };
    }

    public static Specification<Product> hasPriceBetween(BigDecimal minPrice, BigDecimal maxPrice) {
      return (root, query, criteriaBuilder) -> {
            if (minPrice == null && maxPrice == null) {
                return criteriaBuilder.conjunction();
            }
            if (minPrice != null && maxPrice != null) {
                return criteriaBuilder.between(root.get("price"), minPrice, maxPrice);
            } else if (minPrice != null) {
                return criteriaBuilder.greaterThanOrEqualTo(root.get("price"), minPrice);
            } else { // maxPrice != null
                return criteriaBuilder.lessThanOrEqualTo(root.get("price"), maxPrice);
            }
      };
    }

    public static Specification<Product> isStockGreaterThan(Integer stockQuantity) {
      return (root, query, criteriaBuilder) -> {
            if (stockQuantity == null) {
                return criteriaBuilder.conjunction();
            }
            return criteriaBuilder.greaterThan(root.get("stockQuantity"), stockQuantity);
      };
    }

    public static Specification<Product> createdAfter(LocalDateTime dateTime) {
      return (root, query, criteriaBuilder) -> {
            if (dateTime == null) {
                return criteriaBuilder.conjunction();
            }
            return criteriaBuilder.greaterThan(root.get("createdAt"), dateTime);
      };
    }
}
利用示例:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductService {

    @Autowired
    private ProductRepository productRepository;

    public List<Product> findProductsByDynamicConditions(String name, BigDecimal minPrice, BigDecimal maxPrice, Integer minStock) {
      Specification<Product> spec = Specification.where(ProductSpecifications.hasNameLike(name))
                .and(ProductSpecifications.hasPriceBetween(minPrice, maxPrice))
                .and(ProductSpecifications.isStockGreaterThan(minStock));

      return productRepository.findAll(spec);
    }

    public Page<Product> findProductsWithPaginationAndSorting(
            String name, BigDecimal minPrice, BigDecimal maxPrice, Integer minStock,
            int page, int size, String sortBy, String sortDirection) {

      Specification<Product> spec = Specification.where(ProductSpecifications.hasNameLike(name))
                .and(ProductSpecifications.hasPriceBetween(minPrice, maxPrice))
                .and(ProductSpecifications.isStockGreaterThan(minStock));

      Sort sort = Sort.by(Sort.Direction.fromString(sortDirection), sortBy);
      PageRequest pageable = PageRequest.of(page, size, sort);

      return productRepository.findAll(spec, pageable);
    }
}


总结

Spring Data JPA 通过多种方式提供了强大的查询功能,从简朴的约定式方法名查询到复杂的 Specification 和原生 SQL。


[*]方法名分析 适用于简朴、直接的查询,代码简洁。
[*]@Query 注解 提供了编写 JPQL 或原生 SQL 的机动性,适用于自界说查询和聚合。
[*]Criteria API 和 Specification 是处理惩罚复杂、动态和可重用查询条件的最佳实践,它提供了类型安全和编程构建查询的能力。
[*]Query by Example 适用于简朴的等值查询。
在实际项目中,通常会根据查询的复杂度和动态性选择符合的查询方式。对于大多数业务场景,方法名分析和 @Query 已经充足。当需要构建高度动态和可重用的查询时,Specification 是一个非常强大的工具。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: spring Data JPA详细介绍。