[英]Get List Of documents based on my query using spring and mongodb
My Model Class look like this: 我的模型类如下所示:
package io.springBoot.ProductionDetails;
import org.springframework.data.annotation.Id;
public class ProductionDetailsModel {
//Created an entity for ProductDetails
@Id
private String productId;
private String name;
private String description;
private String image; //Alway's in Binary data
private String sellerUserId;
public ProductionDetailsModel() {
}
//Constructors
public ProductionDetailsModel( String productId, String name, String description, String image, String sellerUserId) {
super();
this.productId = productId;
this.name = name;
this.description = description;
this.image = image;
this.sellerUserId = sellerUserId;
}
//Getter and Setter Methods for objects
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getSellerUserId() {
return sellerUserId;
}
public void setSellarUserId(String sellerUserId) {
this.sellerUserId = sellerUserId;
}
I am able to get single document based on this query:
public void getProductDetails(String Id) {
return productdetialsRepository.findOne(id);
}
I want to pass a perameter like: sellerUserId and get all the documents based on sellerUserId form the Mongodb. 我想通过一个像perameter一样的内容:SellerUserId并从Mongodb中获取所有基于SellerUserId的文档。 How can i achieve this? 我怎样才能做到这一点? Any Help please? 有什么帮助吗?
Add a method like this in ProductDetailsRepository 在ProductDetailsRepository中添加这样的方法
public interface ProductdetialsRepository extends MongoRepository<ProductionDetailsModel, String>{
List< ProductionDetailsModel> findAllBySellerUserId(final String sellerUserId);
}
In my Controller I added 在我的控制器中,我添加了
@RequestMapping(method=RequestMethod.GET, path = "/{sellerUserId}")
void getProductDetails(@PathVariable String sellerUserId) {
System.out.println(sellerUserId);
productdetialsRepository.findAllBySellerUserId(sellerUserId);
}
and also i done experiment with this 我也做了这个实验
Query query = new Query();
query.addCriteria(Criteria.where("id").is(sellerUserId));
List<ProductionDetailsModel> productionDetailsList = productdetialsRepository.find(query ProductionDetailsModel.class);
But i am getting null pointer Exception any one can please help me? 但是我得到空指针异常有人可以帮助我吗?
You need to write method in your Repository Interface 您需要在存储库接口中编写方法
List< ProductionDetailsModel> findAllBySellerUserId(final String sellerUserId);
and call this method in your serviceImpl class 并在您的serviceImpl类中调用此方法
List<ProductionDetailsModel> findAllBySellerUserId(final String sellerUserId){
return productdetialsRepository.findBySellerUserId(sellerUserId);
}
And other better solution as you are using mongo with spring is to use mongoOperations(which is provided by spring framework) 当您在Spring中使用mongo时,其他更好的解决方案是使用mongoOperations(由spring框架提供)
Query query = new Query();
query.addCriteria(Criteria.where("id").is(sellerUserId));
List<ProductionDetailsModel> productionDetailsList = mongoOperation.find(your query, ProductionDetailsModel.class);
Add a method like this in ProductDetailsRepository 在ProductDetailsRepository中添加这样的方法
List<ProductDetails> findBySellerUserId(String sellerUserId)
Spring data will formulate the query based on method name and get the result. Spring数据将基于方法名称制定查询并获得结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.