[关闭]
@chris-ren 2016-12-20T09:20:26.000000Z 字数 14701 阅读 2201

Spring Data 介绍

未分类


1. Spring Data概念

Spring Data旨在简化数据库的访问。针对关系型数据库,KV数据库,Document数据库,Graph数据库,Map-Reduce等一些主流数据库,采用统一技术进行访问,并且尽可能简化访问手段。

针对不同的数据储存访问使用对应的数据库库来操作访问。Spring Data中已经为我们提供了很多业务中常用的一些接口和实现类来帮我们快速构建项目,比如分页、排序、DAO一些常用的操作。

此处输入图片的描述

Spring Data 包含多个主要模块(还包括一些社区模块):


2. Spring Data JPA


2.1 Spring Data Repositories使用

2.1.1 核心接口

首先是Repository的最顶层接口:

  1. public interface Repository<T, ID extends Serializable> {
  2. }

该接口只是一个空的接口,目的是为了统一所有Repository类型,该接口使用了泛型,其中T代表实体类型,ID代表实体主键ID类型。

Repository的直接子接口CrudRepository接口:

  1. public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
  2. <S extends T> S save(S entity);
  3. <S extends T> Iterable<S> save(Iterable<S> entities);
  4. T findOne(ID id);
  5. boolean exists(ID id);
  6. Iterable<T> findAll();
  7. Iterable<T> findAll(Iterable<ID> ids);
  8. long count();
  9. void delete(ID id);
  10. void delete(T entity);
  11. void delete(Iterable<? extends T> entities);
  12. void deleteAll();

可以看到在CrudRepository接口中提供了基本的查询、保存、删除等方法。

同时还提供了一个PagingAndSortingRepository接口,增加了分页和排序功能:

  1. public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {
  2. Iterable<T> findAll(Sort sort);
  3. Page<T> findAll(Pageable pageable);
  4. }

以上这几个接口都是spring-data-commons提供的核心接口,已经为我们提供了基本的操作,如果需要实现这些方法,只需要定义一个接口继承它即可,后面我们会详细介绍使用方式。

2.1.2 JPA接口

针对spring-data-jpa也提供了一系列repository接口,有JpaRepository和JpaSpecificationExecutor:
JpaRepository:继承PagingAndSortingRepository接口,是针对JPA技术的接口,提供flush(),saveAndFlush(),deleteInBatch(),deleteAllInBatch()等方法。
【将实体和底层数据库进行同步,当调用persist、merge或者remove方法时,更新并不会立刻同步到数据库中,直到容器决定刷新到数据库中时才会执行,可以调用flush强制刷新。】
JpaSpecificationExecutor:JPA2.0提供了Criteria API,可以用于动态生成query。Spring Data JPA支持Criteria查询便是使用JpaSpecificationExecutor,后面会详细介绍。

2.1.3 查询方法

标准的增删改查需要写SQL语句查询数据库,但是在Spring Data中只需要简单的几个步骤:
1. 声明一个继承Repository或它的子接口的接口,如下:

  1. interface PersonRepository extends Repository<Person, Long> { }
2. 在接口中声明查询方法:
  1. interface PersonRepository extends Repository<Person, Long> {
  2. List<Person> findByLastname(String lastname);
  3. }

只需要声明,不需要实现。
3. 进行配置为这些接口创建代理实例:

  1. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  2. @EnableJpaRepositories
  3. class Config {}
4. 在业务中使用:
  1. public class SomeClient {
  2. @Autowired
  3. private PersonRepository repository;
  4. public void doSomething() {
  5. List<Person> persons = repository.findByLastname("Matthews");
  6. }
  7. }

2.1.4 定义repository接口

通常,我们的Repository会继承Repository, CrudRepository 或者PagingAndSortingRepository中的一个。与继承 Repository 等价的一种方式,就是在持久层接口上使用 @RepositoryDefinition 注解,并为其指定 domainClass 和 idClass 属性。如下:

  1. public interface UserDao extends Repository<User, Long> { …… }
  2. @RepositoryDefinition(domainClass = User.class, idClass = Long.class)
  3. public interface UserDao { …… }

继承CrudRepository接口会让你暴露出很多方法来操作你的实体类。如果你仅仅想暴露几个接口给其他人使用,那么你可以继承Repository,然后从CrudRepository中拷贝几个需要的方法到自己的Repository中。

  1. @NoRepositoryBean
  2. interface MyBaseRepository<T, ID extends Serializable> extends Repository<T, ID> {
  3. T findOne(ID id);
  4. T save(T entity);
  5. }
  6. interface UserRepository extends MyBaseRepository<User, Long> {
  7. User findByEmailAddress(EmailAddress emailAddress);
  8. }

