今天就跟大家聊聊有關如何在mybatis中批量更新數(shù)據(jù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
站在用戶的角度思考問題,與客戶深入溝通,找到民權(quán)網(wǎng)站設計與民權(quán)網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站設計、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、國際域名空間、虛擬主機、企業(yè)郵箱。業(yè)務覆蓋民權(quán)地區(qū)。
其實這種東西寫過來寫過去就是差不多一樣的代碼,不做重復的贅述,直接上代碼。
<!-- 這次用resultmap接收輸出結(jié)果 --> <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> <!-- 批量更新第一種方法,通過接收傳進來的參數(shù)list進行循環(huán)著組裝sql --> <update id="batchUpdate" parameterType="java.util.Map"> <!-- 接收list參數(shù),循環(huán)著組裝sql語句,注意for循環(huán)的寫法 separator=";" 代表著每次循環(huán)完,在sql后面放一個分號 item="cus" 循環(huán)List的每條的結(jié)果集 collection="list" list 即為 map傳過來的參數(shù)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 這是另一種寫法,這種寫著更專業(yè)的感覺 --> <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);
實現(xiàn)類
/** * 用于更新時,獲取更新數(shù)據(jù) * @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; }
測試前準備 首先用上節(jié)的 mybatis學習之路----批量更新數(shù)據(jù) 批量插入,插入10000條數(shù)據(jù)以備下面的批量更新用。
@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("準備數(shù)據(jù)" + 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)); }
開始進行測試效率問題。
首先進行的是測試十條數(shù)據(jù)。調(diào)整查詢數(shù)據(jù)為查詢十條
<!-- 這次用resultmap接收輸出結(jié)果 --> <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("準備數(shù)據(jù)","批量更新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","準備數(shù)據(jù)")); 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("查詢出來的條數(shù) : " + list.size()); if(null != change && !"".equals(change)){ for(Customer customer : list){ customer.setName(change); } } return list; }
第一種拼完整sql的方式耗時:
第二種case when 耗時情況:
結(jié)果可以看出,其實case when 耗時比較多。
下面來加大數(shù)據(jù)量到100條;
第一種拼完整sql的方式耗時:
第二種case when 耗時情況:
結(jié)果可以看出,其實case when 耗時仍然比第一種多。
繼續(xù)加大數(shù)據(jù)量到1000條
第一種拼完整sql的方式耗時:
第二種case when 耗時情況:
結(jié)果可以看出,其實case when 耗時仍然比第一種多。
繼續(xù)加大數(shù)據(jù)量到10000條
第一種拼完整sql的方式耗時:
第二種case when 耗時情況:
結(jié)果可以看出,兩種方式進行批量更新,效率已經(jīng)不在一個數(shù)量級了。case when明顯的慢的多。
看網(wǎng)上有人說第一種的效率跟用代碼循環(huán)著一條一條的循環(huán)著插入的效率差不多,通過測試我就有疑問了,他是怎么做到的。難道我的代碼有問題?明明第一種的效率很高嘛。
第一種效率其實相當高的,因為它僅僅有一個循環(huán)體,只不過最后update語句比較多,量大了就有可能造成sql阻塞。
第二種雖然最后只會有一條更新語句,但是xml中的循環(huán)體有點多,每一個case when 都要循環(huán)一遍list集合,所以大批量拼sql的時候會比較慢,所以效率問題嚴重。使用的時候建議分批插入。
看完上述內(nèi)容,你們對如何在mybatis中批量更新數(shù)據(jù)有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
文章標題:如何在mybatis中批量更新數(shù)據(jù)
轉(zhuǎn)載來于:http://www.rwnh.cn/article46/ghceeg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、微信小程序、全網(wǎng)營銷推廣、虛擬主機、做網(wǎng)站、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)