【发布时间】:2016-09-20 16:02:23
【问题描述】:
环境:
spring-data-mongo: 1.7.0.RC1
mongo-java-driver:3.2.2
文档:
@Document(collection = "products")
public class Product {
@Id
private String sid;
private String name;
private Long vendor;
(...)
}
存储库:
public interface ProductRepository extends MongoRepository<Product, String> {
Product findByName(String productName);
}
我的目标是拦截对 Product 集合执行的任何查询并添加谓词或规范,而无需修改存储库或实现方法 findByNameAndBelongsToVendorList。
我需要这个拦截器或 aspectJ,因为我有多种方法,例如:
Page<Product> findAll(Pageable page);
List<Product> findByCategory(String category, Pageable pageRequest);
(...)
目标
findByName // perform a filter by name (explicit)
// and a filter by vendor (injected via inteceptor or aspecJ)
避免这样做
@Repository
public class ProductRepositoryCustomImpl implements ProductRepositoryCustom {
@Autowired
private MongoTemplate template;
public Product findByNameAndBelongsToVendorList(String name, List<Long> vendors, Pageable pageRequest) {
Criteria criteriaVendor = Criteria.where("vendors").in(vendors);
Query query = new Query(criteriaVendor);
query.with(pageRequest);
return template.findOne(query, Product.class);
}
}
【问题讨论】:
标签: java spring mongodb spring-data spring-data-mongodb