多个Spring Data模块下使用Repository

2.1.5 定义查询方法

Spring Data有两种方式解析用户的查询意图:第一种是直接通过方法的命名规则解析,第二种是通过Query来解析,那么Spring Data如何选择用哪种方式呢?Spring Data有一个查询策略决定到底使用哪种方式(可以通过queryLookupStrategy属性进行配置):
CREATE:通过解析方法名来创建查询。这个策略是根据方法命名规则,删除方法中固定的前缀,然后再解析其余的部分。
USE_DECLARED_QUERY:根据已经定义好的语句去查询,如果找不到,就会抛出异常信息。
CREATE_IF_NOT_FOUND(默认):这个策略结合了以上两个策略。会优先查询是否有定义好的查询语句,如果没有,就根据方法的名字去构建查询。这是一个默认策略。

2.1.5.1 构建查询

Spring Data中有一套内置的查询构建器,它非常强大,会从方法名中剔除find…By,read…By,query…By,count…By,以及get…By等前缀,然后开始解析其余的名字。你可以在方法名中加入更多的表达式,例如你需要Distinct的约束,那么你在方法名中加入Distinct即可。在方法名中,第一个By表示查询语句的开始,你也可以用And或者Or来关联多个条件。

  1. public interface PersonRepository extends Repository<User, Long> {
  2. List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);
  3. // Enables the distinct flag for the query
  4. List<Person> findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname);
  5. List<Person> findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname);
  6. // Enabling ignoring case for an individual property
  7. List<Person> findByLastnameIgnoreCase(String lastname);
  8. // Enabling ignoring case for all suitable properties
  9. List<Person> findByLastnameAndFirstnameAllIgnoreCase(String lastname, String firstname);
  10. // Enabling static ORDER BY for a query
  11. List<Person> findByLastnameOrderByFirstnameAsc(String lastname);
  12. List<Person> findByLastnameOrderByFirstnameDesc(String lastname);
  13. }

以下表格中展示出方法名中支持的关键字:

此处输入图片的描述
此处输入图片的描述
此处输入图片的描述

2.1.5.2 属性表达式

上面说的查询方式只适合实体类上的直接属性,假设一个类Person中有个Address,并且Address还有ZipCode,那么根据ZipCode来查询这个Person应该是下面这种写法:

  1. List<Person> findByAddressZipCode(ZipCode zipCode);

在上面的例子中,框架在解析该方法时,首先剔除 findBy,然后对剩下的属性进行解析,详细规则如下:

  • 先判断 AddressZipCode (根据 POJO 规范,首字母变为小写,下同)是否为 实体的一个属性,如果是,则表示根据该属性进行查询;如果没有该属性,继续第二步;

  • 从右往左截取第一个大写字母开头的字符串(此处为 Code),然后检查剩下的字符串(AddressZip)是否为实体的一个属性,如果是,则表示根据该属性进行查询;如果没有该属性,则重复第二步,继续从右往左截取;最后解析到Address为实体的一个属性;

  • 接着处理剩下部分( ZipCode ),先判断 Address 所对应的类型是否有 ZipCode 属性,如果有,则表示该方法最终是根据 "Person.Address.ZipCode" 的取值进行查询;否则继续按照步骤 2 的规则从右往左截取。

这种解析可能会适合大部分的案例,但是有可能会出错(比如,Person中有一个Address属性,还有一个AddressZip属性),为了避免这种解析的问题,可以用“_”来区分,如:

  1. List<Person> findByAddress_ZipCode(ZipCode zipCode);
2.1.5.3 特殊参数处理

上面的例子已经展示了绑定简单的参数,除此之外,我们还可以绑定一些指定的参数,如Pageable和Sort等。

  1. Page<User> findByLastname(String lastname, Pageable pageable);
  2. Slice<User> findByLastname(String lastname, Pageable pageable);
  3. List<User> findByLastname(String lastname, Sort sort);
  4. List<User> findByLastname(String lastname, Pageable pageable);

第一个方法通过传递org.springframework.data.domain.Pageable来实现分页功能。如果需要排序功能,那么需要添加参数org.springframework.data.domain.Sort,返回的对象可以是List,也可以是Page类型。

