Case Insensitive Queries with Spring Data Repository – 用Spring Data Repository进行不区分大小写的查询

最后修改: 2019年 4月 7日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Spring Data JPA queries, by default, are case-sensitive. In other words, the field value comparisons are case-sensitive.

Spring Data JPA查询,默认是区分大小写的。换句话说,字段值的比较是区分大小写的。

In this tutorial, we’ll explore how to quickly create a case insensitive query in a Spring Data JPA repository.

在本教程中,我们将探讨如何在Spring Data JPA资源库中快速创建一个不区分大小写的查询。

2. Dependencies

2.依赖性

Firstly, let’s make sure we have Spring Data and H2 database dependencies in our pom.xml:

首先,让我们确保在我们的pom.xml中拥有Spring DataH2数据库依赖项。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>2.7.2</version>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
    <version>1.4.199</version>
</dependency>

The latest versions of these are available on Maven Central.

最新版本可在Maven中心获得。

3. Initial Setup

3.初始设置

Let’s say we have a Passenger entity with id, firstName, and lastName attributes:

假设我们有一个Passenger实体,具有id、firstNamelastName属性。

@Entity
class Passenger {
 
    @Id
    @GeneratedValue
    @Column(nullable = false)
    private Long id;
 
    @Basic(optional = false)
    @Column(nullable = false)
    private String firstName;
 
    @Basic(optional = false)
    @Column(nullable = false)
    private String lastName;
 
    // constructor, static factory, getters, setters
}

Also, let’s prepare our test class by populating the database with some sample Passenger data:

另外,让我们准备一下我们的测试类,用一些样本Passenger数据填充数据库。

@DataJpaTest
@RunWith(SpringRunner.class)
public class PassengerRepositoryIntegrationTest {

    @PersistenceContext
    private EntityManager entityManager;
    @Autowired
    private PassengerRepository repository;

    @Before
    public void before() {
        entityManager.persist(Passenger.from("Jill", "Smith"));
        entityManager.persist(Passenger.from("Eve", "Jackson"));
        entityManager.persist(Passenger.from("Fred", "Bloggs"));
        entityManager.persist(Passenger.from("Ricki", "Bobbie"));
        entityManager.persist(Passenger.from("Siya", "Kolisi"));
    }
    
    //...
}

4. IgnoreCase for Case Insensitive Queries

4.IgnoreCase用于不区分大小写的查询

Now, suppose we want to perform a case-insensitive search to find all passengers with a given firstName.

现在,假设我们要进行不区分大小写的搜索,以找到所有具有给定firstName.的乘客。

To do so, we’ll define our PassengerRepository as:

为此,我们将定义我们的PassengerRepository为。

@Repository
public interface PassengerRepository extends JpaRepository<Passenger, Long> {
    List<Passenger> findByFirstNameIgnoreCase(String firstName);
}

Here, the IgnoreCase keyword ensures that the query matches are case insensitive.

这里,IgnoreCase关键字确保查询匹配不区分大小写。

We can also test that out with the help of a JUnit test:

我们也可以在JUnit测试的帮助下对其进行测试。

@Test
public void givenPassengers_whenMatchingIgnoreCase_thenExpectedReturned() {
    Passenger jill = Passenger.from("Jill", "Smith");
    Passenger eve = Passenger.from("Eve", "Jackson");
    Passenger fred = Passenger.from("Fred", "Bloggs");
    Passenger siya = Passenger.from("Siya", "Kolisi");
    Passenger ricki = Passenger.from("Ricki", "Bobbie");

    List<Passenger> passengers = repository.findByFirstNameIgnoreCase("FrED");

    assertThat(passengers, contains(fred));
    assertThat(passengers, not(contains(eve)));
    assertThat(passengers, not(contains(siya)));
    assertThat(passengers, not(contains(jill)));
    assertThat(passengers, not(contains(ricki)));
}

Despite having passed “FrED” as the argument, our returned list of passengers contains a Passenger with the firstName as “Fred”. Clearly, with the help of the IgnoreCase keyword, we have achieved a case insensitive match.

尽管我们传递了“FrED”作为参数,我们返回的乘客列表包含一个PassengerfirstName“Fred”。显然,在IgnoreCase关键字的帮助下,我们实现了一个不区分大小写的匹配。

5. Conclusion

5.总结

In this quick tutorial, we learned how to create a case insensitive query in a Spring Data repository.

在这个快速教程中,我们学习了如何在Spring Data资源库中创建一个不区分大小写的查询。

Finally, code examples are available over on GitHub.

最后,代码示例可在GitHub上获得。