JPA Query By Example (QBE) 不支持日期查询

JPA Query By Example (QBE) 不支持日期查询

一. 先说结论

Query By Example(下文统称QBE)

只支持字符串类型的Example查询, 如果要加另外的例如日期类型的查询, 需要通过构建额外的Specification来实现

1.1 在原有的repo上继承JpaSpecificationExecutor

public interface TranxlogRepository extends JpaRepository<Tranxlog, Long>, JpaSpecificationExecutor<Tranxlog>{ 
}

1.2 构建Specification

public Specification<TranxLog> getSpecFromDatesAndExample(
  LocalDateTime from, LocalDateTime to, Example<TranxLog> example) {
    return (root, query, builder) -> {
         final List<Predicate> predicates = new ArrayList<>();
         if (from != null) {
            predicates.add(builder.greaterThan(root.get("dateField"), from));
         }
         if (to != null) {
            predicates.add(builder.lessThan(root.get("dateField"), to));
         }
         predicates.add(QueryByExamplePredicateBuilder.getPredicate(root, builder, example));
         return builder.and(predicates.toArray(new Predicate[predicates.size()]));
    }
};

1.3 调用repo.findAll方法时传入Specification

public Page<TranxLog> findAllByConditions(TranxReportFormModel formModel, Pageable page) {
    ExampleMatcher matcher = ExampleMatcher.matching()
            .withNullHandler(ExampleMatcher.NullHandler.IGNORE)
            .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
            .withIgnoreCase()
            .withIgnoreNullValues();
    Example<TranxLog> example = Example.of(formModel.getTranxLog(), matcher);
    return tranxlogRepository.findAll(getSpecFromDatesAndExample(from, to, Example.of(formModel.getTranxLog(), matcher)), page);
}

二. 问题描述

QBE是JPA给我们提供的一个通过样例查询的方式, 我们可以通过构建一个 entity “案例”, 然后JPA会通过这个entity存在的字段为我们构建查询.

下图为JpaRepository提供的Example查询方法定义

image-20220711161908089

但是通过查看ExampleMatcher源码发现只有StringMatcher一种实现, 这意味着我们只能对一些字符类型的列进行模糊匹配或者全匹配(也支持REGEX形式的匹配).

如果要对日期类型进行比较则不支持, 需要我们手动构建一个包含Example查询的Specification(详情参照上文, 不赘述)

参考资料

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值