实践Spring Data

Spring Data’s mission is to provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store. It makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks, and cloud-based data services. This is an umbrella project which contains many subprojects that are specific to a given database. The projects are developed by working together with many of the companies and developers that are behind these exciting technologies.

Spring Data Release Train - BOM

1
2
3
4
5
6
7
8
9
10
11
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-releasetrain</artifactId>
      <version>${release-train}</version>
      <scope>import</scope>
      <type>pom</type>
    </dependency>
  </dependencies>
</dependencyManagement>

当前release-train版本为Ingalls-SR6

Repository

Spring Data预定义的Repository接口如下:

spring-data-Repository

不同的持久化方案有不同的扩展,比如:JPA、Redis、MongoDB、Neo4j、Gemfire、LDAP、Cassandra、Solr等。

对于继承Repository或其子接口接口,称为Repository接口,Spring Data会扫描(@Enable${store}Repositories)且自动生成Repository代理实现bean定义,减少了样板代码,提升了开发效率,同时又支持自定义,保持了灵活性。如果希望Repository接口不被自动实例化,仅作为模板接口,可以在其上使用@NoRepositoryBean

扩展Repository

自定义查询方法

策略

CREATE

USE_DECLARED_QUERY

CREATE_IF_NOT_FOUND (default)

创建Query

自动实现

<查询动词><主题>by<条件>OrderBy<排序字段>Asc/Desc

查询动词:get/read/query/find/count

主题:可省略,一般是由泛型参数化决定的,Distinct则不可省略

条件:可以使用And、Or、Between、LessThan、GreaterThan或Like连接实体属性(包括嵌套属性)来定义,IgnoreCaseAllIgnoreCase

例如:

1
2
3
4
5
6
7
8
9
10
List<Person>findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);
List<Person>findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname);
List<Person>findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname);
// Enabling ignoring case for an individual property
List<Person>findByLastnameIgnoreCase(String lastname);
// Enabling ignoring case for all suitable properties
List<Person>findByLastnameAndFirstnameAllIgnoreCase(String lastname, String firstname);
// Enabling static ORDER BY for a query
List<Person>findByLastnameOrderByFirstnameAsc(String lastname);
List<Person>findByLastnameOrderByFirstnameDesc(String lastname);

delete/remove

条件
1
2
List<Person>findByAddressZipCode(ZipCode zipCode);
List<Person>findByAddress_ZipCode(ZipCode zipCode);
Pageable Slice and Sort
1
2
3
4
Page<User>findByLastname(String lastname, Pageable pageable);
Slice<User>findByLastname(String lastname, Pageable pageable);
List<User>findByLastname(String lastname, Sort sort);
List<User>findByLastname(String lastname, Pageable pageable);
first和top
Stream
Async

声明式自定义查询

自动实现

@Query

例如:

1
2
3
// mongoDB
@Query("{'customer':'Chuck Wagon','type':?0}")
List<Order> findChucksOrders(String type);

混合自定义

自定义接口,需要自己实现

实现类后缀配置,默认值是Impl

repositoryImplementationPostfix="Impl"

<SpringDataJpaRepository接口>Impl

1
2
@PersistenceContext
private EntityManager em;