Spring Data Jpa 中原生查询 REGEXP 的使用详解

2022-07-20,,,,

目录
  • spring data jpa原生查询 regexp 的使用
  • spring data jpa 原生查询(查一个json中的某一字段)

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);
}

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

《Spring Data Jpa 中原生查询 REGEXP 的使用详解.doc》

下载本文的Word格式文档,以方便收藏与打印。