Spring Data JPA 方法命名约定规则

509 阅读3分钟

Spring Data JPA 方法命名约定规则

Spring Data JPA 提供了一种通过方法命名约定来自动生成查询的方法。通过遵循特定的命名规则,你可以在不编写实际查询的情况下定义复杂的查询逻辑。

1. 基本规则

方法名由 findByreadByqueryBycountBygetBy 等关键字开始,后跟属性名和条件。

2. 属性名

属性名应该与实体类中的属性名一致,并且首字母大写。

3. 条件关键字

可以使用以下关键字来构建复杂的查询条件:

  • And:用于连接多个条件
  • Or:用于连接多个条件
  • IsEquals:等于
  • Between:在两者之间
  • LessThanLessThanEqual:小于、小于等于
  • GreaterThanGreaterThanEqual:大于、大于等于
  • AfterBefore:在某个时间之后、之前
  • IsNullIsNotNull:为空、不为空
  • LikeNotLike:类似、不类似
  • StartingWithEndingWithContaining:以...开始、以...结束、包含
  • OrderBy:排序

4. 示例

以下是一些常见的示例:

示例实体类

假设我们有一个 User 实体类:

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class User {
    @Id
    private Long id;
    private String username;
    private String email;
    private Integer age;
    private Boolean active;

    // Getters and setters
}

示例 Repository 接口

import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;

public interface UserRepository extends JpaRepository<User, Long> {
    // 使用 findBy
    List<User> findByUsername(String username);
    List<User> findByAgeGreaterThan(Integer age);
    List<User> findByActiveTrue();
    List<User> findByUsernameStartingWith(String prefix);
    List<User> findByEmailContaining(String infix);
    List<User> findByAgeBetween(Integer startAge, Integer endAge);
    List<User> findByUsernameAndActive(String username, Boolean active);
    List<User> findByUsernameOrEmail(String username, String email);
    List<User> findByOrderByAgeDesc();

    // 使用 readBy
    List<User> readByUsername(String username);
    List<User> readByAgeGreaterThan(Integer age);
    List<User> readByActiveTrue();

    // 使用 queryBy
    List<User> queryByEmailContaining(String infix);
    List<User> queryByAgeBetween(Integer startAge, Integer endAge);

    // 使用 countBy
    long countByActiveTrue();
    long countByUsername(String username);

    // 使用 getBy
    List<User> getByUsernameAndActive(String username, Boolean active);
    List<User> getByUsernameOrEmail(String username, String email);
}

详细示例解释

1. 使用 findBy

findBy 方法用于查找数据。

// 查找所有用户名为指定值的用户
List<User> findByUsername(String username);

// 查找所有年龄大于指定值的用户
List<User> findByAgeGreaterThan(Integer age);

// 查找所有活跃的用户
List<User> findByActiveTrue();

2. 使用 readBy

readBy 方法用于读取数据,语义上与 findBy 类似。

// 查找所有用户名为指定值的用户
List<User> readByUsername(String username);

// 查找所有年龄大于指定值的用户
List<User> readByAgeGreaterThan(Integer age);

// 查找所有活跃的用户
List<User> readByActiveTrue();

3. 使用 queryBy

queryBy 方法用于查询数据,语义上与 findBy 类似。

// 查找所有邮箱包含指定字符串的用户
List<User> queryByEmailContaining(String infix);

// 查找所有年龄在两个值之间的用户
List<User> queryByAgeBetween(Integer startAge, Integer endAge);

4. 使用 countBy

countBy 方法用于统计满足条件的数据条目数。

// 统计所有活跃用户的数量
long countByActiveTrue();

// 统计所有用户名为指定值的用户数量
long countByUsername(String username);

5. 使用 getBy

getBy 方法用于获取数据,语义上与 findBy 类似。

// 查找所有用户名为指定值且活跃的用户
List<User> getByUsernameAndActive(String username, Boolean active);

// 查找所有用户名为指定值或邮箱为指定值的用户
List<User> getByUsernameOrEmail(String username, String email);

复杂查询示例

你可以组合多个条件来创建复杂的查询:

import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;

public interface UserRepository extends JpaRepository<User, Long> {
    // 使用 findBy
    List<User> findByAgeGreaterThanAndActiveTrue(Integer age);
    List<User> findByUsernameStartingWithAndEmailContaining(String prefix, String infix);
    List<User> findByAgeBetweenAndActiveTrueOrderByUsernameAsc(Integer startAge, Integer endAge);

    // 使用 readBy
    List<User> readByAgeGreaterThanAndActiveTrue(Integer age);

    // 使用 queryBy
    List<User> queryByUsernameStartingWithAndEmailContaining(String prefix, String infix);

    // 使用 countBy
    long countByAgeBetweenAndActiveTrue(Integer startAge, Integer endAge);

    // 使用 getBy
    List<User> getByAgeBetweenAndActiveTrueOrderByUsernameAsc(Integer startAge, Integer endAge);
}

分页和排序

Spring Data JPA 还支持分页和排序:

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
    // 查找所有活跃的用户并分页
    Page<User> findByActiveTrue(Pageable pageable);

    // 查找所有用户名为指定值的用户并排序
    List<User> findByUsername(String username, Sort sort);
}

总结

通过遵循 Spring Data JPA 的方法命名约定,你可以轻松地创建复杂的查询,而无需编写实际的查询语句。这种方法不仅提高了代码的可读性,还减少了错误的可能性。