JPA中JpaRepository接口的使用方式
JPA JpaRepository接口的使用
SpringData的所有接口

CrudRepository接口 ,其中提供了這些方法提供使用,同時(shí)繼承了其父接口的方法

其中saveAndFlush()方法就相當(dāng)于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");
//該方法就相當(dāng)于hibernate中的saveOrUpdate()和JPA中的merge()
personRepositoiry.saveAndFlush(person);
}
該接口提供了JPA的相關(guān)功能
List<T> findAll(); //查找所有實(shí)體 List<T> findAll(Sort sort); //排序、查找所有實(shí)體 List<T> save(Iterable<? extends T> entities);//保存集合 void flush();//執(zhí)行緩存與數(shù)據(jù)庫同步 T saveAndFlush(T entity);//強(qiáng)制執(zhí)行持久化 void deleteInBatch(Iterable<T> entities);//刪除一個(gè)實(shí)體集合
Jpa Repository繼承自定義接口的主意事項(xiàng)
今天翻看別人寫的項(xiàng)目,看到大神在Repository的接口里又繼承了一個(gè)自定義接口,覺得這樣寫挺好,后面注入還是用原來Repository,然后我就把當(dāng)下自己手里項(xiàng)目也改了改,結(jié)果啟動報(bào)錯(cuò),錯(cuò)誤信息大致如下。
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.創(chuàng)建一個(gè)接口,該接口 extends JpaRepository 或者 CurdRepository, 以及上面自己定義的接口 Test
public interface TestRepository extends CrudRepository<TestEntity, Integer>, Test{
}
3.實(shí)現(xiàn)TestCustom自定義接口,類名我自然而然寫成TestService此類的
public class TestService implements Test{
public void batchSave(List<TestEntity> list){
//.....
}
}
解決方案
自定義接口的實(shí)現(xiàn)類名是有要求的,必須以 2 中創(chuàng)建的接口的名字TestRepository加上 Impl 來聲明,例如
public class TestRepositoryImpl implements Test{
public void batchSave(List<TestEntity> list){
//.....
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot通過自定義注解實(shí)現(xiàn)配置類的自動注入的實(shí)現(xiàn)
本文主要介紹了SpringBoot通過自定義注解實(shí)現(xiàn)配置類的自動注入的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
淺談SpringCloud?Alibaba和SpringCloud的區(qū)別
這篇文章主要介紹了淺談SpringCloud?Alibaba和SpringCloud的區(qū)別,Spring?Cloud?Netflix框架也屬于Spring?Cloud,但是Netflix并不是由spring來進(jìn)行開發(fā)的,需要的朋友可以參考下2023-05-05
java swing實(shí)現(xiàn)電影購票系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java swing實(shí)現(xiàn)電影購票系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Java HttpClient-Restful工具各種請求高度封裝提煉及總結(jié)
這篇文章主要介紹了Java HttpClient-Restful工具各種請求高度封裝提煉及總結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
java用arraycopy實(shí)現(xiàn)多擊事件
這篇文章主要介紹了java用arraycopy實(shí)現(xiàn)多擊事件的多種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Spring?Boot?+?Spring?Batch?實(shí)現(xiàn)批處理任務(wù)的詳細(xì)教程
這篇文章主要介紹了Spring?Boot+Spring?Batch實(shí)現(xiàn)批處理任務(wù),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08

