遍历数组:
int saveOptions(@Param("items")String[] items, @Param("questionId")int questionId) throws Exception;
<insert id="saveOptions">
<foreach item="item" index="index" collection="items" separator=";">
insert into options values(#{item},#{questionId},1)
</foreach>
</insert>
遍历集合:参考之前文章:
20180626:嵌套list遍历
int saveOptions(@Param("list")List<List<String>> list) throws Exception;
<insert id="saveOptions"> insert into options(optionsName,remark,selectOptions) values <foreach item="items" collection="list" separator=","> ( <foreach item="item" collection="items" separator=","> #{item} </foreach> ) </foreach> </insert>
其中collection="list"是指遍历的集合名称是list
item="items"是指遍历的每一项别名叫items
20190319传入json:
//传参: JSONObject jsonParam= new JSONObject(); jsonParam.put("yananId=", 1); jsonParam.put("yananName=","张三"); //接口 List<Map<String, Object>> getListByWhere(@Param("jsonParam")JSONObject jsonParam); //mybatis <select id="getListByWhere" parameterType="net.sf.json.JSONObject" resultType="java.util.Map"> select yananId,yananName from yanan <foreach collection="jsonParam" open="where" separator="and" item="jsonValue" index="jsonKey"> ${jsonKey} #{jsonValue} </foreach> </select>
打印sql:
select yananId,yananName from yanan where yananId=? and yananName =? ==> Parameters: 68(Integer), 张三(String)