spring_reference/IX. ‘How-to’ guides/67.3. Use Spring Data repos...

8 lines
1012 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 67.3. 使用Spring Data仓库
Spring Data可以为你的`@Repository`接口创建各种风格的实现。Spring Boot会为你处理所有事情只要那些`@Repositories`接口跟你的`@EnableAutoConfiguration`类处于相同的包(或子包)。
对于很多应用来说你需要做的就是将正确的Spring Data依赖添加到classpath下对于JPA有一个`spring-boot-starter-data-jpa`对于Mongodb有一个`spring-boot-starter-data-mongodb`创建一些repository接口来处理`@Entity`对象。具体参考[JPA sample](http://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-jpa)或[Mongodb sample](http://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-mongodb)。
Spring Boot会基于它找到的`@EnableAutoConfiguration`来尝试猜测你的`@Repository`定义的位置。想要获取更多控制,可以使用`@EnableJpaRepositories`注解来自Spring Data JPA