Spring Data Jpa原生查询REGEXP

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Spring Data Jpa原生查询REGEXP

奔走的箱子   2022-05-23 我要评论

Spring Data Jpa原生查询 REGEXP 的使用

REGEXP 与like 有通用之处, 单 regexp 有更好的精确度,更加自由灵活

在jpa 中使用时 :其中 定位符 ^ 在jpa @query 注解中使用时需要加上引用号 e.g

@Query(value = "select p.id as id  from zt_products AS p 
       where 
        p.enabled=true 
       and 
        p.name regexp  '^'+?1+'$' ",
         nativeQuery = true)
    List<Map> findByName(String names);

如此 方可使用,,但是% 可以不用加。。。。

嗯 暂时就这个样子///

后记:

在实际使用时 发现上述方法存在缺陷; 无论names传的什么值 查询结果一样;

@Query(value = "select p.id as id  from zt_products AS p 
       where 
        p.enabled=true 
       and 
        p.name regexp  ?1,
         nativeQuery = true)
    List<Map> findByName(String rexgexp);//rexgexp 为 regexp 表达式 直接在这里和一起会不起作用

可参考:

https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.stored-procedures

spring data jpa 原生查询(查一个json中的某一字段)

数据库:

jpa 查询 java:

@Component("ediHistoryDAO")
public interface EdiHistoryDAO extends CrudRepository<EdiHistoryDO, Integer>{
    EdiHistoryDO findById(BigInteger id);
    EdiHistoryDO findByEdiInfoId(BigInteger id);
    List<EdiHistoryDO> findByIdIn(List<BigInteger> id);
    @Query(value="select * from EDI.edi_history  where json_contains(response_summary, ?1) and json_contains(response_summary, ?2)  and json_contains(response_summary, ?3)  and json_contains(response_summary, ?4)", nativeQuery = true)
    public List<EdiHistoryDO> findByResponseSummary(String result,String orderNo,String orderId,String docType);
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们