# 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/)。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET 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?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
