@Query不接受LIKE执行细粒度搜索

时间:2019-04-12 05:49:18

标签: java spring jpa search jpql

  1. 运行此命令时,我可以获得结果,但只有当我输入全名时。.

  2. @Query(value = "SELECT * FROM Student WHERE first_name=?1", nativeQuery = true) public List<Student> findByFirstNameLike(String searchName);

  3. 现在我尝试添加类似运算符

  4. @Query(value = "SELECT * FROM Student WHERE first_name like=?1", nativeQuery = true) public List<Student> findByFirstNameLike(String searchName); }

  5. 我通过输入Dan并点击搜索来寻找学生Daniel,这是一个例外

  6. java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '='Dan'' at line 1

  7. 我什至尝试将=更改为这样的%。

  8. WHERE first_name like%?1

  9. 点2可以工作,但是点4和6给我SQLSyntaxErrorException,我该如何解决?谢谢

  10. 我也尝试过使用本链接中提到的@Param注释,但仍然获得语法异常https://stackoverflow.com/a/38115592/4325878

2 个答案:

答案 0 :(得分:3)

此查询是错误的:

"SELECT * FROM Student WHERE first_name like=?1"

您不能在like关键字后放置等号。应该是这样的:

"SELECT * FROM Student WHERE first_name like ?1"

如果放置"%",则它应该是参数的一部分。

也看看这个:

public List<Student> findByFirstNameLike(@Param( String searchName);

括号不平衡。为了将来,请提供编译代码段...

答案 1 :(得分:0)

您应该尝试以下我发布的内容,希望对您有帮助...

@Query(value = "SELECT * FROM Student WHERE first_name= :searchName%", nativeQuery = true)
public List<Student> findByFirstNameLike(String searchName);