MongoDB 的多个 PredicateSpecification 与 JpaSpecificationExecutor 与 Micronuat 数据的动态组合
Dynamic combination of multiple PredicateSpecification with JpaSpecificationExecutor with Micronuat data for MongoDB
如何使用 MongoDB 中的 JpaSpecificationExecutor 将所有 PredicateSpecification 或 QuerySpecification 附加到 Micronaut 数据?
谓词
public class DiscountSpecification {
final static BeanIntrospection<Discount> introspection = BeanIntrospection.getIntrospection(Discount.class);
static String[] discountProperty = introspection.getPropertyNames();
public static PredicateSpecification<Discount> DiscountIdEquals(ObjectId DiscountId) {
return (root, criteriaBuilder) -> criteriaBuilder.equal(root.get(discountProperty[0]), DiscountId);
}
public static PredicateSpecification<Discount> DiscountCodeEquals(String DiscountCode) {
return (root, criteriaBuilder) -> criteriaBuilder.equal(root.get(discountProperty[1]), DiscountCode);
}
}
现在根据条件我要追加所有谓词
@Introspected
public record QueryBuilderSpecification() {
public static PredicateSpecification<Discount> QueryBuilder(DiscountFilterModel discountFilterModel){
PredicateSpecification<Discount> predicateSpecification = null;
if (discountFilterModel.getId() != null){
predicateSpecification = DiscountIdEquals(new ObjectId(discountFilterModel.getId()));
}
if (discountFilterModel.getDiscountCode() != null){
predicateSpecification = DiscountCodeEquals(discountFilterModel.getDiscountCode());
}
return predicateSpecification;
}
}
上面的代码并没有为这两个条件附加。如果两者都存在,我不确定如何将两者结合起来。
基于代码PredicateSpecification you can and
& or
specifications together. As demonstrated in the Micronaut Data User Guide
应该可以使用DiscountIdEquals(...).and(DiscountCodeEquals(..))
。
你的例子可以用 PredicateSpecification.ALL
作为初始状态,然后有:
predicateSpecification = predicateSpecification.and(DiscountIdEquals(...))
.
如何使用 MongoDB 中的 JpaSpecificationExecutor 将所有 PredicateSpecification 或 QuerySpecification 附加到 Micronaut 数据?
谓词
public class DiscountSpecification {
final static BeanIntrospection<Discount> introspection = BeanIntrospection.getIntrospection(Discount.class);
static String[] discountProperty = introspection.getPropertyNames();
public static PredicateSpecification<Discount> DiscountIdEquals(ObjectId DiscountId) {
return (root, criteriaBuilder) -> criteriaBuilder.equal(root.get(discountProperty[0]), DiscountId);
}
public static PredicateSpecification<Discount> DiscountCodeEquals(String DiscountCode) {
return (root, criteriaBuilder) -> criteriaBuilder.equal(root.get(discountProperty[1]), DiscountCode);
}
}
现在根据条件我要追加所有谓词
@Introspected
public record QueryBuilderSpecification() {
public static PredicateSpecification<Discount> QueryBuilder(DiscountFilterModel discountFilterModel){
PredicateSpecification<Discount> predicateSpecification = null;
if (discountFilterModel.getId() != null){
predicateSpecification = DiscountIdEquals(new ObjectId(discountFilterModel.getId()));
}
if (discountFilterModel.getDiscountCode() != null){
predicateSpecification = DiscountCodeEquals(discountFilterModel.getDiscountCode());
}
return predicateSpecification;
}
}
上面的代码并没有为这两个条件附加。如果两者都存在,我不确定如何将两者结合起来。
基于代码PredicateSpecification you can and
& or
specifications together. As demonstrated in the Micronaut Data User Guide
应该可以使用DiscountIdEquals(...).and(DiscountCodeEquals(..))
。
你的例子可以用 PredicateSpecification.ALL
作为初始状态,然后有:
predicateSpecification = predicateSpecification.and(DiscountIdEquals(...))
.