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的值随意,但是取对象时要使用,其他为固定写法

结果

在这里插入图片描述

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