简体   繁体   English

如何阻止 Spring JPA 将参数转义到查询方法?

[英]How can I stop Spring JPA from escaping the arguments to a query method?

In my Spring JPA application, I have a query method called findAllByNameContaining that works for simple, one word arguments.在我的 Spring JPA 应用程序中,我有一个名为 findAllByNameContaining 的查询方法,它适用于简单的单字参数。

For instance, with a record in the database having a field value of 'Foo bar zed', the query method will return the record if called with 'Foo'.例如,数据库中的记录的字段值为“Foo bar zed”,如果使用“Foo”调用,则查询方法将返回该记录。

However, I'd like the record to be retured if the query method is called with 'Foo zed'.但是,如果使用“Foo zed”调用查询方法,我希望返回记录。 I tried to replace spaces in the search term with '%'.我试图用“%”替换搜索词中的空格。 This returned nothing and when I checked the generated SQL, I see that the '%' that I added to the search term was escaped.这没有返回任何内容,当我检查生成的 SQL 时,我看到我添加到搜索词的 '%' 被转义了。 I understand this is likely to be protection against SQL injection, but I was wondering if there's anyway to stop Sopring escaping.我知道这可能是为了防止 SQL 注入,但我想知道是否有办法阻止 Sopring 逃逸。 I can validate the arguments myself to protect against SQL injection.我可以自己验证参数以防止 SQL 注入。

Use findByNameLike instead and put the wildcards in the argument where ever you want.改用findByNameLike并将通配符放在参数中您想要的任何位置。

Note that you have to provide all wildcards in the argument, Spring Data won't add any on its own but it won't escape any either.请注意,您必须在参数中提供所有通配符,Spring Data 不会自行添加任何通配符,但也不会转义任何通配符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用带有“从方法名创建查询”策略的Spring数据JPA来实现这两个简单查询? - How can I implement these 2 simple queries using Spring data JPA with the “query creation from method names” strategy? 如何将这3个JOIN查询转换为Spring Data JPA命名查询方法? - How can I convert this 3 JOIN query into a Spring Data JPA named query method? 当我可以在 Spring JPA 中编写方法时为什么需要 @Query - Why need @Query when I can write a method in Spring JPA 如何设置Spring Data JPA查询方法必须始终设置为true的参数值? - How can I set that a Spring Data JPA query method have to be a parameter value always set as true? 如何通过基于两个属性检索特定 object 的方法名称来实现此 Spring 数据 JPA 查询? - How can I implement this Spring Data JPA query by method name that retrieve a specific object based on two properties? 如何从Spring JPA中的方法名称关闭查询创建? - How to turn off Query creation from method names in Spring JPA? Spring Data JPA-不带参数的查询 - Spring Data JPA - Query with no arguments 在 Spring 数据 Jpa 中,如何从两个不同的数据源运行 SQL 联合查询...? - In Spring Data Jpa, how can I run an SQL union query from two different datasources...? 如何在 Spring Data jpa 中应用本机连接查询? - How can i apply native join query in spring data jpa? 如何使用 Spring 引导在 JPA 存储库中编写 SQL 查询? - How can I write SQL query in JPA repository with Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM