我是 Spring Data JDBC 的初學者。我正在關注 Spring in action 第 6 版,并創建了如下存盤庫:
package com.springinaction.tacocloud;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;
public interface IngredientRepository extends CrudRepository<Ingredient, String>{
public Optional<Ingredient> getById(String id);
public Iterable<Ingredient> getIngredients();
public Ingredient save(Ingredient ingredient);
}
我已經在我的控制器中注入了這個存盤庫。
@Slf4j
@Controller
@RequestMapping("/design")
public class DesignTacoController {
protected IngredientRepository ingredientRepo;
public DesignTacoController(IngredientRepository ingredientRepo) {
this.ingredientRepo = ingredientRepo;
}
@ModelAttribute
public void addIngredientsToModel(Model model) {
Iterable<Ingredient> ingredientsFromJdbc = ingredientRepo.getIngredients();
//converting Iterable returned by JdbcTemplate to a List
List<Ingredient> ingredients = new ArrayList<Ingredient>();
ingredientsFromJdbc.forEach(ingredients::add);
Type[] types = Ingredient.Type.values();
for (Type type : types) {
model.addAttribute(type.toString().toLowerCase(),filterByType(ingredients, type));
}
}
.......................................................................
為了在控制器中呼叫 getIngredients() 方法,它會引發以下例外
nested exception is org.springframework.data.mapping.PropertyReferenceException: No property 'getIngredients' found for type 'Ingredient'!
我在這里想念什么?
uj5u.com熱心網友回復:
只需洗掉該方法getIngredients()
并使用findAll()
(由超級介面提供)來代替。
您還應該洗掉其他方法,因為它們已經由超級介面提供
save()
<- 提供者CrudRepository
getById()
<-CrudRepository
提供Optional<T> findById(ID id);
getIngredients
<-CrudRepository
提供Iterable<T> findAll();
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/470354.html