运行此命令时,我可以获得结果,但只有当我输入全名时。.
@Query(value = "SELECT * FROM Student WHERE first_name=?1", nativeQuery = true)
public List<Student> findByFirstNameLike(String searchName);
现在我尝试添加类似运算符
@Query(value = "SELECT * FROM Student WHERE first_name like=?1", nativeQuery = true)
public List<Student> findByFirstNameLike(String searchName);
}
我通过输入Dan并点击搜索来寻找学生Daniel,这是一个例外
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
我什至尝试将=更改为这样的%。
WHERE first_name like%?1
点2可以工作,但是点4和6给我SQLSyntaxErrorException,我该如何解决?谢谢
我也尝试过使用本链接中提到的@Param注释,但仍然获得语法异常https://stackoverflow.com/a/38115592/4325878
答案 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);