mybatis foreach 语句循环读取map数据

来源:互联网 发布:java ssh 书籍 编辑:程序博客网 时间:2024/05/17 22:50

 在××maper.java中添加@parame注解;

int Insert(@Param("rd")Map<String, Object> rd);

在对应的mapper.xml文件中如下写入;


<insert id="Insert" parameterType="java.util.HashMap">
insert into sys_code_para
<foreach collection="rd" index="key" item="value"
open="(" close=")" separator=",">
<if test="key !=null ">
${key}
</if>
</foreach>
values
<foreach collection="rd" index="key" item="value"
open="(" close=")" separator=",">
<if test="key !=null ">
#{value}
</if>
</foreach>
</insert>


最后打印sql语句如下:

==>  Preparing: insert into sys_code_para ( sys_code , para_code , para_name , sys_name , para_state , para_value1 ) values ( ? , ? , ? , ? , ? , ? ) 

ok,特此记下;