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 )。我是不是錯過了什么
uj5u.com熱心網友回復:
您可能缺少存盤庫類頂部的@repository注釋。
另一個不相關的忠告:
在您的控制器中,您在 java 中使用 findAll 和 filter 以僅保留 ID。然后您轉到同一個存盤庫并從上面對每個用戶 ID 執行另一個查詢。這會導致您創建多個資料庫呼叫,這是您可以執行的最昂貴的操作之一,當您已經擁有來自第一個單個查詢的所有資料時...
此外,如果您只查看函式的底部,則不需要對每個用戶 ID 進行查詢(當您有用戶 ID 串列作為輸入時),您可以創建一個使用 'in ' 約定并傳遞用戶 ID 串列以創建單個 db 呼叫。
uj5u.com熱心網友回復:
首先,我會@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);對您有效,那么這個方法也應該有效。
uj5u.com熱心網友回復:
您正在嘗試使用 Autowired 注釋注入欄位 customerRepository,但您的類不可注入。
- 您可以在類 UserList 上添加注釋 @Repository
- 或者使用建構式注入(注入 bean 的更好方法)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/314170.html
下一篇:屬性值回傳空
