JPA中JpaRepository接口的使用方式

作者:qq_36722039 时间:2022-05-02 05:41:12 

JPA JpaRepository接口的使用

SpringData的所有接口

JPA中JpaRepository接口的使用方式

CrudRepository接口 ,其中提供了这些方法提供使用,同时继承了其父接口的方法

JPA中JpaRepository接口的使用方式

其中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

标签:JPA,JpaRepository,接口
0
投稿

猜你喜欢

  • C#自定义处理xml数据类实例

    2022-07-02 11:00:39
  • C#线程 BeginInvoke和EndInvoke使用方法

    2023-08-31 05:47:16
  • Spring-boot JMS 发送消息慢的解决方法

    2023-02-06 07:50:54
  • SpringBoot整合RocketMQ的详细过程

    2023-07-10 05:07:00
  • Spring的事务管理你了解吗

    2023-02-05 19:44:16
  • 对dbunit进行mybatis DAO层Excel单元测试(必看篇)

    2023-08-19 02:37:19
  • java微信公众号开发(搭建本地测试环境)

    2021-07-07 05:32:40
  • IDEA基于支付宝小程序搭建springboot项目的详细步骤

    2021-10-30 22:44:46
  • Android SearchView搜索控件使用方法详解

    2022-07-09 16:49:21
  • maven中配置项目的jdk版本无效的排查方式

    2023-07-18 21:43:42
  • C++实现LeetCode(131.拆分回文串)

    2023-07-24 09:58:42
  • java封装前端查询条件通用版

    2023-06-24 12:06:15
  • 使用OpenGL绘制Bezier曲线

    2023-03-14 19:55:39
  • springboot整合Shiro

    2022-11-13 11:07:52
  • Android实现的仿淘宝购物车demo示例

    2023-09-04 08:59:19
  • java使用Jsoup组件生成word文档

    2022-10-28 12:44:16
  • 基于Spring整合mybatis注解扫描是否成功的问题

    2023-01-23 18:29:22
  • java数字转汉字工具类详解

    2023-04-28 02:00:26
  • C#动态执行批处理命令的方法

    2023-03-16 23:19:40
  • 图解Java经典算法冒泡排序的原理与实现

    2023-03-14 21:41:23
  • asp之家 软件编程 m.aspxhome.com