Spring Data JPA自动存储库返回空

eoigrqb6  于 2022-10-15  发布在  Spring
关注(0)|答案(1)|浏览(174)

我正在使用以下存储库和控制器从数据库中检索一些记录:

public interface TableARepository extends CrudRepository<TableA, Long>{
    @Query(value = "SELECT r FROM TableA r  WHERE r.Id = :Id", nativeQuery = true)
    public Rater getTableAById(@Param("Id") Long Id);
}

@RestController
public class TableAController {
    @Autowired
    TableARepository tableARepository;

    @GetMapping(value="/GetTableAById")
    public TableA getTableAById(@RequestParam String email) {
        TableA tableA = new TableA();
        tableA = tableARepository.getTableAById(email);
        return tableA;
    }
}

一旦我调用REST API,就会出现以下错误:

Cannot invoke com.TableARepository.getTableAById(Long) because this.tableARepository is null
nukf8bse

nukf8bse1#

添加注解@Repository

@Repository
public interface TableARepository extends CrudRepository<TableA, Long>{
    @Query(value = "SELECT r FROM TableA r  WHERE r.Id = :Id", nativeQuery = true)
    public Rater getTableAById(@Param("Id") Long Id);
}

相关问题