[[repositories.query-methods]]
= Query Methods

Standard CRUD functionality repositories usually have queries on the underlying datastore.
With Spring Data, declaring those queries becomes a four-step process:

. Declare an interface extending Repository or one of its subinterfaces and type it to the domain class and ID type that it should handle, as shown in the following example:
+
[source,java]
----
interface PersonRepository extends Repository<Person, Long> { … }
----

. Declare query methods on the interface.
+
[source,java]
----
interface PersonRepository extends Repository<Person, Long> {
  List<Person> findByLastname(String lastname);
}
----

. Set up Spring to create proxy instances for those interfaces, either with xref:repositories/create-instances.adoc#repositories.create-instances.java-config[JavaConfig] or with xref:repositories/create-instances.adoc[XML configuration].
+
[tabs]
======
Java::
+
[source,java,subs="attributes,specialchars",role="primary"]
----
import org.springframework.data.….repository.config.Enable{store}Repositories;

@Enable{store}Repositories
class Config { … }
----

XML::
+
[source,xml,role="secondary"]
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jpa="http://www.springframework.org/schema/data/jpa"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
     https://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/data/jpa
     https://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

   <repositories base-package="com.acme.repositories"/>

</beans>
----
======
+
ifeval::[{include-xml-namespaces} != false]
The JPA namespace is used in this example.
If you use the repository abstraction for any other store, you need to change this to the appropriate namespace declaration of your store module.
In other words, you should exchange `jpa` in favor of, for example, `mongodb`.
endif::[]
+
Note that the JavaConfig variant does not configure a package explicitly, because the package of the annotated class is used by default.
To customize the package to scan, use one of the `basePackage…` attributes of the data-store-specific repository's `@Enable{store}Repositories`-annotation.
. Inject the repository instance and use it, as shown in the following example:
+
[source,java]
----
class SomeClient {

  private final PersonRepository repository;

  SomeClient(PersonRepository repository) {
    this.repository = repository;
  }

  void doSomething() {
    List<Person> persons = repository.findByLastname("Matthews");
  }
}
----

The sections that follow explain each step in detail:

* xref:repositories/definition.adoc[Defining Repository Interfaces]
* xref:repositories/query-methods-details.adoc[Defining Query Methods]
* xref:repositories/create-instances.adoc[Creating Repository Instances]
* xref:repositories/custom-implementations.adoc[Custom Implementations for Spring Data Repositories]

