基于JPA的Repository使用详解

news/2025/2/5 4:01:12/

Spring Data JPA

Spring Data是Spring提供的操作数据的框架,Spring Data JPA是Spring Data的一个模块,通过Spring data 基于jpa标准操作数据的模块。

Spring Data的核心能力,就是基于JPA操作数据,并且可以简化操作持久层的代码。

它使用一个叫作Repository的接口类为基础,它被定义为访问底层数据模型的超级接口。而对于某种具体的数据访问操作,则在其子接口中定义。

Spring Data可以让我们只定义接口,只要遵循spring data的规范,就无需写实现类,不用写sql语句直接查询数据

Repository

在这里插入图片描述

 

  • Repository

    提供了findBy + 属性 方法

  • CrudRepository
    继承了Repository 提供了对数据的增删改查

  • PagingAndSortRepository

    继承了CrudRepository 提供了对数据的分页和排序,缺点是只能对所有的数据进行分页或者排序,不能做条件判断

  • JpaRepository

    继承了PagingAndSortRepository
    开发中经常使用的接口,主要继承了PagingAndSortRepository,对返回值类型做了适配

  • JpaSpecificationExecutor

    提供多条件查询

CrudRepository

CrudRepository继承Repository,添加了一组对数据的增删改查的方法
在这里插入图片描述

PagingAndSortingRepository

PagingAndSortingRepository继承CrudRepository,添加了一组分页排序相关的方法
在这里插入图片描述

JpaRepository

JpaRepository继承PagingAndSortingRepository,添加了一组JPA规范相关的方法。对继承父接口中方法的返回值进行了适配,因为在父类接口中通常都返回迭代器,需要我们自己进行强制类型转化。而在JpaRepository中,直接返回了List。

开发中最常用JpaRepository
在这里插入图片描述

JpaSpecificationExecutor

这个接口比较特殊,单独存在,没有继承以上接口。主要提供了多条件查询的支持,并且可以在查询中添加分页和排序。因为这个接口单独存在,因此需要配合以上说的接口使用。
在这里插入图片描述

JpaRepository查询功能

Jpa方法命名规则

JpaRepository支持接口规范方法名查询。意思是如果在接口中定义的查询方法符合它的命名规则,就可以不用写实现,目前支持的关键字如下。
Keyword:关键字
Sample:举例
JPQL snippet: sql片段

KeywordSampleJPQL snippet
AndfindByNameAndPwdwhere name= ? and pwd =?
OrfindByNameOrSexwhere name= ? or sex=?
Is,EqualsfindByNameOrSexwhere id= ?
BetweenfindByIdBetweenwhere id between ? and ?
LessThanfindByIdLessThanwhere id < ?
LessThanEqualsfindByIdLessThanEqualswhere id <= ?
GreaterThanrfindByIdGreaterThanwhere id > ?
GreaterThanEqualsfindByIdGreaterThanEqualswhere id > = ?
AfterfindByIdAfterwhere id > ?
BeforefindByIdBeforewhere id < ?
IsNullfindByNameIsNullwhere name is null
isNotNull,NotNullfindByNameNotNullwhere name is not null
LikefindByNameLikewhere name like ?
NotLikefindByNameNotLikewhere name not like ?
StartingWithfindByNameStartingWithwhere name like ‘?%’
EndingWithfindByNameEndingWithwhere name like ‘%?’
ContainingfindByNameContainingwhere name like ‘%?%’
OrderByfindByIdOrderByXDescwhere id=? order by x desc
NotfindByNameNotwhere name <> ?
InfindByIdIn(Collection<?> c)where id in (?)
NotInfindByIdNotIn(Collection<?> c)where id not in (?)
TruefindByAaaTruewhere aaa = true
FalsefindByAaaFalsewhere aaa = false
IgnoreCasefindByAaaTuewhere UPPER(name)=UPPER(?)
topfindTop10top 10/where ROWNUM <=10

使用方法

使用时自定义接口继承JpaRepository,传入泛型,第一个参数为要操作的实体类,第二个参数为该实体类的主键类型

public interface SpuRepository extends JpaRepository<Spu, Long> {Spu findOneById(Long id);Page<Spu> findByCategoryIdOrderByCreateTimeDesc(Long cid, Pageable pageable);Page<Spu> findByRootCategoryIdOrderByCreateTime(Long cid, Pageable pageable);
}
  • 1
  • 2
  • 3
  • 4
  • 5


http://www.ppmy.cn/news/488678.html

相关文章

r7 5700g和5700x性能差距 5700g和5700x差多少

5700g和5700x区别如下&#xff1a; 1、5700G有显卡&#xff0c;5700X没显卡。 2、5700G TDP是95瓦&#xff0c;5700X TDP是65瓦。 3、5700G三缓为16M&#xff0c;5700X三缓是32M。 组装电脑选r7 5700g还是5700x怎么搭配更合适这些点很重要 http://www.adiannao.cn/du 5700g介绍…

i7 11700和锐龙r7 5800X 参数对比 哪个好

i7-11700 为 8 核 16 线程&#xff0c;主频 2.5 GHz&#xff0c;睿频 4.9GHz&#xff0c;16MB 三级缓存。 选i7 11700还是锐龙r7 5800X这些点很重要看完你就知道了 http://www.adiannao.cn/du R7-5800 处理器与带有 “X”后缀的型号均为 8 核 16 线程设计&#xff0c;只不过普…

r7 3700x和r7 3800x哪个性价比高 r73700x和r73800x哪个更值得买

从规格上来看&#xff0c;锐龙R7-3700X和3800X参数相差不大&#xff0c;包括架构、内存支持、制程工艺&#xff0c;核心线程数量以及三级缓存等均相同&#xff0c;只有频率和TDP功耗不同而已。 组装电脑选r7 3700x还是r7 3800x这些点很重要 看过你就懂了 http://www.adiannao.c…

批量选项-代码

<template> <div class"drawing" :style"{ height: maxHeight }"> <a-space :size"GLOBAL.spaceDefault"> <!-- 新增 --> <span class"plm-icon-btn" click"add"> <span class"opera…

【编程语言 · C语言 · 指针】

什么是指针&#xff1f; 指针在C编程中非常重要&#xff0c;因为它们使你可以轻松地处理内存位置。 指针是数组&#xff0c;字符串以及其他数据结构和算法的基础。 指针是一个变量&#xff0c;其中包含另一个变量的地址。换句话说&#xff0c;它“指向”分配给变量的位置&am…

2023最新软件测试面试题【1000道题含答案】

1、自动化代码中,用到了哪些设计模式? 单例设计模式 工厂模式PO设计模式数据驱动模式面向接口编程设计模式 2、什么是断言( Assert) ? 断言Assert用于在代码中验证实际结果是不是符合预期结果&#xff0c;如果测试用例执行失败会抛出异常并提供断言日志 3、什么是web自动化测…

国家代码电话区号中英文简写

http://链接&#xff1a;https://pan.baidu.com/s/1c0xIqn-vJo7fLK4SSxf4kw 提取码&#xff1a;ampm 其他文章有sql文件可以查看下一个文章

港澳台手机号正则表达式

大陆手机号 区号86 /^1[3-9]\d{9}$/ 香港手机号 区号852 /^([5|6|9])\d{7}$/ 澳门手机号 区号853 /^6\d{7}$/ 台湾手机号 区号886 /^[0][9]\d{8}$///使用方法 if(this.phone ){return this.$message.error(请输入手机号) }else if(this.value 86 && !/^1[0-9]{10…