隨筆 - 100  文章 - 50  trackbacks - 0
          <2016年8月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          我收藏的一些文章!

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          首先,mysql需要數據庫連接配置&allowMultiQueries=true

          jdbc:mysql://127.0.0.1:3306/mybank?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true

          oracle下支持執行多條語句,下面3個相同

          <update id="batchUpdate" parameterType="java.util.List">
                  <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";" > 
                      update T_EMP_1 
                      <set>       
                          age = #{item.age}+1,name=#{item.name}
                      </set>
                      where id = #{item.id}
                  </foreach>
          </update>

          <update id="batchUpdate" parameterType="java.util.List">
                  <foreach collection="list" item="item" index="index" open="begin" close="end;" separator="" > 
                      update T_EMP_1 
                      <set>       
                          age = #{item.age}+1,name=#{item.name}
                      </set>
                      where id = #{item.id};
                  </foreach>
              </update>

          <update id="batchUpdate" parameterType="java.util.List">
                  begin
                  <foreach collection="list" item="item" index="index" separator="" > 
                      update T_EMP_1 
                      <set>       
                          age = #{item.age}+1,name=#{item.name}
                      </set>
                      where id = #{item.id};
                  </foreach>
                  end;
              </update>

          foreach的主要用在構建in條件中,它可以在SQL語句中進行迭代一個集合。foreach元素的屬性主要有 item,index,collection,open,separator,close。item表示集合中每一個元素進行迭代時的別名,index指 定一個名字,用于表示在迭代過程中,每次迭代到的位置,open表示該語句以什么開始,separator表示在每次進行迭代之間以什么符號作為分隔 符,close表示以什么結束,在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,但是在不同情況 下,該屬性的值是不一樣的,主要有一下3種情況: 
          1. 如果傳入的是單參數且參數類型是一個List的時候,collection屬性值為list 
          2. 如果傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值為array 
          3. 如果傳入的參數是多個的時候,我們就需要把它們封裝成一個Map了,當然單參數也可以封裝成map,實際上如果你在傳入參數的時候,在breast里面也是會把它封裝成一個Map的,map的key就是參數名,所以這個時候collection屬性值就是傳入的List或array對象在自己封裝的map里面的key

           批量刪除

          <delete id="batchDeleteStudent" parameterType="List">
          DELETE FROM STUDENT WHERE id IN
          <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
               #{item}
          </foreach>
          </delete>

          批量更新 注意:oracle中 形如 update *** set *** where ** in(....) 這種語句 in所在的集合有條數限制 為1000條

          <update id="batchUpdateStudent" parameterType="List">      
          UPDATE STUDENT SET name = "5566" WHERE id IN
          <foreach collection="list" item="item" index="index" open="(" separator="," close=")" >
          #{item}
          </foreach>
          </update>

          <update id="batchUpdateStudentWithMap" parameterType="Map" > 
          UPDATE STUDENT SET name = #{name} WHERE id IN
          <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
          #{item}
          </foreach>
          </update>

          批量插入  mysql和oracle不一樣

          mysql:
          <insert id="batchInsertStudent" parameterType="java.util.List">  
              INSERT INTO STUDENT (id,name,sex,tel,address)  
              VALUES   
              <foreach collection="list" item="item" index="index" separator="," >  
                  (#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address})  
              </foreach>  
          </insert>

          oracle:
          <insert id="insertBatch4Oracle" parameterType="List">
                  insert into aa(a,b)
                  <foreach collection="list" item="item" index="index" separator="union all" >
                     select  #{item.a},#{item.b} from dual
                  </foreach>
           </insert>
          posted on 2016-08-16 18:46 fly 閱讀(194) 評論(0)  編輯  收藏 所屬分類: 數據庫學習
          主站蜘蛛池模板: 积石山| 景泰县| 雷波县| 双柏县| 东乌珠穆沁旗| 古蔺县| 读书| 内丘县| 乐清市| 桐乡市| 乌兰浩特市| 冀州市| 当雄县| 本溪市| 师宗县| 原平市| 恩平市| 乌鲁木齐市| 沅陵县| 涞水县| 金华市| 巴青县| 买车| 亳州市| 阳西县| 霍城县| 晋州市| 池州市| 上虞市| 舞钢市| 延安市| 桓仁| 海晏县| 特克斯县| 庆阳市| 和田县| 饶平县| 东阿县| 永清县| 凭祥市| 伊金霍洛旗|