springData学习(三)CrudRepository接口学习

CrudRepository接口的save方法的记录判断 实现CrudRepository接口的save方法进行记录更新时,遇到了更新记录反而插入新纪录的情况,经过探究发现,还是对源码不熟,对该方法的逻辑未清晰。下面通过这个bug来探究CrudRepository接口的save方法的记录判断。 首先看下CrudRepository接口的save方法的源码: @Transactional public <S extends T> S save(S entity) { if (entityInformation.isNew(en 阅读详情

首先看一下CrudRepository接口里面有哪些方法。

save(S entity)insert一条数据
Iterable<S> saveAll(Iterable<S> entities);批量插入多条数据
findById(ID id);根据id查找数据
existsById(ID id);判断id是否存在
findAll();查找所有
findAllById(Iterable<ID> ids);根据ids批量查找
count();返回数量
deleteById(ID id);根据id删除
delete(T entity);根据一个实体类删除
deleteAll(Iterable<? extends T> entities);批量删除
deleteAll();全部删除

接下来以此测试。跟上一篇文章不一样的是,在进行插入和删除的时候需要事务,所以新建一个service层来添加事务。

1.save方法

dao层:
public interface StudentCrud extends CrudRepository<Student, Integer>{

}

service层:
@Service
public class StudentCrudService {

	@Autowired
	private StudentCrud studentCrud;
	
	@Transactional
	public void save(Student student) {
		studentCrud.save(student);
	}
}

测试类:
@Test
	public void testSave() {
		Student student = new Student();
		student.setAge(18);
		student.setName("测试save");
		student.setScore((double)10);
		studentCrudService.save(student);
	}

需要注意的是:要在实体类的id字段上面加一个注解

@Id
	@GeneratedValue(strategy= GenerationType.IDENTITY)
	private Integer id;

2.saveAll方法

service层:
@Transactional
	public void save(List<Student> sList) {
		studentCrud.saveAll(sList);
	}
测试类:
@Test 
	public void testSaveAll() { 
		 List<Student> sList = new
		 ArrayList<Student>(); for(int i=0;i<50;i++) { Student student = new
		 Student(); student.setName("测试"+i); student.setAge(50-i);
		 student.setScore((double)i); sList.add(student); }
		 studentCrudService.save(sList); 
	 }

3.findById方法

service层:
public Student findById(Integer id) {
		Optional<Student> student = studentCrud.findById(id);
		return student.get();
	}

测试类:
@Test
	public void testFindById() {
		Student s = studentCrudService.findById(10);
		System.out.println(s.toString());
	}

4.existsById方法

service层:
public boolean existsById(Integer id) {
		boolean flag = studentCrud.existsById(id);
		return flag;
	}

测试类:
@Test
	public void testExistById() {
		boolean flag = studentCrudService.existsById(5);
		System.out.println(flag);
	}

5.findAll方法

服务层:
public List<Student> findAll(){
		List<Student> sList = (List<Student>) studentCrud.findAll();
		return sList;
	}

测试类:
@Test
	public void testFindAll() {
		List<Student> sList = studentCrudService.findAll();
		sList.forEach(System.out::println);
	}

6.findAllById方法

service层
public List<Student> findAllById(List<Integer> sList){
		List<Student> studentList = (List<Student>) studentCrud.findAllById(sList);
		return studentList;
	}

测试类:
@Test
	public void testFindAllById() {
		List<Integer> sList = new ArrayList<Integer>();
		sList.add(1);
		sList.add(2);
		sList.add(3);
		sList.add(4);
		sList.add(5);
		List<Student> studentList =  studentCrudService.findAllById(sList);
		studentList.forEach(System.out::println);
	}

其他方法其实都一样。这里就不进行测试了

 

SpringDataJPA之CrudRepository接口 Repository接口,该接口并没有提供完成的DML操作,SpringDataJPA中完成的CRUD操作是通过CrudRepository接口来实现的。 CrudRepository接口   CrudRepository接口继承自Repository接口,所以Repository接口具有的功能CrudRepository接口都具有,同时又扩展了CRUD相关的功能 API方法介绍   Crud... 阅读详情

相关推荐

SpringData核心数据访问接口--CrudRepository示例

1.Spring Data JPA可以极大地简化JPA的写法,在几乎不用写接口实现的情况下完成对数据的访问和操作。 2.Spring Data通过提供Repository接口来约定数据访问的统一标准。 3.Repository接口下包含一些常用的子接口:    CrudRepository、PagingAndSortingRepository、JpaRepository 4.在进行spri...

BADAO_LIUMANG_QIZHI的博客 4491

CrudRepository基础

CrudRepository基础 一.基础 CrudRepository接口提供了最基本的对实体类的添删改查操作 –T save(Tentity);//保存单个实体 –Iterable save(Iterable<?extends T> entities);//保存集合 –T findOne(IDid);//根据id查找实体 –boolean exists(IDid);//根据id判断...

arkitocy的博客 1万+

SpringData核心数据访问接口--CrudRepository示例代码

SpringData核心数据访问接口--CrudRepository,SpringData核心数据访问接口--CrudRepository示例代码

springboot之CrudRepository的简单使用

上面的代码中,使用 @Autowired 注解将 UserRepository 接口注入到 UserService 中,然后通过 CrudRepository 的方法完成对数据库的操作。上面的代码中,使用 @Query 注解定义了 Native SQL 查询语句,其中 value 属性表示查询语句,nativeQuery 属性表示是否使用原生 SQL。上面的代码中,使用方法名 findByUsername 定义了根据用户名查询用户的方法,Spring Data JPA 会自动生成查询语句。

m0_63486787的博客 3352

MATLAB闪退别慌!5分钟搞定Windows系统预设文件修复(附详细截图)

本文针对MATLAB在Windows系统下频繁闪退的问题,提供了快速有效的解决方案。核心在于修复或重置损坏的MATLAB预设文件,通过图文并茂的五步法,指导用户定位AppData目录、重命名预设文件夹,从而让MATLAB自动生成全新配置以解决闪退。文章还涵盖了问题诊断、设置备份及进阶排查方法,帮助用户彻底根除启动故障。

weixin_29234423的博客 925

从零构建:Esp32+Esp-IDF驱动ST7789屏幕并集成LVGL图形库

本文详细介绍了如何从零开始使用Esp32和Esp-IDF驱动ST7789屏幕并集成LVGL图形库。内容涵盖环境准备、SPI驱动配置、屏幕初始化、LVGL图形库集成及常见问题排查,帮助开发者快速实现高效稳定的显示解决方案。

weixin_42657024的博客 524

SpringData中的CrudRepository接口

CrudRepository 接口提供了最基本的对实体类的添删改查操作 1.T save(T entity);//保存单个实体 2.Iterable save(Iterable entities);//保存集合         3.T findOne(ID id);//根据id查找实体          4.boolean exists(ID id);//根据id判断实体是否存在

Strive_Peter的博客 1万+

【dify】 dify环境变量配置说明

SMTP_SERVER、SMTP_PORT、SMTP_USERNAME、SMTP_PASSWORD、SMTP_USE_TLS、SMTP_OPPORTUNISTIC_TLS:当 MAIL_TYPE 为 smtp 时,SMTP服务器的相关配置。UPLOAD_IMAGE_FILE_SIZE_LIMIT、UPLOAD_VIDEO_FILE_SIZE_LIMIT、UPLOAD_AUDIO_FILE_SIZE_LIMIT:分别为上传图像、视频、音频文件的大小限制(MB)。

tonysh_zds的博客 7355

SpringData Modifying注解与CrudRepository 接口

一、Modifying 注解在@Qurey中使用DELETE UPDATE 语句1、可用通过自定义JPQL来完成Update 和 Delete 操作,注意JPQL不支持Insert 2、在使用@Query注解的时候,必须使用@Modifying注解从而来通知SpringData,这是一个Update 或者 Delete操作 3、Update 和Delete操作需要事务,就必须在Serivce层定义...

weixin_33910434的博客 424

PCL 点云拟合 最小二乘法拟合平面

在点云处理领域,最小二乘法(Least Squares)拟合是一种常用的数学方法,用于估计数据点的最佳拟合曲线或平面。平面拟合广泛应用于点云数据处理中的地面检测、物体表面分析等任务。 本文将介绍如何使用PCL库实现基于最小二乘法的平面拟合算法,通过拟合点云数据中的平面,来提取出数据的平面部分。

纵马踏花向自由 1026

S7-1200PLC 2轴直线插补详细算法(完整源代码)

速度轴V90PN总线伺服梯形加减速速度控制(标准报文1应用)_v90伺服加减速时间怎么调整-优快云博客文章浏览阅读288次。

RXXW_Dor的博客 4255

SpringDataJPA的CrudRepository接口详解

在用SpringData(JPA是操作关系型的数据库)是个非常不错的框架,开发人员省去了创建数据库表格的时间,与此同时还简化了持久层的代码量.在SpringData中目前为止最核心的是Repository接口,但是里面没有任何的方法: public interface Repository<T, ID extends Serializable> Repository还有许多的额子接口...

weixin_44664095的博客 7127

西安交通大学软件学院——分布式系统练习题(综合题)

摘要:本文探讨了分布式系统的核心特性与关键问题。首先阐述了分布式系统的8种透明性特性,包括访问、定位、迁移等。然后通过具体案例分析了分布式事务的回滚机制、域名解析的迭代与递归过程、不同分布式系统的异同比较。重点讨论了RPC通信中客户端/服务器崩溃的处理方法,包括消灭、再生和到期三种策略。最后介绍了Lamport时间戳算法在进程通信中的应用及问题解决方案。全文涵盖了分布式系统设计中的透明性、可靠性、一致性等核心问题。

qq_73044452的博客 204

SpringData之repository接口详解

SpringData官方文档以下是本人阅读各种大牛博客 + 官方文档总结的知识点 避免大家学习走弯路 总结如下Repository 接口是 Spring Data 的一个核心接口,它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法 与继承 Repository 等价的一种方式。@RepositoryDefinition 注解,并为其指定 domainClass 和 idClas

just xiaoqiang 4757

本地玩转AI还得靠这个,SillyTavern(酒馆)到底有多强?

SillyTavern是一款本地运行的LLM交互前端,支持文本生成、图像生成、语音合成等功能。它最初是TavernAI的分支,现已独立开发,社区活跃,功能更新频繁。SillyTavern通过前端UI和插件系统连接各种大模型后端,如OpenAI、KoboldAI等,用户可以在本地创建“虚拟酒馆”与各种角色对话。其功能亮点包括角色卡、角色记忆、世界设定、多人聊天室、自定义UI和行为脚本、插件扩展支持等。SillyTavern适合AI创作用户、AI工程师、LLM爱好者和AIGC创作者。它有两个维护分支:relea

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 8033

从地方政府网站抓取政务公开数据:Python爬虫技术详解

在本文中,我们通过具体实例介绍了如何使用Python编写爬虫,从地方政府门户网站抓取政务公开数据。我们使用了requestsSelenium等最新技术,结合SQLite数据库存储抓取的数据,最后使用matplotlib进行数据可视化分析。通过本项目,您可以掌握如何抓取政府网站的公开数据,并将其进行清洗、存储和分析。这些技术对于进行政府政策分析、公共服务分析等方面具有重要的实际意义。希望通过本文的介绍,您能够更好地理解政务数据抓取的流程和方法,并能在实际工作中运用这些技术进行数据采集和分析。

2201_76125261的博客 1701

SpringData的CrudRepository接口

 SpringData的所有接口 CrudRepository接口 ,其中提供了这些方法提供使用:需要注意的是在执行保存,删除,更新操作时需要将方法放到service中加上事务 CrudRepository 接口提供了最基本的对实体类的添删改查操作 T save(T entity);//保存单个实体 Iterable&lt;T&gt; save(Iterable&lt;? ext...

qq_36722039的博客 437

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

SpringData 学习(6)—— 学习 Repository 接口的第 1 个子接口 CRUDRepository

CRUDRepository 的使用很简单,原来我们继承 Repository ,现在我们继承 CRUDRepository ,就可以使用 CRUDRepository 固定的一些方法,我们不用写 SQL 语句。public interface BookRepository extends CrudRepository<Book, Integer> {}说明:里面可以不用写任何方法,因为 CrudR

李威威的博客 8280

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

SpringData系列三:Repository接口

 Repository 接口是 Spring Data 的一个核心接口,是一个空接口,即是一个标记接口。它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法。 public interface Repository<T, ID extends Serializable> { }  若我们定义的接口继承了 Repository, 则该接口会被 IOC 容器识别为一个 Repo...

苍穹尘的博客 556

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

什么是SpringData

Spring Data 是Spring 的一个子项目, 旨在统一和简化对各类型持久化存储, 而不拘泥于是关系型数据库还是NoSQL 数据存储。无论是哪种持久化存储, 数据访问对象(或称作为DAO,即Data Access Objects)通常都会提供对单一域对象的CRUD (创建、读取、更新、删除)操作、查询方法、排序和分页方法等。Spring Data则提供了基于这些层面的统一接口(CrudRepository,PagingAndSortingRepository)以及对持久化存储的实现。 一: Spr

bobozai86的博客 8909

SpringData JPA -JPA接口体系

一)JPA接口的继承结构 1.1)SpringData 加入的主要是两个包 1.SpringData-Commons.jar 2.SpringData-jpa.jar 1.2)JPA接口分布在这两个包下每个接口主要的作用和方法介绍 1.JapResposttory:接口 该接口是开放是常用的接口,此接口为封装了对数据库常用的Curd操作。 此接口存放于SpringData-...

