春季启动:如何通过MySQL数据库中的情况敏感参数找到名称
我是名称的简单数据库。只有三个名称: a d a m, a rnold和m a 锡。我想获得所有包含字母“ a” 的名称。 我在我的存储库中有这样的方法: 令弦字母=“ a”;
@Query(value = "SELECT * FROM person WHERE name LIKE %:letter%", nativeQuery = true)
List<PersonEntity> findByNameLike( @Param("letter") String letter);
它返回所有名称(名称列表),因为它在所有名称中都找到了一些“ a”或“ a” 。但是我只想找到包含完全较低案例的名称“ a” 。 此查询在Workbech Wery中运行良好: 从人名中的人命名'%a%'的人中选择 *;
但是此代码返回空列表。
@Query(value = "SELECT * FROM person WHERE name LIKE BINARY '%:letter%'", nativeQuery = true)
List<PersonEntity> findByNameLike( @Param("letter") String letter);
我不知道如何将可变字母链接到查询。谢谢您的任何想法。 PS:在这种情况下,如果变量字母包裹在“%”中,如何对SQLINFOSITY进行保护?
I make simple database of names. There are only three names: Adam , Arnold and Matin. And I want to get all names that contains letter "a".
I have in my repository this method:
Let String letter = "a";
@Query(value = "SELECT * FROM person WHERE name LIKE %:letter%", nativeQuery = true)
List<PersonEntity> findByNameLike( @Param("letter") String letter);
It returns all names(List of names), because it find some "a" or "A" in all of names. But I want to find only names that contains exactly lower case "a".
This query works in the Workbech wery well:SELECT * FROM person WHERE name LIKE BINARY '%a%';
But this code returns empty(null) List.
@Query(value = "SELECT * FROM person WHERE name LIKE BINARY '%:letter%'", nativeQuery = true)
List<PersonEntity> findByNameLike( @Param("letter") String letter);
I dont know how to link the variable letter to the query. Thank you for any ideas.
PS:How to make SQLinjection protection in this case if the variable letter is wrapped in "%" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
您的情况下,您正在寻找包含字符串“:字母”的名称,尝试
使用查询通量,没有机会注入SQL。
通过使用右整理,您可以在表创建中确定问题是否敏感或不敏感,请参见在mysql中敏感的唯一指数案例?
With
you are looking for names that contain the String ':letter', try
And using Query-annotation there is no chance of SQL-injection.
By using the right collation you decide on table creation wether queries are case sensitive or case insensitive, see Are UNIQUE indices case sensitive in MySQL?
将二进制更改为下。它指定了您想要一个小型案例。
作为提示,如果您可以利用弹簧数据,我将不使用“ jpql”,而您不必写下查询:
doc: jpql类似案例不敏感
Change BINARY to lower. It specifies you want a lower-case.
As a tip, I wouldn't use 'jpql', if you can take advantage of spring-data, and you don't have to writte the query:
Doc: JPQL Like Case Insensitive