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

  • 相关阅读:
    Webwork【04】Configuration 详解
    Webwork【03】核心类 ServletDispatcher 的初始化
    Webwork【02】前端OGNL试练
    Webwork【01】Webwork与 Struct 的前世今生
    Oracle 数据库日常巡检
    php jquery ajax select 二级联动【get方式】
    PHP+ajax实现二级联动【post+json方式】
    thinkphp中在页面怎么格式输出小数和时间
    DataTables Bootstrap 3 example
    Bootstrap表格动态加载内容和排序功能
  • 原文地址:https://www.cnblogs.com/javakangkang/p/14071719.html
Copyright © 2011-2022 走看看