> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/iv.-spring-boot-features/29.-working-with-sql-databases/29.3.-jpa-and-spring-data/29.3.2.-spring-data-jpa-repositories.md).

# 29.3.2. Spring Data JPA仓库

[Spring Data JPA](http://projects.spring.io/spring-data-jpa/)仓库是用来定义访问数据的接口。根据你的方法名，JPA查询会被自动创建，比如，一个`CityRepository`接口可能声明一个`findAllByState(String state)`方法，用来查找给定状态的所有城市。

对于比较复杂的查询，你可以使用Spring Data的[`Query`](https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/Query.html)注解你的方法。

Spring Data仓库通常继承自[`Repository`](https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/Repository.html)或[`CrudRepository`](https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html)接口。如果你使用自动配置，Spring Boot会搜索主配置类（注解`@EnableAutoConfiguration`或`@SpringBootApplication`的类）所在包下的仓库。

下面是典型的Spring Data仓库接口定义：

```java
package com.example.myapp.domain;

import org.springframework.data.domain.*;
import org.springframework.data.repository.*;

public interface CityRepository extends Repository<City, Long> {

    Page<City> findAll(Pageable pageable);

    City findByNameAndCountryAllIgnoringCase(String name, String country);
}
```

**注**：我们仅仅触及了Spring Data JPA的表面，具体查看它的[参考指南](https://docs.spring.io/spring-data/jpa/docs/current/reference/html/)。
