mybatis批量操做

首先,mysql須要數據庫鏈接配置&allowMultiQueries=truejava

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

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

<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條oracle

<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不同spa

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>
相關文章
相關標籤/搜索