使用spring和mongodb根据我的查询获取文档列表

问题描述 投票:0回答:2

我的模型类看起来像这样:

 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);    
    }   

我想传递一个参数:卖家用户ID,并根据Mongodb的卖家UserId获取所有文件。我怎样才能实现这一目标?请帮忙吗?

在ProductDetailsRepository中添加这样的方法

 public interface ProductdetialsRepository extends MongoRepository<ProductionDetailsModel, String>{ 
    List< ProductionDetailsModel> findAllBySellerUserId(final String  sellerUserId); 
    }

在我的控制器中我添加了

@RequestMapping(method=RequestMethod.GET, path = "/{sellerUserId}")
    void getProductDetails(@PathVariable String sellerUserId) {
    System.out.println(sellerUserId);
    productdetialsRepository.findAllBySellerUserId(sellerUserId);
    }

我也做了这个实验

Query query = new Query();
query.addCriteria(Criteria.where("id").is(sellerUserId));
List<ProductionDetailsModel> productionDetailsList =    productdetialsRepository.find(query ProductionDetailsModel.class);

但我得到空指针异常任何人都可以请帮助我吗?

java spring mongodb spring-data-mongodb
2个回答
1
投票

您需要在Repository Interface中编写方法

List< ProductionDetailsModel> findAllBySellerUserId(final String sellerUserId);

并在serviceImpl类中调用此方法

List<ProductionDetailsModel> findAllBySellerUserId(final String sellerUserId){
    return productdetialsRepository.findBySellerUserId(sellerUserId); 
}

当你使用mongo和spring时,其他更好的解决方案是使用mongoOperations(由spring框架提供)

Query query = new Query();
query.addCriteria(Criteria.where("id").is(sellerUserId));
List<ProductionDetailsModel> productionDetailsList = mongoOperation.find(your query, ProductionDetailsModel.class);

0
投票

在ProductDetailsRepository中添加这样的方法

List<ProductDetails> findBySellerUserId(String sellerUserId)

Spring数据将根据方法名称制定查询并获得结果。

© www.soinside.com 2019 - 2024. All rights reserved.