Page与Slice:Page是知道元素和页的总数的。它是通过基础框架生成一个计数查询来计算总数的。这可能成本较高,此时可以使用Slice。Slice。只知道是不是有下一个Slice,这在遍历一个大结果集时已足够。Page像是个容器,而Slice像只是个迭代器,所以在大数据量时可以考虑用Slice。

2.1.5.4 限制查询结果

可以通过firsttop关键字限制查询结果:

  1. //不写数字默认就是1
  2. User findFirstByOrderByLastnameAsc();
  3. User findTopByOrderByAgeDesc();
  4. Slice<User> findTop3ByLastname(String lastname, Pageable pageable);
  5. List<User> findFirst10ByLastname(String lastname, Sort sort);
  6. List<User> findTop10ByLastname(String lastname, Pageable pageable);
2.1.5.5 异步查询结果
  1. //用java.util.concurrent.Future作为返回结果
  2. @Async
  3. Future<User> findByFirstname(String firstname);
  4. //用Java 8 java.util.concurrent.CompletableFuture 作为返回结果
  5. @Async
  6. CompletableFuture<User> findOneByFirstname(String firstname);
  7. //用org.springframework.util.concurrent.ListenableFuture 作为返回结果
  8. @Async
  9. ListenableFuture<User> findOneByLastname(String lastname);

要在spring中启用@Async,需要增加@EnableAsync 的配置:

  1. @Configuration
  2. @EnableAsync
  3. public class SpringAsyncConfig { ... }
2.1.5.6 使用@Query

通过@Query注解查询,可以通过JPQL语句或原生SQL进行查询。

  1. public interface UserRepository extends JpaRepository<User, Long> {
  2. @Query("select u from User u where u.emailAddress = ?1")
  3. User findByEmailAddress(String emailAddress);
  4. //like查询
  5. @Query("select u from User u where u.firstname like %?1")
  6. List<User> findByFirstnameEndsWith(String firstname);
  7. //使用原生的 SQL,原生SQL目前不支持分页
  8. @Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?1",nativeQuery = true)
  9. User findByEmailAddress(String emailAddress);
  10. //通过": 变量"的格式指定参数,同时在方法的参数前面使用 @Param 将方法参数与JPQL中的命名参数对应
  11. @Query("from AccountInfo a where a.accountId = :id")
  12. public AccountInfo findByAccountId(@Param("id")Long accountId);
  13. //用@Modifying,这样框架最终会生成一个更新的操作,而非查询。
  14. @Modifying
  15. @Query("update User u set u.firstname = ?1 where u.lastname = ?2")
  16. int setFixedFirstnameFor(String firstname, String lastname);
  17. }
2.1.5.7 使用SpEL表达式

在Spring Data JPA 1.4以后,支持在@Query中使用SpEL表达式(简介)来接收变量。
SpEL支持的变量:
此处输入图片的描述
以下的例子中,我们在查询语句中插入表达式:

  1. @Entity
  2. public class User {
  3. @Id
  4. @GeneratedValue
  5. Long id;
  6. String lastname;
  7. }
  8. public interface UserRepository extends JpaRepository<User,Long> {
  9. @Query("select u from #{#entityName} u where u.lastname = ?1")
  10. List<User> findByLastname(String lastname);
  11. }

如果想写一个通用的Repository接口,那么可以用这个表达式来处理:

  1. @MappedSuperclass
  2. public abstract class AbstractMappedType {
  3. String attribute
  4. }
  5. @Entity
  6. public class ConcreteType extends AbstractMappedType { }
  7. @NoRepositoryBean
  8. public interface MappedTypeRepository<T extends AbstractMappedType>
  9. extends Repository<T, Long> {
  10. @Query("select t from #{#entityName} t where t.attribute = ?1")
  11. List<T> findAllByAttribute(String attribute);
  12. }
  13. public interface ConcreteRepository
  14. extends MappedTypeRepository<ConcreteType> { }
2.1.5.8 Streaming查询结果

Spring Data 支持以Java 8的Stream作为查询返回结果类型:

  1. @Query("select u from User u")
  2. Stream<User> findAllByCustomQueryAndStream();
  3. Stream<User> readAllByFirstnameNotNull();
  4. @Query("select u from User u")
  5. Stream<User> streamAllPaged(Pageable pageable);

流在使用后必须进行关闭,你可以通过close() 方法或者使用Java 7的try-with-resources块:

  1. try (Stream<User> stream = repository.findAllByCustomQueryAndStream()) {
  2. stream.forEach(…);
  3. }
2.1.5.9 使用Projections

