异常出现在
@Select("<script>" +
"SELECT *FROM students " +
"<where>" +
"<if test = 'selectStr!=null and selectStr!=\"\"'>" +
"and `name` LIKE \"%#{selectStr}%\"" +
"</if>" +
"</where>" +
"ORDER BY create_date DESC LIMIT #{page},#{size}" +
"</script>")
List<Student> fetchAll(@Param("selectStr") String selectStr, @Param("page") int page, @Param("size") int size);
//预处理语句为
Preparing: SELECT *FROM students WHERE `name` LIKE "%?%" ORDER BY create_date DESC LIMIT ?,?
//selectStr传入值抛出异常
java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
将if标签中的#{}换成${}成功运行
或者如下操作
`name` LIKE '%'#{selectStr}'%'"
#传入的参数在SQL中显示为字符串(当成一个字符串),会对自动传入的数据加一个双引号。
--在sql终可以执行成功
SELECT *FROM students WHERE `name` LIKE '%'"终"'%'