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){
		//.....
	}
}

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值