最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用Mybatis怎么實現(xiàn)批量更新

這篇文章將為大家詳細講解有關使用Mybatis怎么實現(xiàn)批量更新,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設,同心企業(yè)網(wǎng)站建設,同心品牌網(wǎng)站建設,網(wǎng)站定制,同心網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,同心網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

方式一:

 
  
    update tableName
    
      name=${item.name},
      name2=${item.name2}
    
    where id = ${item.id}
     

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

方式二:


    update tableName
    
      
        
          
            when id=#{cus.id} then #{cus.name}
          
        
      
      
        
          
            when id=#{cus.id} then #{cus.age}
          
        
      
    
    
      
        id = #{cus.id}
      
    

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

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

方式三:

臨時改表sqlSessionFactory的屬性,實現(xiàn)批量提交的java,但無法返回受影響數(shù)量。

public int updateBatch(List 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;//提交數(shù)量,到達這個數(shù)量就提交
      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 是自己的更具業(yè)務需求的Mapper接口類,Object是對象。

總結(jié)

  • 方式一 需要修改mysql的連接url,讓全局支持多sql執(zhí)行,不太安全

  • 方式二 當數(shù)據(jù)量大的時候 ,效率明顯降低

  • 方式三 需要自己控制,自己處理,一些隱藏的問題無法發(fā)現(xiàn)。

附件: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;
    }
  }
}

關于使用Mybatis怎么實現(xiàn)批量更新就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。


分享文章:使用Mybatis怎么實現(xiàn)批量更新
網(wǎng)頁URL:http://fisionsoft.com.cn/article/pdisjh.html