• 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

  • 相关阅读:
    getParameter和getAttribute的区别
    forward和sendRedirect的区别
    关于html/css的路径问题
    手写ORM
    数据库其他使用方法介绍
    Navicat使用与python操作数据库
    表查询
    表与表之间的三种关系
    SQL语法
    MySQL数据库的安装与使用
  • 原文地址:https://www.cnblogs.com/javakangkang/p/14071719.html
Copyright © 2020-2023  润新知