随笔 - 836  文章 - 1 评论 - 40 阅读 - 102万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

cankao : https://stackoverflow.com/questions/45973070/spring-jpa-examplematcher-compare-date-condition

场景: 动态条件查询, (主要解决时间问题)

repository

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

 

serviceImp:

复制代码
public Specification<TranxLog> getSpecFromDatesAndExample(
  LocalDateTime from, LocalDateTime to, Example<TranxLog> example) {

    return (Specification<TranxLog>) (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()]));
    }
};
复制代码

..

复制代码
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);
}
复制代码

 

方式2:

 

复制代码
    select
        exchangera0_.currency as currency3_1_,
        exchangera0_.effectiveFromLoc as effectiv4_1_,
        exchangera0_.effectiveToLoc as effectiv5_1_,
        exchangera0_.exchangeRate as exchange6_1_,
        exchangera0_.localCountry as localcou7_1_,
        exchangera0_.localCurrency as localcur8_1_,
        exchangera0_.rateType as ratetype9_1_ 
    from
        ExchangeRate exchangera0_ 
    where
        exchangera0_.rateType=? 
        and exchangera0_.effectiveFromLoc<=? 
        and (
            exchangera0_.currency in (
                ? , ?
            )
        ) 
        and (
            exchangera0_.effectiveToLoc>=? 
            or exchangera0_.effectiveToLoc is null
        )
复制代码

 

 

 

实现:

 

复制代码
    private List<ExchangeRatePO> queryRateDataByDate(String effectiveDate, List<String> currency) {
        
        Specification<ExchangeRatePO> specification = (Specification<ExchangeRatePO>) (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            predicates.add(criteriaBuilder.equal(root.get("rateType"), "Sea"));
            predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("effectiveFromLoc"),effectiveDate));
//            predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("effectiveToLoc"),effectiveDate));
            //process for effectiveToLoc
            List<Predicate> orPredicatesForEffectiveToLoc = new ArrayList<>(); //or 条件的 一定要放在一个集合中
            orPredicatesForEffectiveToLoc.add(criteriaBuilder.greaterThanOrEqualTo(root.get("effectiveToLoc"),effectiveDate));
            orPredicatesForEffectiveToLoc.add(criteriaBuilder.equal(root.get("effectiveToLoc"), criteriaBuilder.nullLiteral(String.class)));
            
            Path<Object> path = root.get("currency");
            CriteriaBuilder.In<Object> in = criteriaBuilder.in(path);
            in.value(currency);
            predicates.add(in); 
            predicates.add(criteriaBuilder.or(orPredicatesForEffectiveToLoc.toArray(new Predicate[orPredicatesForEffectiveToLoc.size()])));
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        return exchangeRateRepository.findAll(specification);
复制代码

 

posted on   lshan  阅读(915)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2019-05-27 JAVA8之妙用Optional解决判断Null为空的问题
点击右上角即可分享
微信分享提示