这个春季数据查询不起作用 [英] This spring data query is not working
问题描述
这是我的UserRepository和一个查找函数
Here is my UserRepository and a find function
@Repository
public interface UserRepository extends CrudRepository<JPA_Users, Long> {
List<JPA_Users> findByName(String Name);
}
然后在控制器功能之一中尝试访问此查找函数如下
then in one of the controller function i try to access this find function as follows
@RequestMapping(value = "/jpadata1", method = RequestMethod.GET)
public String jpadata1(ModelMap map) {
UserRepository repository;
ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml");
repository = context.getBean(UserRepository.class);
Iterable usrs = repository.findByName("shahzad");
}
在数据库中有一个名为名称的列,我有多个带有值shahzad的记录我期望所有这些记录都将被返回,但是此函数不返回任何记录,不会返回任何记录
there is a column with named name in database and i have multiple records with values shahzad i was expecting that all those records will be return but this function returns nothing no records are return
Shahzad
推荐答案
ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml");
repository = context.getBean(UserRepository.class);
您正在为每个请求创建一个新的上下文,这是一个错误的策略。
You are creating a new context with each request, which is a really wrong strategy.
作为第一个更正,不要在Controller中创建新的Context,应使用依赖项注入。
As a first correction, don't create a new Context in your Controller, you should use dependency injection.
@Autorwired
private UserRepository userRepository;
@RequestMapping(value = "/jpadata1", method = RequestMethod.GET)
public String jpadata1(ModelMap map) {
Iterable usrs = userRepository.findByName("shahzad");
}
这篇关于这个春季数据查询不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!