mybatis的foreach写用法

一、mybatis查询

public abstract List<Model> findByIds(@Param("ids")List<Integer> ids);
select * from table
<where>
    id in <foreach collection="ids" item="item" index="index" 
open="(" separator="," close=")">#{item}</foreach>
</where>

二、mybatis插入

public abstract void saves(@Param("tables")List<Model> tables);
insert into table(name,addtime) values
<foreach collection="tables" item="item" index="index" separator=",">  
    (#{item.name},#{item.addtime})
</foreach>

以上方法Mybatis会帮我们进行sql注入拦截,Mybatis如果采用#{xxx}的形式设置参数,Mybatis会进行sql注入的过滤。如果采用的是${xxx},Mybatis不会进行sql注入过滤,而是直接将参入的内容输出为sql语句。

posted @ 2015-11-09 18:41  花语苑  阅读(1085)  评论(0编辑  收藏  举报