2017-08-02 192 views
-2

我怎样才能得到所有对象从CrudRepository春季

@RequestMapping(value="/test", method=RequestMethod.GET) 
public ModelAndView recoverPass(@RequestParam("num") int num) { 
    ModelAndView mv = new ModelAndView("test"); 
    mv.addObject("lists",appRepo.findAll(num)); 
    return mv; 
} 

我的浏览器,但没有结果输入http://localhost:8080/test?num=40数据库。 (int)

回答

1

Spring中的CrudRepository没有一个名为findAll()的方法,它接受一个整数参数。没有参数的findAll()返回所有的对象,而不仅仅是给定的一个特定的整数ID。

你需要弄清楚你想要什么。如果你想要所有的对象,然后调用没有参数的findAll(),并摆脱int参数。

如果您只想找到给定整数id的特定对象,请改为使用findOne(num)。

+0

谢谢,我想找到具有特定字段值的所有对象 –

+0

@RayEn,那么你应该使用'match'查询 – sunkuet02

+0

@RayEn如果你想这样做,那么你将需要使用Spring数据查询方法。假设你的字段被称为带有字符串值的foo,然后向你的appRepo接口添加一个名为findByFoo(String foo)的方法,并且Spring数据会神奇地使它适用于你。 – Plog

0

在Spring中CrudRepository只有findAll()方法巫婆返回从表中的所有项目。 如果你想获得的所有项目,你应该使用

appRepo.findAll(); 

,或者你应该需要的条件申报方法:

@Repository 
public interface AppRepo extends CrudRepository<Foo, Long> { 
    List<Foo> findBySomeValue(String someValue); 
} 

,并用它在你的逻辑appRepo.findBySomeValue('bar');

0

我认为你正在尝试获得id = 40的用户。如果您使用spring引导的spring数据jpa,则可以创建一个存储库,如userRepository,并创建一个方法findById()以获取特定id的用户。

public interface UserRepository extends JpaRepository<User, Serializable>{ 
    User findById(Long id); 
} 

然后可以自动装配在服务UserRepository并调用findById()方法。您也可以使用相同的UserRepository对象通过调用findAll()方法来获取所有用户列表。