温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在mybatis中批量更新数据

发布时间:2021-05-18 18:00:39 来源:亿速云 阅读:6103 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关如何在mybatis中批量更新数据,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

其实这种东西写过来写过去就是差不多一样的代码,不做重复的赘述,直接上代码。

 <!-- 这次用resultmap接收输出结果 -->
  <select id="findByName" parameterType="string" resultMap="customerMap">
    select * from t_customer where c_name like concat('%', #{name},'%') order by c_ceroNo limit 0,100
  </select>
 
 
  <!-- 批量更新第一种方法,通过接收传进来的参数list进行循环着组装sql -->
  <update id="batchUpdate" parameterType="java.util.Map">
    <!-- 接收list参数,循环着组装sql语句,注意for循环的写法
       separator=";" 代表着每次循环完,在sql后面放一个分号
       item="cus" 循环List的每条的结果集
       collection="list" list 即为 map传过来的参数key -->
    <foreach collection="list" separator=";" item="cus">
      update t_customer set
      c_name = #{cus.name},
      c_age = #{cus.age},
      c_sex = #{cus.sex},
      c_ceroNo = #{cus.ceroNo},
      c_ceroType = #{cus.ceroType}
      where id = #{cus.id}
    </foreach>
  </update>
 
  <!-- 批量更新第二种方法,通过 case when语句变相的进行批量更新 -->
  <update id="batchUpdateCaseWhen" parameterType="java.util.Map">
    update t_customer
    <trim prefix="set" suffixOverrides=",">
      <!-- 拼接case when 这是一种写法 -->
      <!--<foreach collection="list" separator="" item="cus" open="c_age = case id" close="end, ">-->
      <!--when #{cus.id} then #{cus.age}-->
      <!--</foreach>-->
 
      <!-- 拼接case when 这是另一种写法,这种写着更专业的感觉 -->
      <trim prefix="c_name =case" suffix="end,">
        <foreach collection="list" item="cus">
          <if test="cus.name!=null">
            when id=#{cus.id} then #{cus.name}
          </if>
        </foreach>
      </trim>
      <trim prefix="c_age =case" suffix="end,">
        <foreach collection="list" item="cus">
          <if test="cus.age!=null">
            when id=#{cus.id} then #{cus.age}
          </if>
        </foreach>
      </trim>
      <trim prefix="c_sex =case" suffix="end,">
        <foreach collection="list" item="cus">
          <if test="cus.sex!=null">
            when id=#{cus.id} then #{cus.sex}
          </if>
        </foreach>
      </trim>
      <trim prefix="c_ceroNo =case" suffix="end,">
        <foreach collection="list" item="cus">
          <if test="cus.ceroNo!=null">
            when id=#{cus.id} then #{cus.ceroNo}
          </if>
        </foreach>
      </trim>
      <trim prefix="c_ceroType =case" suffix="end,">
        <foreach collection="list" item="cus">
          <if test="cus.ceroType!=null">
            when id=#{cus.id} then #{cus.ceroType}
          </if>
        </foreach>
      </trim>
    </trim>
    <where>
      <foreach collection="list" separator="or" item="cus">
        id = #{cus.id}
      </foreach>
    </where>
  </update>

接口

 List<Customer> findByName(String name);
 
  int batchUpdate(Map<String,Object> param);
 
  int batchUpdateCaseWhen(Map<String,Object> param);

实现类

 /**
   * 用于更新时,获取更新数据
   * @param name
   * @return
   */
  public List<Customer> findByName(String name) {
    SqlSession sqlSession = null;
    try {
      sqlSession = SqlsessionUtil.getSqlSession();
      return sqlSession.selectList("customer.findByName", name);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      SqlsessionUtil.closeSession(sqlSession);
    }
    return new ArrayList<Customer>();
  }
 
 
  /**
   * 批量更新第一种方式
   * @param param
   * @return
   */
  public int batchUpdate(Map<String,Object> param) {
    return bathUpdate("customer.batchUpdate",param);
  }
 
  /**
   * 批量更新第二种方式
   * @param param
   * @return
   */
  public int batchUpdateCaseWhen(Map<String,Object> param) {
    return bathUpdate("customer.batchUpdateCaseWhen",param);
  }
 
  /**
   * 公共部分提出
   * @param statementId
   * @param param
   * @return
   */
  private int bathUpdate(String statementId,Map param){
    SqlSession sqlSession = null;
    try {
      sqlSession = SqlsessionUtil.getSqlSession();
      int key = sqlSession.update(statementId, param);
      // commit
      sqlSession.commit();
      return key;
    } catch (Exception e) {
      sqlSession.rollback();
      e.printStackTrace();
    } finally {
      SqlsessionUtil.closeSession(sqlSession);
    }
    return 0;
  }

测试前准备   首先用上节的 mybatis学习之路----批量更新数据 批量插入,插入10000条数据以备下面的批量更新用。

@Test
  public void batchInsert() throws Exception {
    Map<String,Object> param = new HashMap<String,Object>();
    List<Customer> list = new ArrayList<Customer>();
    for(int i=0;i<10000;i++){
      Customer customer = new Customer();
      customer.setName("准备数据" + i);
      customer.setAge(15);
      customer.setCeroNo("111111111111"+i);
      customer.setCeroType(2);
      customer.setSex(1);
      list.add(customer);
    }
    param.put("list",list);
    Long start = System.currentTimeMillis();
    int result = customerDao.batchInsert(param);
    System.out.println("耗时 : "+(System.currentTimeMillis() - start));
  }

开始进行测试效率问题。

首先进行的是测试十条数据。调整查询数据为查询十条

  <!-- 这次用resultmap接收输出结果 -->
  <select id="findByName" parameterType="string" resultMap="customerMap">
    select * from t_customer where c_name like concat('%', #{name},'%') order by c_ceroNo limit 0,10
  </select>

测试类

  @Test
  public void batchudpate() throws Exception {
    Map<String,Object> param = new HashMap<String,Object>();
 
    param.put("list",getFindByName("准备数据","批量更新01"));
    Long start = System.currentTimeMillis();
    customerDao.batchUpdate(param);
    System.out.println("耗时 : "+(System.currentTimeMillis() - start));
  }
 
  @Test
  public void batchudpateCaseWhen() throws Exception {
    Map<String,Object> param = new HashMap<String,Object>();
    param.put("list",getFindByName("批量更新01","准备数据"));
    Long start = System.currentTimeMillis();
    customerDao.batchUpdateCaseWhen(param);
    System.out.println("耗时 : "+(System.currentTimeMillis() - start));
  }
 
  private List<Customer> getFindByName(String name, String change){
    List<Customer> list = customerDao.findByName(name);
    System.out.println("查询出来的条数 : " + list.size());
    if(null != change && !"".equals(change)){
      for(Customer customer : list){
        customer.setName(change);
      }
    }
 
    return list;
  }

第一种拼完整sql的方式耗时:

如何在mybatis中批量更新数据

第二种case when 耗时情况:

如何在mybatis中批量更新数据

结果可以看出,其实case when 耗时比较多。

下面来加大数据量到100条;

第一种拼完整sql的方式耗时:

如何在mybatis中批量更新数据

第二种case when 耗时情况:

如何在mybatis中批量更新数据

结果可以看出,其实case when 耗时仍然比第一种多。

继续加大数据量到1000条

第一种拼完整sql的方式耗时:

如何在mybatis中批量更新数据

第二种case when 耗时情况:

如何在mybatis中批量更新数据

结果可以看出,其实case when 耗时仍然比第一种多。

继续加大数据量到10000条

第一种拼完整sql的方式耗时:

如何在mybatis中批量更新数据

第二种case when 耗时情况:

如何在mybatis中批量更新数据

结果可以看出,两种方式进行批量更新,效率已经不在一个数量级了。case when明显的慢的多。

看网上有人说第一种的效率跟用代码循环着一条一条的循环着插入的效率差不多,通过测试我就有疑问了,他是怎么做到的。难道我的代码有问题?明明第一种的效率很高嘛。

第一种效率其实相当高的,因为它仅仅有一个循环体,只不过最后update语句比较多,量大了就有可能造成sql阻塞。

第二种虽然最后只会有一条更新语句,但是xml中的循环体有点多,每一个case when 都要循环一遍list集合,所以大批量拼sql的时候会比较慢,所以效率问题严重。使用的时候建议分批插入。

看完上述内容,你们对如何在mybatis中批量更新数据有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI