JPA查询分为四个步骤
1、声明扩展Repository或其子接口之一的接口,并将其键入到它将处理的域类和ID类型。
interface PersonRepository extends Repository<Person, Long> { … }
2、在接口上声明查询方法。
interface PersonRepository extends Repository<Person, Long> {
List<Person> findByLastname(String lastname);
}
3、设置Spring以为这些接口创建代理实例。通过JavaConfig:
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@EnableJpaRepositories
class Config {}
4、获取注入的存储库实例并使用它。
public class SomeClient {
@Autowired
private PersonRepository repository;
public void doSomething() {
List<Person> persons = repository.findByLastname("Matthews");
}
}