mybatis @insert 批量插入 mapList foreach
代码如下
@Insert({
"<script>",
"insert",
"into tableName(",
"<foreach collection='colList' item='col' separator=',' >",
"${col}",
"</foreach>",
") values",
"<foreach collection='mapList' item='map' separator=',' >",
"<foreach collection='map.values' item='value' open='(' separator=',' close=')'>",
"#{value}",
"</foreach>",
"</foreach>",
"</script>"
})
void insertMapList(@Param("mapList") List<LinkedHashMap<String, String>> mapList, @Param("colList") List<String> colList);
需注意字段名与值的顺序要对应
参数 | 含义 |
---|---|
colList | 数据库字段list |
mapList | 值 |
附插入单条的:
@Insert({
"<script>",
"insert into tableName(",
"<foreach collection='map.keys' item='key' separator=',' >",
"${key}",
"</foreach>",
") values",
"<foreach collection='map.keys' item='key' open='(' separator=',' close=')'>",
"#{map[${key}]}",
"</foreach>",
"</script>"
})
void insertMap(@Param("map") Map<String,String> map);