1. 插入map
为了方便就不要service层了
controller 层
Map<Integer,String> map=new HashMap();
map.put(5,"sb");
map.put(6,"dsb");
map.put(7,"wbd");
if (testDao.insert(map)>0){
return true;
}else {
return false;
}
dao层
int insert(@Param("map") Map map);
mapper层
<insert id="insert" parameterType="map" useGeneratedKeys="true">
insert into sys_post (post_id,post_code) values
<foreach collection="map" item="value" index="key" separator=",">
(#{key},#{value})
</foreach>
</insert>
注意:
dao层参数必须用@param,值可以随便填,但是mapper中的collection要与之相同,其他为固定写法
结果
2. 插入list对象
对象实体类
public class User {
private Integer id;
private String value;
}
controller层
public boolean showKeyName1(@RequestBody List<User> user) {
if (testDao.insert1(user) > 0) {
return true;
} else {
return false;
}
}
请求参数
[{
"id":100,
"value":"sb"
},
{
"id":101,
"value":"s11b"
}]
dao层
int insert1(List<User> user);
mapper层
<insert id="insert1" parameterType="java.util.List" useGeneratedKeys="true">
insert into sys_post (post_id, post_code) values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.id,jdbcType=INTEGER},
#{item.value,jdbcType=VARCHAR}
)
</foreach>
</insert>
注意:当传入list时,collection要写list,item的值随意,但是取对象时要使用,其他为固定写法