-1

这是我的 mongo 存储库

@Repository
public interface StockRepository extends MongoRepository<Stock,String> {
    Stock findStockByStockSymbol(String stockSymbol);
}

这是我的Stock

@Document(collection="stocks")
public class Stock {

    @Id
    private String stockSymbol;
    private String StockName;

    private Double basePrice;


    public String getStockSymbol() {
        return stockSymbol;
    }

    public void setStockSymbol(String stockSymbol) {
        this.stockSymbol = stockSymbol;
    }

    public String getStockName() {
        return StockName;
    }

    public void setStockName(String stockName) {
        StockName = stockName;
    }

    public Double getBasePrice() {
        return basePrice;
    }

    public void setBasePrice(Double basePrice) {
        this.basePrice = basePrice;
    }

    @Override
    public String toString() {
        return "Stock{" +
                "stockSymbol='" + stockSymbol + '\'' +
                ", StockName='" + StockName + '\'' +
                ", basePrice=" + basePrice +
                '}';
    }
}

我正在尝试从列表中获取Stock列表stockSymbols

stockRepository.findAllStockByStockSymbol(symbolList);

但它返回一个空列表。我究竟做错了什么?

4

1 回答 1

2

显然 spring-mongo 无法理解我的查询。我通过使用Query注释指定自定义查询来使其工作。

@Repository
public interface StockRepository extends MongoRepository<Stock,String> {
    @Query("{ 'stockSymbol' : { $in : ?0 } }")
    List<Stock> findAllStockByStockSymbol(List<String> stockSymbol);

}
于 2017-05-17T10:12:40.463 回答