JPA#Interfaces总结

本文详细解析了SpringData JPA的五个核心接口:Repository、CrudRepository、PagingAndSortingRepository、JpaRepository和JpaSpecificationExecutor,阐述了它们的功能及如何进行高效数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

_开局一张图,内容全靠编

震惊:某小白熟练使用了JpaRepository和JpaSpecificationExecutor,就在简历上写下了,精通SpringData Jpa。

 

 

 

震惊,如果想熟练的使用SpringData JPA 对数据库进行操作,只需要重点关注上图中框住的5个接口,和其他的一些相关接口。

 


 

 

接口名称要点
 Repository 1.方法命名查询。2.Query注解查询的支持。
 CrudRepository 1.继承Repository。2.Crud操作。
 PagingAndSortingRepository 1.继承了CrudRepository。2.能够分页和排序。3.无法进行复杂的查询。
 JpaRepository 1.继承了PagingAndSortingRepository。2.对返回值的类型进行了统一。3.通常与JpaSpecificationExecutor配合使用。
 JpaSpecificationExecutor 1.复杂查询的支持。2.分页排序的支持。3.通常与JpaRepository配合使用。

 

 

 

 

 

 

 

---

 


 

 --

 

其他但是同样重要的。

JpaSpecificationExecutor提供的方法如下:

涉及到了两个重要的接口,分别表示查询的where已经查询的分页信息。其中分页信息中又包含了排序信息。

org.springframework.data.domain.Pageable接口用于表示分页信息,通常用其实现类org.springframework.data.domain.PageRequest

//code--begin

// PageRequest的构造又会涉及到Sort及其内部类Direction和Order
// org.springframework.data.domain.Sort
// org.springframework.data.domain.Sort.Direction
// org.springframework.data.domain.Sort.Order


//---------------------------------------------
//排序的构造方法及其使用示列
//---------------------------------------------
/**
* Creates a new Sort instance using the given Orders.
* 
* @param orders must not be  null.
*/
public Sort(Order... orders);
/*使用示列*/
// new Sort(new Order(Direction.DESC, "id"));


/**
* Creates a new Sort instance.
* 
* @param orders must not be  null or contain  null.
*/
public Sort(List<Order> orders);
/*使用示列*/
// new Sort(Arrays.asList(new Order(Direction.DESC,"name"),new Order(Direction.ASC,"age")));

/**
* Creates a new Sort instance. Order defaults to Direction#ASC.
* 
* @param properties must not be  null or contain  null or empty strings
*/
public Sort(String... properties);
/*使用示列*/
// new Sort("name","age");


/**
* Creates a new Sort instance.
* 
* @param direction defaults to  Sort#DEFAULT_DIRECTION (for  null cases, too)
* @param properties must not be  null, empty or contain  null or empty strings.
*/
public Sort(Direction direction, String... properties);
/*使用示列*/
// new Sort(Direction.DESC,"name","age");


/**
* Creates a new Sort} instance.
* 
* @param direction defaults to  Sort#DEFAULT_DIRECTION (for  null cases, too)
* @param properties must not be  null or contain  null or empty strings.
*/
public Sort(Direction direction, List<String> properties);
/*使用示列*/
// new Sort(Direction.DESC, Arrays.asList("name","age"));





//---------------------------------------------
// PageRequest的构造方法
//---------------------------------------------
/**
* Creates a new PageRequest. Pages are zero indexed, thus providing 0 for page will return the first
* page.
* 
* @param page zero-based page index.
* @param size the size of the page to be returned.
*/
public PageRequest(int page, int size) {
    this(page, size, null);
}

/**
* Creates a new PageRequest with sort parameters applied.
* 
* @param page zero-based page index.
* @param size the size of the page to be returned.
* @param direction the direction of the Sort to be specified, can be null.
* @param properties the properties to sort by, must not be null or empty.
*/
public PageRequest(int page, int size, Direction direction, String... properties) {
    this(page, size, new Sort(direction, properties));
}

/**
* Creates a new PageRequest with sort parameters applied.
* 
* @param page zero-based page index.
* @param size the size of the page to be returned.
* @param sort can be null.
*/
public PageRequest(int page, int size, Sort sort) {
    super(page, size);
    this.sort = sort;
}

//code--end

 

 

 

 

 

 

 

 

 

 

_

 

 

 

 

_

转载于:https://www.cnblogs.com/luohaonan/p/11249040.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值