失忆๑的博客 417

SpringData CrudRepository接口(六)

通过前面的学习,我们知道Repository接口是一个空接口,即一个标记型接口。实际上Repository有很多子接口。现在我们来学习第一个子接口CrudRepository接口。点进CrudRepository,看这些方法名我们就能够知道他是干啥的~我们现在通过sava方法进行测试保存数据因为我们要做的事情实际上是一个保存操作,所以说这个方法呢需要添加到service层。测试类,进行批量添加~其...

qq_41686190的博客 640

Spring Boot中CrudRepository与JpaRepository

原文链接:https://blog.youkuaiyun.com/xuemengrui12/article/details/80525227 自学Spring Boot的时候看到书上在介绍Spring Data JPA的时候,用的是JpaRepository接口,但看很多的博客用的是CrudRepository,为了搞明白两者的关系,决定好好研究下,先看下两者的接口代码: @NoRepositoryBean ...

weixin_45335305的博客 372

Spring Data的CrudRepository使用及其遇到的问题和解决方法

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34356555的博客 5193

【SpringData&JPA从入门到精通】03-SpringData-Repository 子接口

笔记来源:尚硅谷SpringData教程(springdata经典,spring data快速上手) 文章目录Repository 子接口1、Repository 子接口2、CrudRepository 接口3、PagingAndSortingRepository 接口4、JpaRepository 接口5、JpaSpecificationExecutor 接口6、自定义 Repository 方法总结 Repository 子接口 1、Repository 子接口 在之前 01-Repository .

VectorX's Blog 691

SpringData入门案列

springData基础 目录 1.创建项目,导入依赖:pom.xml 2.配置Spring核心配置文件:ApplicationContext.xml 3.创建实体: 4.Repository接口 5.Repository接口中方法定义约定 6.@Query注解自定义SQL 7.CrudRepository接口 8.PagingAndSortingRepository接口 1....

m0_37657725的博客 453
上一篇: springData学习(二)@Query注解详解
下一篇: springData学习(四)PagingAndSortingRepository接口学习
若往夕之
博客等级 码龄9年 20粉丝 82原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值