ibatis中使用like模糊查询

来源:互联网 发布:淘宝海报图片 编辑:程序博客网 时间:2024/04/27 17:46

1.问题

select * from t_comment where content LIKE '%#keyword#%'

提示:Parameter index out of range (x > number of parameters, which is y.),x、y为数字

2.解决
参考http://www.cnblogs.com/gaojing/archive/2013/01/04/2844932.html
2.1 使用$代替#。此种方法就是去掉了类型检查,使用字符串连接,不过可能会有sql注入风险。
select * from t_comment where content LIKE '%$keyword$%'
2.2 使用连接符。不过不同的数据库中方式不同。

 mysql: 

<span style="font-size:14px;">select  *  from t_comment where content like concat('%', #keyword#, '%')</span>
 oracle:

<span style="font-size:14px;">select  *  from t_comment where content like '%' || #keyword# || '%'</span>
 sql server:

<span style="font-size:14px;">select  *  from t_comment where content like '%' + #keyword# + '%'</span>

0 0