簡體   English   中英

我在使用spring crud reprsitry時遇到問題

[英]i am facing issues in using spring crud reprositry

上下文初始化期間遇到異常-取消刷新嘗試:org.springframework.beans.factory.UnsatisfiedDependencyException:創建名稱為“ rabbitMqController”的bean時出錯:通過字段“ recordsReprositry”表示的不滿足的依賴關系; 嵌套的異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有類型為'com.rabbitmq.config.RecordsReprositry'的合格bean:期望至少有1個有資格作為自動裝配候選的bean。 依賴項注釋:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

看來您是帶注釋的接口。。應將@repository放入其實現類。

package com.rabbitmq.config;                                                                                  

import java.util.UUID;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;


@Repository
public **interface** RecordsReprositry extends CrudRepository<Records, Long>{

    public Records findById(UUID id);

}

從spring嘗試JPA ...(DOC: http : //docs.spring.io/spring-data/jpa/docs/current/reference/html/

例:

@Repository
public interface MyRepository extends JpaRepository<EntityName,Long> {
    // here you can write your query; example:
    EntityName findByAttribute(Type value);
    // or
    @Query("SELECT * FROM EntityName t WHERE t.ID=?1")
    EntityName findByID(Long id);
}

然后,您可以在服務中使用此存儲庫(必須使用自動裝配)

例:

@Service
public class MyService{
    @Autowired 
    private MyRepository repo;

    // here you can call in a method your query
    public EntityName example() {
        EntityName e = repo.findByID((long)1);
        return e;
    }
}

重要提示:您必須僅在服務中使用存儲庫,而在控制器中必須使用它的服務

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM