如何在 Spring Data Repository Method 中使用 Regex 关键字 [英] How to use Regex keyword in Spring Data Repository Method

查看:22
本文介绍了如何在 Spring Data Repository Method 中使用 Regex 关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的是 spring-data-jpa 版本 1.9.4.

I am currently using spring-data-jpa version 1.9.4.

我有一个 MySql 表,其中包含 project(integer)、summary(varchar) 和 description(varchar) 列.

I have a MySql table with columns project(integer), summary(varchar), and description(varchar).

我有一个正则表达式,我想用它来搜索摘要和/或描述字段,这意味着如果在摘要中找到它,则不需要将正则表达式应用于描述.

I have a regex that I would like to use to search the summary and/or description field meaning that if it finds it in summary does not need to apply regex to description.

我尝试使用的存储库方法是:

The repository method I am attempting to use is:

List<Issue> findByProjectAndSummaryOrDescriptionRegex(long project, String regex)

我收到的错误是:

java.lang.IllegalArgumentException: 不支持的关键字正则表达式 (1):[匹配正则表达式,匹配,正则表达式]

java.lang.IllegalArgumentException: Unsupported keyword REGEX (1): [MatchesRegex, Matches, Regex]

在我的公司环境中更新/升级版本很困难,所以如果问题不是我的语法而是如果有人知道哪个版本现在支持正则表达式"进行查询派生或者我在哪里可以找到特定信息我将感激不尽.我查看了 Changelog 并且看起来 1.9.4 应该支持但它似乎不支持.

It is difficult in my company environment to update/upgrade versions, so if the issue is NOT my syntax but rather the then if someone knows which version now supports 'Regex' for query derivation or where I could find that specific information I would be grateful. I have looked at the Changelog and it appears that 1.9.4 should support but it appears not.

感谢您的帮助!

京东

编辑 1:我知道 @Query 注释,但我的领导要求仅在我找不到支持关键字 REGEX [MatchesRegex,匹配,正则表达式]

EDIT 1: I am aware of the @Query annotation but have been asked by my lead to only use that as a last resort if I cannot find the correct version which supports keyword REGEX [MatchesRegex, Matches, Regex]

推荐答案

如果 Spring 数据语法不起作用,我建议使用原生查询(带有 @Query 注释),例如:

I would recommend using native query (with @Query annotation) if the Spring data syntax does not work, e.g.:

@Query(nativeQuery=true, value="SELECT * FROM table WHERE project = ?1 AND (summary regexp ?2 OR description regexp ?2)")
List<Issue> findByProjectAndSummaryOrDescription(long project, String regex);

更新

如果本机查询不是一个选项,那么 (a) 你可以用单列试试看是否有效,(b) 你可以通过将 regex 附加到两列来尝试,例如:

If native query is not an option then (a) could you try it with single column and see if that works and (b) could you try by appending regex to both the columns, e.g.:

List<Issue> findByProjectAndDescriptionRegex(long project, String regex);

List<Issue> findByProjectAndSummaryRegexOrDescriptionRegex(long project, String regex, String regex);

这篇关于如何在 Spring Data Repository Method 中使用 Regex 关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