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> { <S extends T> S save(S entity); T findOne(ID primaryKey); Iterable<T> findAll(); Long count(); void delete(T entity); boolean exists(ID primaryKey); // … more functionality omitted. }
|
Saves the given entity. |
|
Returns the entity identified by the given id. |
|
Returns all entities. |
|
Returns the number of entities. |
|
Deletes the given entity. |
|
Indicates whether an entity with the given id exists.
|
CrudRepository接口
- 保存实体
- 通过id查找实体,并返回
- 返回所有的实体
- 返回所有的实体的数目
- 删除目标实体
- 判断一个实体是否存在
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,自定义一个方法需要下面四个步骤:
-
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> { … }
-
Declare query methods on the interface.
在接口上声明自己的方法
List<Person> findByLastname(String lastname);
-
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>
-
Get the repository instance injected and use it.
注入实例对象,就可以使用它了。
public class SomeClient { @Autowired private PersonRepository repository; public void doSomething() { List<Person> persons = repository.findByLastname("Matthews"); } }
相关推荐
repositories
repositories for sbt
清楚_remote.repositories脚本文件
创建Repository接口是第一步。例如,我们可以创建一个`UserRepositoryInterface`,定义用户相关的操作: ```php namespace App\Repositories; interface UserRepositoryInterface { public function getAll(); ...
1. **文件结构**:`repositories.xml`通常位于Maven的全局配置目录`~/.m2/settings.xml`中,或者在项目的`pom.xml`文件内。文件内部使用XML格式,包含一个或多个`<repository>`标签,每个`<repository>`标签代表一个...
快速删除maven仓库repositories.bat
在"laravel-repositories"项目中,我们很可能会看到一系列接口和实现这些接口的类,这些类通常会负责处理数据的CRUD操作(创建、读取、更新、删除)。例如,可能会有一个`UserRepository`接口,定义了如`createUser...
jarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVENjarMAVEN
maven仓库一键删除lastUpdated和_remote.repositories
`repositories` 文件在 `stable-diffusion-webui` 中扮演着核心角色,它包含了项目的源代码仓库,用于自动化安装和管理这些模型的依赖。 `CodeFormer` 可能是一个代码生成模型,它能够根据用户的指令或者已有代码...
SSM