我最近一直在用方法名创建查询。我有扩展MongoRepository的此接口:
public interface CompanyRepository extends MongoRepository<Company, String> {
//this works.
Company findByEmployeename(String name);
//this doesn't
List<Company> findByEmployeename(List<String> name);
}
我希望可以从员工列表中找回公司列表,但是当我调用该方法时,我会得到一个空列表。我不想在循环或某种形式中使用
findByEmployeename(String name)
。使用自定义存储库方法是否可能? 最佳答案
是。只需将In
附加到List<Company> findByEmployeenameIn(List<String> name);
,即可在Mongo查询中转换为$in
运算符。您应该只阅读https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repositories.query-methods。
如果您想在控制台日志中看到由您的方法创建的相应查询。在属性/ yml文件中,添加logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG