直接看代码:
@Test
public void testInsertList() {
HashMap<String, Object> param = new HashMap<>();
param.put("key1", "value1");
param.put("key2", "value2");
List<String> list = new ArrayList<String>();
list.add("list value 01");
list.add("list value 02");
param.put("testList", list);
testDao.testInsertList(param);
}
Dao层很简单,就一个insert的方法:
@Mapper
public interface TestDao {
public void testInsertList(HashMap<String, Object> param);
}
看mapper文件:
<insert id="testInsertList" parameterType="HashMap">
insert into test
(col1,
col2,
col3)
values
<foreach collection="testList" separator = "," item = "item">
(
#{key1},
#{item},
#{key2}
)
</foreach>
</insert>
经过测试,可以正常的添加到数据库: