zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【java】定时任务@Scheduled
    20180513 实参 形参 数组
    20180513 实参 形参
    20180513 数组 实参 形参
    <转载>二维数组回形遍历
    20180318 代码错题(8)
    20180318 代码错题(7)
    20180318 代码错题(6)
    20180318 代码错题(5)
    20180318 bit置0
  • 原文地址:https://www.cnblogs.com/javakangkang/p/14071719.html
Copyright © 2011-2022 走看看