在查询时,Spring Data Repositories 一般返回实体对象,但有时候你可能需要改变展示的实体视图,这个时候你可以自己定义Projections,如下例子:

  1. @Entity
  2. public class Person {
  3. @Id @GeneratedValue
  4. private Long id;
  5. private String firstName, lastName;
  6. @OneToOne
  7. private Address address;
  8. }
  9. @Entity
  10. public class Address {
  11. @Id @GeneratedValue
  12. private Long id;
  13. private String street, state, country;
  14. }

Person里有几个属性:
主键key id;数据属性firstName和lastName;address是链接到其它实体的一个link

  1. interface PersonRepository extends CrudRepository<Person, Long> {
  2. Person findPersonByFirstName(String firstName);
  3. }

如果按照上面的方式进行查询,返回的结果会包含Person中所有的属性。
如果你不想暴露address的详细信息,你可以通过定义一个或多个 projections来提供另一种选择:

  1. //一个简单的Projection,暴露FirstName和LastName属性
  2. interface NoAddresses {
  3. String getFirstName();
  4. String getLastName();
  5. }

查询时使用如下方式:

  1. interface PersonRepository extends CrudRepository<Person, Long> {
  2. NoAddresses findByFirstName(String firstName);
  3. }

重构数据
从上面可以看出,你可以自己定义projections 来暴露一些信息,你也可以在projection中增加自己的虚拟属性:

  1. interface RenamedProperty {
  2. String getFirstName();
  3. //增加了name属性,这个属性指向lastName的值
  4. @Value("#{target.lastName}")
  5. String getName();
  6. }

按照需求对属性值进行format:

  1. interface FullNameAndCountry {
  2. @Value("#{target.firstName} #{target.lastName}")
  3. String getFullName();
  4. @Value("#{target.address.country}")
  5. String getCountry();
  6. }

高级使用,@value中可以使用表达式:
假设有如下的实体:

  1. @Entity
  2. public class User {
  3. @Id @GeneratedValue
  4. private Long id;
  5. private String name;
  6. private String password;
  7. }

假设需要对密码特殊处理,有密码时以******显示:

  1. interface PasswordProjection {
  2. @Value("#{(target.password == null || target.password.empty) ? null : '******'}")
  3. String getPassword();
  4. }
2.1.5.10 Specifications

JPA 2 引入了criteria API 建立查询,Spring Data JPA使用Specifications来实现这个API。在Repository中,你需要继承JpaSpecificationExecutor:

  1. public interface CustomerRepository extends CrudRepository<Customer, Long>, JpaSpecificationExecutor {
  2. }

JpaSpecificationExecutor接口定义如下:

  1. public interface JpaSpecificationExecutor<T> {
  2. T findOne(Specification<T> spec);
  3. List<T> findAll(Specification<T> spec);
  4. Page<T> findAll(Specification<T> spec, Pageable pageable);
  5. List<T> findAll(Specification<T> spec, Sort sort);
  6. long count(Specification<T> spec);
  7. }

Specification 接口定义如下:

  1. public interface Specification<T> {
  2. Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
  3. CriteriaBuilder builder);
  4. }

所以,我们要如何实现这个接口呢?如下定义一个Customer的Specifications :

  1. public class CustomerSpecs {
  2. public static Specification<Customer> isLongTermCustomer() {
  3. return new Specification<Customer>() {
  4. public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query,CriteriaBuilder builder) {
  5. LocalDate date = new LocalDate().minusYears(2);
  6. return builder.lessThan(root.get(_Customer.createdAt), date);
  7. }
  8. };
  9. }
  10. public static Specification<Customer> hasSalesOfMoreThan(MontaryAmount value) {
  11. return new Specification<Customer>() {
  12. public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query,
  13. CriteriaBuilder builder) {
  14. // build query here
  15. }
  16. };
  17. }
  18. }

接下来,我们如何去调用这个方法呢?

  1. List<Customer> customers = customerRepository.findAll(isLongTermCustomer());

对于上面的一个条件简单查询,其实没有必要使用Specification,当有多个条件组合查询的时候,才能显示出它的优势:

  1. MonetaryAmount amount = new MonetaryAmount(200.0, Currencies.DOLLAR);
  2. List<Customer> customers = customerRepository.findAll(
  3. where(isLongTermCustomer()).or(hasSalesOfMoreThan(amount)));
2.1.5.11 事务

