Mybatis批量更新三種方式的實現

這篇文章主要介紹了Mybatis批量更新三種方式的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨着小編來一起學習學習吧

Mybatis實現批量更新操作

方式一:

<update id="updateBatch" parameterType="java.util.List"> 
  <foreach collection="list" item="item" index="index" open="" close="" separator=";">
    update tableName
    <set>
      name=${item.name},
      name2=${item.name2}
    </set>
    where id = ${item.id}
  </foreach>   
</update>

但Mybatis映射文件中的sql語句默認是不支持以" ; " 結尾的,也就是不支持多條sql語句的執行。所以需要在連接mysql的url上加 &allowMultiQueries=true 這個纔可以執行。

方式二:

<update id="updateBatch" parameterType="java.util.List">
    update tableName
    <trim prefix="set" suffixOverrides=",">
      <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>
    <where>
      <foreach collection="list" separator="or" item="cus">
        id = #{cus.id}
      </foreach>
    </where>
</update>

這種方式貌似效率不高,但是可以實現,而且不用改動mysql連接

效率參考文章:https://www.jb51.net/article/155835.htm

方式三:

臨時改表sqlSessionFactory的屬性,實現批量提交的java,但無法返回受影響數量。

public int updateBatch(List<Object> list){
    if(list ==null || list.size() <= 0){
      return -1;
    }
    SqlSessionFactory sqlSessionFactory = SpringContextUtil.getBean("sqlSessionFactory");
    SqlSession sqlSession = null;
    try {
      sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
      Mapper mapper = sqlSession.getMapper(Mapper.class);
      int batchCount = 1000;//提交數量,到達這個數量就提交
      for (int index = 0; index < list.size(); index++) {
        Object obj = list.get(index);
        mapper.updateInfo(obj);
        if(index != 0 && index%batchCount == 0){
          sqlSession.commit();
        }          
      }
      sqlSession.commit();
      return 0;
    }catch (Exception e){
      sqlSession.rollback();
      return -2;
    }finally {
      if(sqlSession != null){
        sqlSession.close();
      }
    }
    
}

其中 SpringContextUtil 是自己定義的工具類 用來獲取spring加載的bean對象,其中getBean() 獲得的是想要得到的sqlSessionFactory。Mapper 是自己的更具業務需求的Mapper接口類,Object是對象。

總結

  • 方式一 需要修改mysql的連接url,讓全局支持多sql執行,不太安全
  • 方式二 當數據量大的時候 ,效率明顯降低
  • 方式三 需要自己控制,自己處理,一些隱藏的問題無法發現。

附件:SpringContextUtil.java

@Component
public class SpringContextUtil implements ApplicationContextAware{

  private static ApplicationContext applicationContext;

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    SpringContextUtil.applicationContext = applicationContext;
  }

  public static ApplicationContext getApplicationContext(){
    return applicationContext;
  }

  public static Object getBean(Class T){
    try {
      return applicationContext.getBean(T);
    }catch (BeansException e){
      return null;
    }
  }

  public static Object getBean(String name){
    try {
      return applicationContext.getBean(name);
    }catch (BeansException e){
      return null;
    }
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章