JPA中JpaRepository接口的使用方式
作者:qq_36722039 时间:2022-05-02 05:41:12
JPA JpaRepository接口的使用
SpringData的所有接口
CrudRepository接口 ,其中提供了这些方法提供使用,同时继承了其父接口的方法
其中saveAndFlush()方法就相当于hibernate中的saveOrUpdate()和JPA中的merge()
@Test
public void JpaRepository() {
Person person = new Person();
person.setId(27);
person.setName("ab");
person.setAge(22);
person.setEmail("ab@qq.com");
//该方法就相当于hibernate中的saveOrUpdate()和JPA中的merge()
personRepositoiry.saveAndFlush(person);
}
该接口提供了JPA的相关功能
List<T> findAll(); //查找所有实体
List<T> findAll(Sort sort); //排序、查找所有实体
List<T> save(Iterable<? extends T> entities);//保存集合 void flush();//执行缓存与数据库同步
T saveAndFlush(T entity);//强制执行持久化
void deleteInBatch(Iterable<T> entities);//删除一个实体集合
Jpa Repository继承自定义接口的主意事项
今天翻看别人写的项目,看到大神在Repository的接口里又继承了一个自定义接口,觉得这样写挺好,后面注入还是用原来Repository,然后我就把当下自己手里项目也改了改,结果启动报错,错误信息大致如下。
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'TestService': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'TestRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query method public abstract void net.csdn.repository.Test.batchSave(java.util.List)! No property batchSave found for type TestEntity!
整体代码如下
1.自定义接口
public interface Test{
public void batchSave(List<TestEntity> list);
}
2.创建一个接口,该接口 extends JpaRepository 或者 CurdRepository, 以及上面自己定义的接口 Test
public interface TestRepository extends CrudRepository<TestEntity, Integer>, Test{
}
3.实现TestCustom自定义接口,类名我自然而然写成TestService此类的
public class TestService implements Test{
public void batchSave(List<TestEntity> list){
//.....
}
}
解决方案
自定义接口的实现类名是有要求的,必须以 2 中创建的接口的名字TestRepository加上 Impl 来声明,例如
public class TestRepositoryImpl implements Test{
public void batchSave(List<TestEntity> list){
//.....
}
}
来源:https://blog.csdn.net/qq_36722039/article/details/81147219
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#自定义处理xml数据类实例
C#线程 BeginInvoke和EndInvoke使用方法
![](https://img.aspxhome.com/file/2023/6/104336_0s.jpg)
Spring-boot JMS 发送消息慢的解决方法
SpringBoot整合RocketMQ的详细过程
![](https://img.aspxhome.com/file/2023/6/60256_0s.jpg)
Spring的事务管理你了解吗
![](https://img.aspxhome.com/file/2023/4/88204_0s.png)
对dbunit进行mybatis DAO层Excel单元测试(必看篇)
![](https://img.aspxhome.com/file/2023/3/110853_0s.png)
java微信公众号开发(搭建本地测试环境)
![](https://img.aspxhome.com/file/2023/6/63076_0s.png)
IDEA基于支付宝小程序搭建springboot项目的详细步骤
![](https://img.aspxhome.com/file/2023/3/69933_0s.png)
Android SearchView搜索控件使用方法详解
![](https://img.aspxhome.com/file/2023/7/103927_0s.gif)
maven中配置项目的jdk版本无效的排查方式
![](https://img.aspxhome.com/file/2023/9/57509_0s.png)
C++实现LeetCode(131.拆分回文串)
java封装前端查询条件通用版
使用OpenGL绘制Bezier曲线
![](https://img.aspxhome.com/file/2023/4/112284_0s.jpg)
springboot整合Shiro
![](https://img.aspxhome.com/file/2023/0/71140_0s.jpg)
Android实现的仿淘宝购物车demo示例
![](https://img.aspxhome.com/file/2023/9/110689_0s.png)
java使用Jsoup组件生成word文档
基于Spring整合mybatis注解扫描是否成功的问题
![](https://img.aspxhome.com/file/2023/9/71149_0s.png)
java数字转汉字工具类详解
C#动态执行批处理命令的方法
图解Java经典算法冒泡排序的原理与实现
![](https://img.aspxhome.com/file/2023/0/71020_0s.gif)