您的位置:首页 > 其它

mybatis的foreach写用法

2015-11-09 18:41 423 查看
一、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语句。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: