看两个最基本的语句
①select * from student where name like 'xy'
②select * from student where naem like '%xy%'
两个语句都是用了like,貌似都是模糊查询的样子。其实①实际上是一个完全匹配的查询,与用等号写成如下语句是一致的。①语句等价于select * from student where name = 'xy'。这就给在ibatis写模糊查询语句带来启示。
例子一
<dynamic prepend="where">
<isNotNull prepend="AND" property="id">
id = #id#
</isNotNull>
<isNotNull prepend="AND" property="name">
name like #name#
</isNotNull>
</dynamic>
该例子与①语句错误相同
例子二
<dynamic prepend="where">
<isNotNull prepend="AND" property="id">
id = #id#
</isNotNull>
<isNotNull prepend="AND" property="name">
name like '%#name#%'
</isNotNull>
</dynamic>
报错:java.sql.SQLException: Invalid argument in JDBC call: parameter index out of range: 1
例子三 正确
<dynamic prepend="where">
<isNotNull prepend="AND" property="id">
id = #id#
</isNotNull>
<isNotNull prepend="AND" property="name">
name like '%$name$%'
</isNotNull>
</dynamic>
"$"操作符相当于在SQL语句被转变为参数化语句之前,将值插入该语句