默认的CRUD操作在Repository里面都是事务性的。对于查询操作,默认配置的事务是readOnly的,其他操作则配置为@Transaction。如果你想修改一个Repository的事务,只需要在子接口中重写并且修改它的事务:

  1. public interface UserRepository extends CrudRepository<User, Long> {
  2. //这里findAll方法会有10秒的超时,并且不是只读事务
  3. @Override
  4. @Transactional(timeout = 10)
  5. public List<User> findAll();
  6. // Further query method declarations
  7. }

另一种方式是在调用或者service实现层修改,一般是包含多个repository:

  1. @Service
  2. class UserManagementImpl implements UserManagement {
  3. private final UserRepository userRepository;
  4. private final RoleRepository roleRepository;
  5. @Autowired
  6. public UserManagementImpl(UserRepository userRepository,
  7. RoleRepository roleRepository) {
  8. this.userRepository = userRepository;
  9. this.roleRepository = roleRepository;
  10. }
  11. @Transactional
  12. public void addRoleToAllUsers(String roleName) {
  13. Role role = roleRepository.findByName(roleName);
  14. for (User user : userRepository.findAll()) {
  15. user.addRole(role);
  16. userRepository.save(user);
  17. }
  18. }
2.1.5.12 审计

SpringData为操作审计提供了支持。如果想要实现这些支持,仅仅需要使用几个注解或者实现接口即可。
SpringData提供了@CreatedBy,@LastModifiedBy去捕获谁操作了实体,当然还有相应的操作时间@CreatedDate和@LastModifiedDate。

  1. class Customer {
  2. @CreatedBy
  3. private User user;
  4. @CreatedDate
  5. private DateTime createdDate;
  6. // … further properties omitted
  7. }

基于接口的审计
如果你不想用注解来做审计的话,那么你可以实现Auditable接口。他暴露了审计属性的get/set方法。
如果你不想实现接口,那么你可以继承AbstractAuditable,通常来说,注解方式时更加方便的。

如果你在用@CreatedBy或者@LastModifiedBy的时候,想植入当前的业务操作者,那你可以使用AuditorAware接口。
下面给出一个案例,我们将结合SpringSecurity来做:

  1. class SpringSecurityAuditorAware implements AuditorAware<User> {
  2. public User getCurrentAuditor() {
  3. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  4. if (authentication == null || !authentication.isAuthenticated()) {
  5. return null;
  6. }
  7. return ((MyUserDetails) authentication.getPrincipal()).getUser();
  8. }
  9. }

JPA审计

SpringData JPA有一个实体监听器,他可以用于触发捕获审计信息。要用之前,你需要在orm.xml里面注册AuditingEntityListener,当然你还需要引入spring-sapects.jar:

  1. <persistence-unit-metadata>
  2. <persistence-unit-defaults>
  3. <entity-listeners>
  4. <entity-listener class="….data.jpa.domain.support.AuditingEntityListener" />
  5. </entity-listeners>
  6. </persistence-unit-defaults>
  7. </persistence-unit-metadata>

你也可以用@EntityListeners 注解为单个实体启用AuditingEntityListener :

  1. @Entity
  2. @EntityListeners(AuditingEntityListener.class)
  3. public class MyEntity {
  4. }

要启用这个审计,还需要在配置文件里面增加如下配置:

  1. <jpa:auditing auditor-aware-ref="yourAuditorAwareBean" />

Spring Data JPA 1.5以上,可以通过@EnableJpaAuditing注解启用审计:

  1. @Configuration
  2. @EnableJpaAuditing
  3. class Config {
  4. @Bean
  5. public AuditorAware<AuditableUser> auditorProvider() {
  6. return new AuditorAwareImpl();
  7. }
  8. }

2.1.6 Spring Data扩展

这部分主要介绍把SpringData扩展到其他的框架中。

2.1.6.1 Querydsl

Querydsl是一个Java开源框架,用于构建类型安全的SQL查询语句,它采用API代替拼凑字符串来构造查询语句。
QueryDslPredicateExecutor接口如下:

  1. public interface QueryDslPredicateExecutor<T> {
  2. T findOne(Predicate predicate);
  3. Iterable<T> findAll(Predicate predicate);
  4. long count(Predicate predicate);
  5. boolean exists(Predicate predicate);
  6. // … more functionality omitted.
  7. }

定义自己的Repository继承QueryDslPredicateExecutor:

  1. interface UserRepository extends CrudRepository<User, Long>, QueryDslPredicateExecutor<User> {
  2. }

调用时,通过如下方式调用:

  1. Predicate predicate = user.firstname.equalsIgnoreCase("dave")
  2. .and(user.lastname.startsWithIgnoreCase("mathews"));
  3. userRepository.findAll(predicate);
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注