![](/img/trans.png)
[英]Cannot invoke "com.smart.repository.UserRepository.getUserByUsername(String)" because "this.userRepository" is null
[英]spring boot Cannot invoke Repository because Repository is null
public class UserList {
private String id;
private String email;
private String userType;
private String rolls;
private String partner;
private Integer customersLinked;
private String position;
private String status;
@Autowired
ICustomerRepository customerRepository;
public UserList (Users user){
this.id = user.getId();
this.email = user.getEmail();
this.userType = user.getUserType();
this.rolls = user.getRolls();
this.partner = user.getPartner();
List<Customer> customersLinked = customerRepository.findAllByLinkedUsersIn(user.getId());
this.customersLinked = 0;
this.position = user.getPosition();
this.status =user.getStatus();
}
//Getter and Setter
}
此类在前端用作列表,获取特定数据,而不是发送所有数据
@RequestMapping(value = "usersLinked/{id}/{type}", method = RequestMethod.GET)
public Object getUsersLinkedById(@PathVariable("id") String id,@PathVariable("type") Integer type) {
List<String> users = null;
switch (type) {
case 0:
users = usersRepository.findAll().stream().map(m -> m.getId()).collect(Collectors.toList());
break;
}
//Add userList
List<UserList> userList = new ArrayList<>();
if(users != null)
{
users.forEach(userId ->
{
Optional<Users> user = this.usersRepository.findById(userId);
userList.add(new UserList(user.get()));
});
}
return userList;
}
}
正如您从上面看到的,我正在调用用户存储库中的所有数据并将其发送到列表中
我的客户资料库
public interface ICustomerRepository extends MongoRepository<Customer, String> {
Customer findByBusinessInformation_businessName(String businessName);
List<Customer> findByBusinessInformation_partnerAssigned(String partnerAssigned);
@Query("{ 'LinkedUsers' : ?0 }")
Customer findByLinkedUsers(String id);
List<Customer> findAllByLinkedUsersIn(String id);
}
在 userList 中,当我添加带有 customerRepository 的逻辑时出现错误,没有存储库,一切正常(想要使用存储库获取客户数组,然后获取数组的 size() 并将其添加到linkedCustomers )。 我是不是错过了什么
您可能缺少存储库类顶部的@repository注释。
另一个不相关的忠告:
在您的控制器中,您在 java 中使用 findAll 和 filter 以仅保留 ID。 然后您转到同一个存储库并从上面对每个用户 ID 执行另一个查询。 这会导致您创建多个数据库调用,这是您可以执行的最昂贵的操作之一,当您已经拥有来自第一个单个查询的所有数据时...
此外,如果您只查看函数的底部,则不需要每个用户 ID 的查询(当您有用户 ID 列表作为输入时),您可以创建一个使用 'in ' 约定并传递用户 ID 列表以创建单个 db 调用。
首先我会摆脱@Autowired ICustomerRepository customerRepository;
在UserList
类中。 它不属于那里。 链接客户的计数应在ICustomerRepository
执行,并将结果通过构造函数传递到UserList
。
例如
public class UserList {
private String id;
private String email;
private String userType;
private String rolls;
private String partner;
private Long customersLinked; //better use Long instead of Integer
private String position;
private String status;
// constructor takes the number of linked customers as parameter
public UserList (Users user, Long customersLinked ) {
this.id = user.getId();
this.email = user.getEmail();
this.userType = user.getUserType();
this.rolls = user.getRolls();
this.partner = user.getPartner();
this.customersLinked = customersLinked;
this.position = user.getPosition();
this.status =user.getStatus();
}
//Getter and Setter
}
然后在ICustomerRepository
创建计数查询
例如
public interface ICustomerRepository extends MongoRepository<Customer, String> {
//other methods
Long countByLinkedUsersIn(String id); //not so sure if this query works in mongo
}
最后在你的控制器中
Optional<Users> user = this.usersRepository.findById(userId);
Long count = this.usersRepository.countByLinkedUsersIn(userId);
userList.add(new UserList(user.get(), count));
PS我对查询方法有疑问: Long countByLinkedUsersIn(String id);
. 通常,当存储库方法的名称中包含“In”时, countByLinkedUsers In ,则应将其作为参数列表而不是单个用户 ID。 但是,如果您以前的方法List<Customer> findAllByLinkedUsersIn(String id);
为你工作,那么这个也应该工作。
您正在尝试使用 Autowired 注释注入字段 customerRepository,但您的类不可注入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.