第二章,第一节,使用Repositories

1. Working with Spring Data Repositories

The goal of Spring Data repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores.

使用Spring Data 存储库

Spring Data JPA就是为了显著减少数据访问层的真正需要的代码的数量。

 

1.1 Core concepts

The central interface in Spring Data repository abstraction is Repository (probably not that much of a surprise). It takes the the domain class to manage as well as the id type of the domain class as type arguments. This interface acts primarily as a marker interface to capture the types to work with and to help you to discover interfaces that extend this one. The CrudRepository provides sophisticated CRUD functionality for the entity class that is being managed.

核心概念

Spring Data的核心接口是Repository(没什么意外的),它使用模型类和模型类的id类型作为它的类型参数,它作为一个标志接口,声明了要处理的对象的类型,帮助使用者理清其它继承于它的接口。CrudRepository提供了操作模型类的精巧的CRUD方法。

 

Example 1.1. CrudRepository interface

public interface CrudRepository<T, ID extends Serializable>
    extends Repository<T, ID> {
                                                                                                                       (1)
    <S extends T> S save(S entity);
                                                                                                                       (2)
    T findOne(ID primaryKey);
                                                                                                                       (3)
    Iterable<T> findAll();

    Long count();
                                                                                                                       (4)
    void delete(T entity);
                                                                                                                       (5)
    boolean exists(ID primaryKey);
                                                                                                                       (6)
    // … more functionality omitted.
}

1

Saves the given entity.

2

Returns the entity identified by the given id.

3

Returns all entities.

4

Returns the number of entities.

5

Deletes the given entity.

6

Indicates whether an entity with the given id exists.

 

CrudRepository接口

 

  1. 保存实体
  2. 通过id查找实体,并返回
  3. 返回所有的实体
  4. 返回所有的实体的数目
  5. 删除目标实体
  6. 判断一个实体是否存在

Usually we will have persistence technology specific sub-interfaces to include additional technology specific methods. We will now ship implementations for a variety of Spring Data modules that implement CrudRepository.

一般来讲,我们通过定义子接口来实现自定义的方法,下面我们来看一下Spring Data模块继承CrudRepository的一些接口。

On top of the CrudRepository there is a PagingAndSortingRepository abstraction that adds additional methods to ease paginated access to entities:

在继承CrudRepository接口的顶层,有一个PagingAndSortingRepository接口,能够很方便的使用分页的方法访问实体。

Example 1.2. PagingAndSortingRepository

public interface PagingAndSortingRepository<T, ID extends Serializable> 
  extends CrudRepository<T, ID> {

  Iterable<T> findAll(Sort sort);

  Page<T> findAll(Pageable pageable);
}

Accessing the second page of User by a page size of 20 you could simply do something like this:

以每页20条,访问模块类User的第二页,你可以简单的像下面这样:

PagingAndSortingRepository<User, Long> repository = // … get access to a bean
Page<User> users = repository.findAll(new PageRequest(1, 20));

1.2 Query methods

自定义查询方法

Standard CRUD functionality repositories usually have queries on the underlying datastore. With Spring Data, declaring those queries becomes a four-step process:

标准的有CRUD功能的repository一般都有面向底层数据仓库的查询方法,使用Spring Data,自定义一个方法需要下面四个步骤:

  1. Declare an interface extending Repository or one of its subinterfaces and type it to the domain class that it will handle.

    声明一个继承Repository或者它的子类的接口,并声明要处理的模型类的类型。

    public interface PersonRepository extends Repository<User, Long> { … }
  2. Declare query methods on the interface.

    在接口上声明自己的方法

    List<Person> findByLastname(String lastname);
  3. Set up Spring to create proxy instances for those interfaces.

    设置Spring,让它为你生成代理实例对象

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.springframework.org/schema/data/jpa"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    
      <repositories base-package="com.acme.repositories" />
    
    </beans>
  4. Get the repository instance injected and use it.

    注入实例对象,就可以使用它了。

    public class SomeClient {
    
      @Autowired
      private PersonRepository repository;
    
      public void doSomething() {
        List<Person> persons = repository.findByLastname("Matthews");
      }
    }

猜你喜欢

转载自limitee-god.iteye.com/blog/1966612