mybatis批量新增和修改删除

接口:

//批量新增
int batchInsertGoods(List<user> list);

//根据ids批量获取Goods列表
List<Map<String, Object>> getGoodsList(List<String> ids);

Mybatis:

<!-- 批量新增-->
<insert id="batchInsertGoods" parameterType="java.util.List">
    INSERT INTO user(id, userName,passWord,realName)
    VALUES
    <foreach collection="list" item="item" separator=",">
        (#{item.id,jdbcType=VARCHAR},#{item.userName,jdbcType=VARCHAR},
         #{item.passWord,jdbcType=VARCHAR},
         #{item.realName,jdbcType=VARCHAR})
    </foreach>
</insert>

  

<!-- 根据ids批量获取Goods列表-->
<select id="getGoodsList" parameterType="java.util.List" resultType="java.util.Map" >
    SELECT id, goods_name WHERE id in
       <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
           #{item}
       </foreach>
</select>

  

批量更新

批量更新必须在添加如下数据库连接配置:&allowMultiQueries=true,否则会报SQL格式错误

url: jdbc:mysql://localhost:3306/springboot_mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC&allowMultiQueries=true

接口:

 //批量修改
    int batchUpdateGoodsByIds(List<User> wordsList);

Mybatis:

<!--批量修改方式 -->
<update id="batchUpdateGoodsByIds" parameterType="java.util.List" >
      <foreach collection="list" item="item" index="index" separator=";">
       update user
       <set >
          <if test="item.userName != null" >
            userName = #{item.userName,jdbcType=VARCHAR},
          </if>
          <if test="item.passWord != null" >
            passWord = #{item.passWord,jdbcType=VARCHAR},
          </if>
          <if test="item.realName != null" >
           realName = #{item.realName,jdbcType=VARCHAR},
          </if>
        </set>
        where id = #{item.id,jdbcType=VARCHAR}
    </foreach>
  </update>

 

 

 

 

参考:https://blog.csdn.net/lan_qinger/article/details/84138216

https://blog.csdn.net/weixin_30650039/article/details/94846944?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control

 

posted @ 2020-12-02 01:45  java_静止  阅读(556)  评论(0编辑  收藏  举报