├── reference.md
├── auditing.md
├── basics.md
├── locking.md
├── streaming_queryresults.md
├── introduction.md
├── jpa_auditing.md
├── save_entity.md
├── specifications.md
├── web_support.md
├── jpa_modify_query.md
├── jpa_query_creation.md
├── jpa_query_lookup.md
├── jpa_query_methods.md
├── persisting_entity.md
├── question_ask.md
├── stored_procedures.md
├── transactionality.md
├── using_query_tag.md
├── applying_query_hints.md
├── legacy_web_support.md
├── spring_namespace.md
├── styles
└── ebook.css
├── using_named_parameters.md
├── annotation_configuration.md
├── cdi_integration.md
├── config_fetch.md
├── jpa_general_auditing_config.md
├── jpa_repository.md
├── merging_persistence_units.md
├── namespace_reference.md
├── transaction_query_methods.md
├── using_jpa_namedquery.md
├── using_spel_expressions.md
├── repo_query_keywords.md
├── repository_populators.md
├── spring_data_extensions.md
├── entity_jpamappingfile.md
├── populators_namespace_reference.md
├── cover.jpg
├── book.json
├── preface.md
├── define_query_methods.md
├── creating_repository_instances.md
├── custom_impl_spring_data.md
├── define_repository_interface.md
├── README.md
├── project_metadata.md
├── standalone_usage.md
├── .gitignore
├── workingwith_spring_data_repositories.md
├── .github
└── workflows
│ └── jekyll.yml
├── query_lookup.md
├── java_config.md
├── xml_configuration.md
├── limiting_query.md
├── streaming_query_results.md
├── property_expressions.md
├── special_parameter_handling.md
├── fine_tuning_define.md
├── add_custom_all.md
├── query_methods.md
├── query_creation.md
├── add_custom_single.md
├── core_concepts.md
└── SUMMARY.md
/reference.md:
--------------------------------------------------------------------------------
1 | # 参考文档
--------------------------------------------------------------------------------
/auditing.md:
--------------------------------------------------------------------------------
1 | # 审查
2 |
3 |
--------------------------------------------------------------------------------
/basics.md:
--------------------------------------------------------------------------------
1 | # 基本审查
2 |
3 |
--------------------------------------------------------------------------------
/locking.md:
--------------------------------------------------------------------------------
1 | # 锁定
2 |
3 |
--------------------------------------------------------------------------------
/streaming_queryresults.md:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/introduction.md:
--------------------------------------------------------------------------------
1 | # 介绍
2 |
3 |
--------------------------------------------------------------------------------
/jpa_auditing.md:
--------------------------------------------------------------------------------
1 | # JPA审查
2 |
3 |
--------------------------------------------------------------------------------
/save_entity.md:
--------------------------------------------------------------------------------
1 | # 保存实体
2 |
3 |
--------------------------------------------------------------------------------
/specifications.md:
--------------------------------------------------------------------------------
1 | # 说明
2 |
3 |
--------------------------------------------------------------------------------
/web_support.md:
--------------------------------------------------------------------------------
1 | # 网络支持
2 |
3 |
--------------------------------------------------------------------------------
/jpa_modify_query.md:
--------------------------------------------------------------------------------
1 | # 修改查询
2 |
3 |
--------------------------------------------------------------------------------
/jpa_query_creation.md:
--------------------------------------------------------------------------------
1 | # 创建查询
2 |
3 |
--------------------------------------------------------------------------------
/jpa_query_lookup.md:
--------------------------------------------------------------------------------
1 | # 查询查找策略
2 |
3 |
--------------------------------------------------------------------------------
/jpa_query_methods.md:
--------------------------------------------------------------------------------
1 | # 查询方法
2 |
3 |
--------------------------------------------------------------------------------
/persisting_entity.md:
--------------------------------------------------------------------------------
1 | # 持久化实体
2 |
3 |
--------------------------------------------------------------------------------
/question_ask.md:
--------------------------------------------------------------------------------
1 | # 附录D:常见问答
2 |
3 |
--------------------------------------------------------------------------------
/stored_procedures.md:
--------------------------------------------------------------------------------
1 | # 使用存储过程
2 |
3 |
--------------------------------------------------------------------------------
/transactionality.md:
--------------------------------------------------------------------------------
1 | # 事务处理
2 |
3 |
--------------------------------------------------------------------------------
/using_query_tag.md:
--------------------------------------------------------------------------------
1 | # 使用@Query
2 |
3 |
--------------------------------------------------------------------------------
/applying_query_hints.md:
--------------------------------------------------------------------------------
1 | # 应用查询提示
2 |
3 |
--------------------------------------------------------------------------------
/legacy_web_support.md:
--------------------------------------------------------------------------------
1 | # 保留的网络支持
2 |
3 |
--------------------------------------------------------------------------------
/spring_namespace.md:
--------------------------------------------------------------------------------
1 | # Spring命名空间
2 |
3 |
--------------------------------------------------------------------------------
/styles/ebook.css:
--------------------------------------------------------------------------------
1 | /* CSS for ebook */
2 |
--------------------------------------------------------------------------------
/using_named_parameters.md:
--------------------------------------------------------------------------------
1 | # 使用命名参数
2 |
3 |
--------------------------------------------------------------------------------
/annotation_configuration.md:
--------------------------------------------------------------------------------
1 | # 基于配置的声明
2 |
3 |
--------------------------------------------------------------------------------
/cdi_integration.md:
--------------------------------------------------------------------------------
1 | # 创建Repository实例集成
2 |
3 |
--------------------------------------------------------------------------------
/config_fetch.md:
--------------------------------------------------------------------------------
1 | # 配置Fetch和LoadGraphs
2 |
3 |
--------------------------------------------------------------------------------
/jpa_general_auditing_config.md:
--------------------------------------------------------------------------------
1 | # 审查配置
2 |
3 |
--------------------------------------------------------------------------------
/jpa_repository.md:
--------------------------------------------------------------------------------
1 | # JPA Repositories
2 |
3 |
--------------------------------------------------------------------------------
/merging_persistence_units.md:
--------------------------------------------------------------------------------
1 | # 持久化单元的合并
2 |
3 |
--------------------------------------------------------------------------------
/namespace_reference.md:
--------------------------------------------------------------------------------
1 | # 附录A:命名空间参考
2 |
3 |
--------------------------------------------------------------------------------
/transaction_query_methods.md:
--------------------------------------------------------------------------------
1 | # 事务处理查询方法
2 |
3 |
--------------------------------------------------------------------------------
/using_jpa_namedquery.md:
--------------------------------------------------------------------------------
1 | # 使用JAP命名查询
2 |
3 |
--------------------------------------------------------------------------------
/using_spel_expressions.md:
--------------------------------------------------------------------------------
1 | # 使用SpEL表达式
2 |
3 |
--------------------------------------------------------------------------------
/repo_query_keywords.md:
--------------------------------------------------------------------------------
1 | # 附录C:Repository查询关键字
2 |
3 |
--------------------------------------------------------------------------------
/repository_populators.md:
--------------------------------------------------------------------------------
1 | # Repository填充器
2 |
3 |
--------------------------------------------------------------------------------
/spring_data_extensions.md:
--------------------------------------------------------------------------------
1 | # Spring Data的扩展
2 |
3 |
--------------------------------------------------------------------------------
/entity_jpamappingfile.md:
--------------------------------------------------------------------------------
1 | # 路径扫描@Entity类和JPA映射文件
2 |
3 |
--------------------------------------------------------------------------------
/populators_namespace_reference.md:
--------------------------------------------------------------------------------
1 | # 附录B:填充命名空间参考
2 |
3 |
--------------------------------------------------------------------------------
/cover.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tokyo2006/spring-data/HEAD/cover.jpg
--------------------------------------------------------------------------------
/book.json:
--------------------------------------------------------------------------------
1 | {
2 | "plugins": [
3 | "autocover"
4 | ],
5 | "pluginsConfig": {}
6 | }
--------------------------------------------------------------------------------
/preface.md:
--------------------------------------------------------------------------------
1 | # 前言
2 |
3 | GitBook allows you to organize your book into chapters, each chapter is stored in a separate file like this one.
4 |
--------------------------------------------------------------------------------
/define_query_methods.md:
--------------------------------------------------------------------------------
1 | # 定义查询方法
2 |
3 | Repository代理由两种方式从方法名字中导出特异性查询。它可以通过方法名直接导出查询,也能勇敢手动定义查询。可用选项依赖于实际的存储方式。然而这里通过策略的方式来决定哪些实际的查询被创建。让我们一起看看这些可用的选项。
--------------------------------------------------------------------------------
/creating_repository_instances.md:
--------------------------------------------------------------------------------
1 | # 创建repository实例
2 |
3 | 在这个部分你创建实例和为repository接口定义的bean。这样做的一个方法是使用Spring的名称空间,这是与每个Spring Data模块,支持存储机制,虽然我们一般建议使用的JAVA配置风格的配置。
--------------------------------------------------------------------------------
/custom_impl_spring_data.md:
--------------------------------------------------------------------------------
1 | # 自定义实现Spring Data repositories
2 |
3 | 有时候需要自定义实现一些repository的新方法。Spring Data repositories允许你很容易集成你自己实现的repostiroy功能,包括抽象的CRUD和查询功能
--------------------------------------------------------------------------------
/define_repository_interface.md:
--------------------------------------------------------------------------------
1 | # 定义repository接口
2 |
3 | 首先定义一个特别的实体Repository interface。接口必须继承自Repository并且要定义为实体类和一个ID类型。如果你想要为这个实体类实现CRUD操作,继承CrudRepository替换Repository
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Spring Data
2 | =======
3 | Spring Data 是spring 众多项目中的一个,但是我只看到了英文版,所以闲来无事自己有空就把它翻译成中文的英文原版在这里[Spring Data JPA - Reference Documentation](http://docs.spring.io/spring-data/jpa/docs/current/reference/html/)。
4 | 这个是这本书的github地址:[https://github.com/tokyo2006/spring-data.git](https://github.com/tokyo2006/spring-data.git)
5 |
--------------------------------------------------------------------------------
/project_metadata.md:
--------------------------------------------------------------------------------
1 | # 项目元数据
2 |
3 | Version control - http://github.com/spring-projects/spring-data-jpa
4 |
5 | Bugtracker - https://jira.spring.io/browse/DATAJPA
6 |
7 | Release repository - https://repo.spring.io/libs-release
8 |
9 | Milestone repository - https://repo.spring.io/libs-milestone
10 |
11 | Snapshot repository - https://repo.spring.io/libs-snapshot
--------------------------------------------------------------------------------
/standalone_usage.md:
--------------------------------------------------------------------------------
1 | # 独立使用
2 | 你也可以在spring容器外使用repository组件,比如在CDI环境中,你依然需要一些spring的libraries在你的classpath中,但通常你也能以编程的方式来搭建repositories。你可以像下面示例一样使用由Spring Data模块提供各种repository持久化支持的RepositoryFactory。
3 | ```java
4 | RepositoryFactorySupport factory = … // Instantiate factory here
5 | UserRepository repository = factory.getRepository(UserRepository.class);
6 | ```
7 |
8 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Node rules:
2 | ## Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
3 | .grunt
4 |
5 | ## Dependency directory
6 | ## Commenting this out is preferred by some people, see
7 | ## https://docs.npmjs.com/misc/faq#should-i-check-my-node_modules-folder-into-git
8 | node_modules
9 |
10 | # Book build output
11 | _book
12 |
13 | # eBook build output
14 | *.epub
15 | *.mobi
16 | *.pdf
--------------------------------------------------------------------------------
/workingwith_spring_data_repositories.md:
--------------------------------------------------------------------------------
1 | # 使用Spring Data Repositories
2 |
3 | 抽象Spring Data repository的目标是显著减少所需的数据访问层实现对各种持久存储的代码量。
4 |
5 | >Spring Data repository 文档和你的模块。
6 | 这一章节解释了Spring Data repository 的核心概念和它的接口。
7 | 这一章的信息是从Spring Data通用模块取得。它使用的是Java Persistence API(JPA)模块的配置和示例代码。 你可以使用适用于XML的命名空间和声明来扩展特定模块。命名空间参考覆盖所有被支持repositoryAPI的Spring Data 模块和支持的XML配置, Repository 查询关键字覆盖查询支持通常的repository抽象方法关键字。对于你特殊功能的详细信息可以在相关模块的章节查看。
--------------------------------------------------------------------------------
/.github/workflows/jekyll.yml:
--------------------------------------------------------------------------------
1 | name: Jekyll site CI
2 |
3 | on:
4 | push:
5 | branches: [ master ]
6 | pull_request:
7 | branches: [ master ]
8 |
9 | jobs:
10 | build:
11 |
12 | runs-on: ubuntu-latest
13 |
14 | steps:
15 | - uses: actions/checkout@v2
16 | - name: Build the site in the jekyll/builder container
17 | run: |
18 | docker run \
19 | -v ${{ github.workspace }}:/srv/jekyll -v ${{ github.workspace }}/_site:/srv/jekyll/_site \
20 | jekyll/builder:latest /bin/bash -c "chmod 777 /srv/jekyll && jekyll build --future"
21 |
--------------------------------------------------------------------------------
/query_lookup.md:
--------------------------------------------------------------------------------
1 | # 查询查找策略
2 |
3 | 以下策略可用于仓库基础结构来解决查询。你可以在XML配置中的命名空间通过query-lookup-strategy属性来配置策略或者在JAVA配置中通过Enable\$\{store\}Repositories声明queryLookupStrategy属性。有些策略可能对于特别的datastores并不支持。
4 |
5 | - CREATE 从查询方法名来尝试构建一个特别的数据查询。一般的方法都是从方法名称中移除已知设定好的前缀并且解析剩余的方法名。更多信息在[创建查询](query_creation.md)
6 | - USE_DECLARED_QUERY 尝试找到一个声明查询并在找不到的时候抛出一个异常。查询可以用声明的方式在任何地方进行定义或者被其他方法声明。查看特别的store文档来找到对这个store可用的选项.如果载入时间中在基础repository中找不到为这个方法这个声明的查询,这个查询就失败了。
7 | - CREATE_IF_NOT_FOUND (默认)结合CREATE和USE_DECLARED_QUERY。它会首先查找声明过的查询,如果找不到被声明的查询,它会创建一个以方法名为基础的自定义查询。这就是默认的查找策略,因此如果你没有明确的做任何配置就会使用默认的策略。它能够让你通过方法名快速的查询定义但也可以通过引入所需的声明查询自定义调整这些查询。
--------------------------------------------------------------------------------
/java_config.md:
--------------------------------------------------------------------------------
1 | # JavaConfig
2 |
3 | 你也可以在一个JavaConfig类中使用``` @Enable${store}Repositories ```声明来触发repository的构建。基于java类配置的spring容器介绍请参考此文档[JavaConfig in the Spring reference documentation](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-java)
4 | 一个简单的开启Spring Data repositories的配置看上去是这样的
5 | ```java
6 | @Configuration
7 | @EnableJpaRepositories("com.acme.repositories")
8 | class ApplicationConfiguration {
9 |
10 | @Bean
11 | public EntityManagerFactory entityManagerFactory() {
12 | // …
13 | }
14 | }
15 |
16 | ```
17 |
18 | > 示例使用了JPA声明,你也可以根据你实际使用的store模块替换掉这个声明,这同样也适用于定义的EntityManagerFactory bean.The same applies to the definition of the EntityManagerFactory bean. 你可以查阅不同的store实现配置。
--------------------------------------------------------------------------------
/xml_configuration.md:
--------------------------------------------------------------------------------
1 | # XML配置
2 |
3 | 每一个Spring Data模块都包含repositories元素能够让你简单的基于base-package定义来进行Spring扫描。
4 |
5 | 示例13。 通过XML来开启Spring Data repositories
6 |
7 | ```xml
8 |
9 |
16 |
17 |
18 |
19 |
20 | ```
21 |
--------------------------------------------------------------------------------
/limiting_query.md:
--------------------------------------------------------------------------------
1 | # 限制查询
2 |
3 | 查询方法的结果可以通过关键字first或者top来限制,它们可以交替使用。在top/firest后添加数字来表示返回最大的结果数。如果没有数字,则默认假定1作为结果大小。
4 |
5 | 示例10。用Top和First查询限制结果大小
6 |
7 | ```java
8 | User findFirstByOrderByLastnameAsc();
9 |
10 | User findTopByOrderByAgeDesc();
11 |
12 | Page queryFirst10ByLastname(String lastname, Pageable pageable);
13 |
14 | Slice findTop3ByLastname(String lastname, Pageable pageable);
15 |
16 | List findFirst10ByLastname(String lastname, Sort sort);
17 |
18 | List findTop10ByLastname(String lastname, Pageable pageable);
19 | ```
20 |
21 | 限制表达式也支持Distinct关键字。对于限制查询的结果集定义到一个实例中包装这个结果到一个Optional中也是被支持的。
22 |
23 | 如果分页或者切片被应用到一个限制查询分页(计算多少页可用)则它也能应用于限制结果。
24 |
25 | > 要注意结合通过Sort参数动态排序的限制结果容许表达查询的方法为“K”最小的,以及“K”最大的元素。
26 |
27 |
--------------------------------------------------------------------------------
/streaming_query_results.md:
--------------------------------------------------------------------------------
1 | # 流查询结果
2 |
3 | 查询方法能对以JAVA 8的Stream为返回的结果进行逐步处理。而不是简单地包装查询结果在被用来执行流的流数据存储特定的方法。
4 |
5 | 示例11。以JAVA 8的Stream来进行查询的流处理结果
6 |
7 | ```java
8 |
9 |
10 | @Query("select u from User u")
11 | Stream findAllByCustomQueryAndStream();
12 |
13 | Stream readAllByFirstnameNotNull();
14 |
15 | @Query("select u from User u")
16 | Stream streamAllPaged(Pageable pageable);
17 | ```
18 |
19 | > 一个数据流可能包裹底层数据存储特定资源,因此在使用后必须关闭。 你也可以使用close()方法或者JAVA 7 try-with-resources区块手动关闭数据流。
20 |
21 |
22 | 示例12.在try-with-resources块中操作一个Stream
23 |
24 | ```java
25 | try(Stream 当前不是所有的Spring Data模块都支持Stream作为返回类型
31 |
--------------------------------------------------------------------------------
/property_expressions.md:
--------------------------------------------------------------------------------
1 | # 属性表达式
2 |
3 | 正如前面的例子所示,属性表达式只能引用托管实体的直接属性。在查询创建的时候你已确认解析的属性是托管实体类的属性之一。然而你也能通过遍历嵌套属性来定义约束。假设一个Person拥有含ZipCode的Address。在这个方法名称中
4 | ```java
5 | List findByAddressZipCode(ZipCode zipCode);
6 | ```
7 | 创建了一个属性遍历x.address.zipCode。解析算法由理解实体的部分(AddressZipCode)为属性开始并检查与属性同名(未大写)的领域类。如果解析成功则使用那个属性,如果不成功,算法则会按照驼峰法则从右边开始将源拆开为头和尾并找到相应的属性,在我们的实例中,AddressZip和Code。如果算法从头那找到一个属性它会从尾部开始并继续解析,按照上面的方式来拆解尾部。如果第一个拆解不匹配,那么算法会移动到左边的拆分点(Address,ZipCode)继续分析。
8 |
9 | 虽然这个工作可能会有很多可能,它也可能因为算法选择错误的属性。假设Person类有一个addressZip属性,则算法会匹配第一个分拆点并且基本上会选择一个错误的属性并失败(就像addressZip类型属性没有code属性)。
10 |
11 | 要解决这种歧义你可以使用\_加在你的方法名字中来手动的定义遍历节点。所以我们的方法名称会看起来像这样:
12 | ```java
13 | List findByAddress_ZipCode(ZipCode zipCode);
14 | ```
15 | 我们将下划线作为保留字符 我们强烈建议遵循标志的JAVA命名规范(比如禁止使用下划线在属性命名中而是用驼峰法则替代)。
--------------------------------------------------------------------------------
/special_parameter_handling.md:
--------------------------------------------------------------------------------
1 | # 特殊参数处理
2 |
3 | 正如上面所看到的那样,在您的查询中处理参数,您只需定义方法参数。除了基本的还要识别某些特别的类型像Pageable和Sort这些在你的查询中提供动态的分页和排序。
4 |
5 | 示例9。使用Pageable,Slice和Sort在查询方法中
6 |
7 | ```java
8 | Page findByLastname(String lastname, Pageable pageable);
9 |
10 | Slice findByLastname(String lastname, Pageable pageable);
11 |
12 | List findByLastname(String lastname, Sort sort);
13 |
14 | List findByLastname(String lastname, Pageable pageable);
15 | ```
16 |
17 | 第一个方法允许在你的查询方法的静态定义查询中通过一个org.springframework.data.domain.Pageable实例来动态的添加分页。分页类知道元素的总数和可用页数。它通过基础库来触发一个统计查询计算所有的总数。由于这个查询可能对store消耗巨大,可以使用Slice来替代。Slice仅仅知道是否有下一个Slice可用,这对查询大数据已经足够了。
18 |
19 | 排序选项和分页的处理方式一样。如果你需要排序,简单的添加一个org.springframework.data.domain.Sort参数到你的方法即可。也正如你所见,简单的返回一个列表也是可以的,在这种情况下,生产的分页实例所需的附加元数据将不会被创建(这也意味着额外的计数查询可能需要但不一定被公布)。
20 |
21 | > 要找到在你的查询中有多少页,你需要触发一个额外的计数查询。按照默认来说这个查询可以从你实际触发查询中衍生出来
--------------------------------------------------------------------------------
/fine_tuning_define.md:
--------------------------------------------------------------------------------
1 | # 调整repository定义
2 |
3 | 通常你的Repository interface将会继承自Repository,CrudRepository或者PagingAndSortingRepository。另外,如果你不想继承Spring Data的接口,你也能通过@RepositoryDefinition声明你的Repository interface。继承自CrudRepository会暴露一系列的方法来操作你的实体。如果你喜欢选择性的暴露某些方法,简单的从CrudRepository拷贝你想要暴露的方法到你的domain repository就可以了。
4 |
5 | > 这允许你在Srping Data提供的功能上定义你自己的抽象。
6 |
7 |
8 | 示例7。选择性暴露CRUD方法
9 |
10 | ```java
11 | @NoRepositoryBean
12 | interface MyBaseRepository extends Repository{
13 | T findOne(ID id);
14 | T save(T entity);
15 | }
16 |
17 | interface UserRepository extends MyBaseRepository{
18 | User findByEmailAddress(EmailAddress emailAddress);
19 | }
20 | ```
21 |
22 | 你首先为你所有的领域接口定义了一个通用的基础接口并且暴露了findOne(...)和save(...)方法。这些方法会路由到由你选择的Spring Data数据处理方式实现基础仓库中,例如JPA SimpleJapRepository,因为他们在CrudRepository匹配到了这些方法签名。所以UserRepository可以保存用户而且可以通过id找到用户,同样的也可以通过他们的邮件地址查询找到这些用户。
--------------------------------------------------------------------------------
/add_custom_all.md:
--------------------------------------------------------------------------------
1 | # 为所有的repositories添加自定义行为
2 |
3 | 之前的章节并没有实现当你想把一个方法添加到所有的repository接口中。要添加一个自定义行为到所有的repository中,你首先需要添加一个中介接口来声明一个共享的行为。
4 |
5 | _Example 27. An interface declaring custom shared behavior_
6 |
7 | ```java
8 | @NoRepositoryBean
9 | public interface MyRepository
10 | extends PagingAndSortingRepository {
11 |
12 | void sharedCustomMethod(ID id);
13 | }
14 | ```
15 | 现在你自己的repository需要扩展这个中介接口来替换之前包含方法声明的Repository接口,接着创建一个扩展持久化repository基础类的中介接口的实现类,这个类之后会作为代理自定义的repository基础类。
16 |
17 | _Example 28. Custom repository base class_
18 |
19 | ```java
20 | public class MyRepositoryImpl
21 | extends SimpleJpaRepository implements MyRepository {
22 |
23 | private final EntityManager entityManager;
24 |
25 | public MyRepositoryImpl(JpaEntityInformation entityInformation,
26 | EntityManager entityManager) {
27 | super(entityInformation, entityManager);
28 |
29 | // Keep the EntityManager around to used from the newly introduced methods.
30 | this.entityManager = entityManager;
31 | }
32 |
33 | public void sharedCustomMethod(ID id) {
34 | // implementation goes here
35 | }
36 | }
37 | ```
38 |
39 |
--------------------------------------------------------------------------------
/query_methods.md:
--------------------------------------------------------------------------------
1 | # 查询方法
2 |
3 | 标准的CRUD功能的repositories通常在数据存储底层都有各种查询。使用Spring Data,声明这些查询有以下四步流程。
4 |
5 | 1. 声明一个继承于Repository的接口或者一个子接口并注入实体类和它的ID类型
6 | ```java
7 | interface PersonRepository extends Repository { … }
8 | ```
9 | 1.
10 | 在接口中声明查询方法
11 | ```java
12 | interface PersonRepository extends Repository {
13 | List findByLastname(String lastname);
14 | }
15 | ```
16 | 1.
17 | 使用Spring来为这些接口创建代理实例。也可以通过[JavaConfig](java_config.md):
18 |
19 | ```java
20 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
21 |
22 | @EnableJpaRepositories
23 | class Config {}
24 | ```
25 |
26 | 或者通过[XML配置](xml_configuration.md):
27 |
28 | ```xml
29 |
30 |
37 |
38 |
39 |
40 |
41 | ```
42 |
43 | JPA的命名空间也在这个例子中。如果你正在使用其他抽象repository,你需要改变相应的命名空间来替换jpa的声明支持你得存储模块,例如mongodb.并且注意JavaConfig的变量并不需要配置一个明确的包如同默认使用的声明类。定制包扫描。
44 |
45 | 4.
46 | 获取repository实例注入并使用它。
47 | ```java
48 | public class SomeClient {
49 |
50 | @Autowired
51 | private PersonRepository repository;
52 |
53 | public void doSomething() {
54 | List persons = repository.findByLastname("Matthews");
55 | }
56 | }
57 | ```
58 |
59 | 以下的章节将会详细解释这些步骤
60 |
61 |
--------------------------------------------------------------------------------
/query_creation.md:
--------------------------------------------------------------------------------
1 | # 创建查询
2 |
3 | 基础Spring Data repository内置的查询生成器机制对于创建实体仓库的约束查询是有用的,它会从方法名中去掉find...By,read...By,query...By,count...By和get...By这些前缀并解析剩下的内容.这些前缀还能包含更多的表达式例如Distinct,设置一个distinct标志并在查询中创建它,然后第一个By的动作就像一个分隔符来表明查询实际标准的开始。最基本的方式你可以在实体属性中定义表达式并用AND和OR连接它们。
4 |
5 | ```java
6 | public interface PersonRepository extends Repository{
7 | List findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);
8 |
9 | //Enables the distinct flag for the query
10 | List findDistinctPeopleByLastnameOrFirstname(String lastname,String firstname);
11 |
12 | List findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname);
13 |
14 | // Enabling ignoring case for an individual property
15 | List findByLastnameIgnoreCase(String lastname);
16 | // Enabling ignoring case for all suitable properties
17 | List findByLastnameAndFirstnameAllIgnoreCase(String lastname, String firstname);
18 |
19 | // Enabling static ORDER BY for a query
20 | List findByLastnameOrderByFirstnameAsc(String lastname);
21 | List findByLastnameOrderByFirstnameDesc(String lastname);
22 | }
23 | ```
24 |
25 | 解析方法实际的结果依赖于你创建查询的持久存储。然后这里还算是有一些需要注意的事情。
26 |
27 | - 表达式通常会结合级联的操作来进行属性遍历。你可以结合表达式属性AND和OR。对于属性表达式你也可以使用可支持的操作比如Between,LessThan,GreaterThan ,Like。这些支持的操作由不同的数据存储而不同,所以你需要查看你参考的文档中合适的部分。
28 | - 方法解析支持为某些属性设置一个IgnoreCase标志(例如,findByLastnameIgnoreCase(...))或者为所有属性都支持忽略类型(通常是String实例,例如,findByLastnameAndFirstnameAllIgnoreCase(...))。是否支持ignoring cases 可能根据store不同而不同,所以相关部分在特殊库查询方法的参考文档中。
29 | - 你可以通过增加一个OrderBy字段在按照属性来升降序的查询方法中用来静态排序。想要创建一个查询方法能够支持动态排序,请看[特殊参数处理](special_parameter_handling.md)
--------------------------------------------------------------------------------
/add_custom_single.md:
--------------------------------------------------------------------------------
1 | # 为单一的repositories添加自定义行为
2 | 为了给repository添加更丰富的自定义功能,首先你需要定义一个接口和实现这个接口中的方法。使用你提供的repostiroy接口来扩展自定义接口
3 |
4 | _Example 22. Interface for custom repository functionality_
5 |
6 | ```java
7 | interface UserRepositoryCustom {
8 | public void someCustomMethod(User user);
9 | }
10 | ```
11 |
12 | _Example 23. Implementation of custom repository functionality_
13 |
14 | ```java
15 | class UserRepositoryImpl implements UserRepositoryCustom {
16 |
17 | public void someCustomMethod(User user) {
18 | // Your custom implementation
19 | }
20 | }
21 | ```
22 | > 在实现的类中一定要加上**Impl**这个后缀,这点很重要
23 |
24 | 实现类本身并没有依赖任何的Spring Data,所以实现类也是一个正常的spring bean,所以你可以使用标准的依赖注入行为将它注入到其它bean中比如jdbcTemplate,诸如此类。
25 |
26 | _Example 24. Changes to the your basic repository interface_
27 |
28 | ```java
29 | interface UserRepository extends CrudRepository, UserRepositoryCustom {
30 |
31 | // Declare query methods here
32 | }
33 | ```
34 |
35 | 让你的标准repository扩展为一个自定义的。组合CRUD和自定义的方法并让其在客户端可用。
36 |
37 | ## 配置
38 |
39 | 如果你使用命名空间配置,repository构件会在定义的类包中自动扫描自定义实现。这些自定义类必须按照repository-impl-postfix的命名规则命名,默认的后缀名为Impl
40 |
41 | _Example 25. Configuration example_
42 |
43 | ```xml
44 |
45 |
46 |
47 | ```
48 |
49 | 第一个配置示例会查找类com.acme.repository.UserRepositoryImpl来作为自定义repository的实现类,而第二个示例则会尝试查找com.acme.repository.UserRepositoryFooBar
50 |
51 | ## 手动连接
52 |
53 | 上面的示例展示了定义实现使用基础声明的自动连接,如何其它spring bean一样。如果自定义实现需要一些特别的连接,仅需要按照规则简单的声明和命名,构建就会参考手动定义的bean名字,而不是自动创建一个。
54 |
55 | _Example 26. Manual wiring of custom implementations_
56 |
57 | ```xml
58 |
59 |
60 |
61 |
62 |
63 | ```
64 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/core_concepts.md:
--------------------------------------------------------------------------------
1 | # 核心概念
2 |
3 | 在Spring Data repository 抽象的接口中心是仓库(Repository).它使得领域类的管理好比领域类的id类型就像一个类型参数,这个接口的行为主要是作为一个标记接库来捕获各种类型工作并帮助你发现接库并扩展它。CrudRepository为所管理的实体类提供复杂的CRUD功能。
4 | 事例1.CrudRepository 接口
5 |
6 | ```java
7 | public interface CrudRepository extends Repository{
8 |
9 | S save(S entity); //1
10 |
11 | T findOne(ID primaryKey); //2
12 |
13 | Iterable findAll(); //3
14 |
15 | Long count(); //4
16 |
17 | void delete(T entity); //5
18 |
19 | boolean exists(ID primaryKey); //6
20 |
21 | // … more functionality omitted.
22 | }
23 | ```
24 | 1.保存所给实例
25 |
26 | 2.返回所给ID的实例
27 |
28 | 3.返回所有实例
29 |
30 | 4.返回实例数量
31 |
32 | 5.删除所给实例
33 |
34 | 6.是否存在所给的ID实例
35 |
36 | >我们还提供了特别技术的持久性抽象,例如JpaRepository或者MongoRepository.这些接口扩展了CrudRepository和暴露了在基本持久技术中增加的潜在持久行技术接口,例如增删改查
37 |
38 | 在CrudRepository之上还有一个PagingAndSortingReporitory的抽象,它使得实体对象更容易进行分页
39 |
40 | 事例2. PagingAndSortingReposiory
41 |
42 | ```java
43 | public interface PagingAndSortingRepository
44 | extends CrudRepository {
45 |
46 | Iterable findAll(Sort sort);
47 |
48 | Page findAll(Pageable pageable);
49 | }
50 | ```
51 |
52 | 访问每页20条数据第二页的用户信息你可以简单的这样做:
53 |
54 | ```java
55 | PagingAndSortingRepository repository = // … get access to a bean
56 | Page users = repository.findAll(new PageRequest(1, 20));
57 | ```
58 |
59 | 在增加的查询方法中,关键字的计数和删除查询都是有效的
60 |
61 | 事例3. 关键字计数查询
62 |
63 | ```java
64 | public interface UserRepository extends CrudRepository {
65 |
66 | Long countByLastname(String lastname);
67 | }
68 | ```
69 |
70 | 事例4. 关键字删除查询
71 |
72 | ```java
73 | public interface UserRepository extends CrudRepository {
74 |
75 | Long deleteByLastname(String lastname);
76 |
77 | List removeByLastname(String lastname);
78 |
79 | }
80 | ```
81 |
82 |
--------------------------------------------------------------------------------
/SUMMARY.md:
--------------------------------------------------------------------------------
1 | # Summary
2 |
3 | * [介绍](README.md)
4 | * [前言](preface.md)
5 | * [项目元数据](project_metadata.md)
6 | * [使用Spring Data Repositories](workingwith_spring_data_repositories.md)
7 | * [核心概念](core_concepts.md)
8 | * [查询方法](query_methods.md)
9 | * [定义repository接口](define_repository_interface.md)
10 | * [调整repository定义](fine_tuning_define.md)
11 | * [定义查询方法](define_query_methods.md)
12 | * [查询查找策略](query_lookup.md)
13 | * [创建查询](query_creation.md)
14 | * [属性表达式](property_expressions.md)
15 | * [特殊参数处理](special_parameter_handling.md)
16 | * [限制查询](limiting_query.md)
17 | * [流查询结果](streaming_query_results.md)
18 | * [创建repository实例](creating_repository_instances.md)
19 | * [XML配置](xml_configuration.md)
20 | * [JavaConfig](java_config.md)
21 | * [独立使用](standalone_usage.md)
22 | * [自定义实现Spring Data repositories](custom_impl_spring_data.md)
23 | * [为单一的repositories添加自定义行为](add_custom_single.md)
24 | * [为所有的repositories添加自定义行为](add_custom_all.md)
25 | * [Spring Data的扩展](spring_data_extensions.md)
26 | * [网络支持](web_support.md)
27 | * [Repository填充器](repository_populators.md)
28 | * [保留的网络支持](legacy_web_support.md)
29 | * 参考文档
30 | * [JPA Repositories](jpa_repository.md)
31 | * [介绍](introduction.md)
32 | * [Spring命名空间](spring_namespace.md)
33 | * [基于配置的声明](annotation_configuration.md)
34 | * [持久化实体](persisting_entity.md)
35 | * [保存实体](save_entity.md)
36 | * [查询方法](jpa_query_methods.md)
37 | * [查询查找策略](jpa_query_lookup.md)
38 | * [创建查询](jpa_query_creation.md)
39 | * [使用JAP命名查询](using_jpa_namedquery.md)
40 | * [使用@Query](using_query_tag.md)
41 | * [使用命名参数](using_named_parameters.md)
42 | * [使用SpEL表达式](using_spel_expressions.md)
43 | * [修改查询](jpa_modify_query.md)
44 | * [应用查询提示](applying_query_hints.md)
45 | * [配置Fetch和LoadGraphs](config_fetch.md)
46 | * [使用存储过程](stored_procedures.md)
47 | * [说明](specifications.md)
48 | * [事务处理](transactionality.md)
49 | * [事务处理查询方法](transaction_query_methods.md)
50 | * [锁定](locking.md)
51 | * [审查](auditing.md)
52 | * [基本审查](basics.md)
53 | * [JPA审查](jpa_auditing.md)
54 | * [审查配置](jpa_general_auditing_config.md)
55 | * 杂项
56 | * [持久化单元的合并](merging_persistence_units.md)
57 | * [路径扫描@Entity类和JPA映射文件](entity_jpamappingfile.md)
58 | * [创建Repository实例集成](cdi_integration.md)
59 | * 附录
60 | * [附录A:命名空间参考](namespace_reference.md)
61 | * [附录B:填充命名空间参考](populators_namespace_reference.md)
62 | * [附录C:Repository查询关键字](repo_query_keywords.md)
63 | * [附录D:常见问答](question_ask.md)
64 |
65 |
--------------------------------------------------------------------------------