mybatis的批量操作

来源:互联网 发布:slack软件应用 编辑:程序博客网 时间:2024/05/21 08:53

1.批量更新
1)mysql

<update id="updateStoreTask" parameterType="Object">        <foreach collection="storeTaskList" item="item" index="index" separator=";">            update t_store_task set                store_sale_task=#{item.storeSaleTask}                where task_id=#{taskId} AND store_no=#{item.storeNo}            </foreach>    </update>

注意:
1.在配置时必须加上allowMultiQueries=true如:
url=”jdbc:mysql://localhost:3306/testDatabase?allowMultiQueries=true”
allowMultiQueries=true时,可以允许一次执行多条sql(通过分号分割)
2.最后编译的结果为

Preparing: update t_store_task set store_sale_task=? where task_id=? AND store_no=? ; update t_store_task set store_sale_task=? where task_id=? AND store_no=? 

2)oracle

<!--oracle-->    <update id="updateStoreTask" parameterType="Object">        <foreach collection="storeTaskList" item="item" index="index" open="begin" close=";end;" separator=";">            update t_store_task set            store_sale_task=#{item.storeSaleTask}            where task_id=#{taskId} AND store_no=#{item.storeNo}        </foreach>    </update>

@see https://www.cnblogs.com/feixian/p/5960111.html

2.批量插入
1)mysql

<insert id="batchAdd" parameterType="Object">        insert into        t_store_task(task_id,store_no,store_name,store_sale_task)        values        <foreach collection="storeTaskList" item="item" index="index" separator=",">            (#{taskId},#{item.storeNo},#{item.storeName},#{item.storeSaleTask})        </foreach>    </insert>

最后编译的结果为

 Preparing: insert into t_store_task(task_id,store_no,store_name,store_sale_task) values (?,?,?,?) , (?,?,?,?) 

3.批量筛选
in条可以实现通过id批量查询,批量删除,批量更新
动态 SQL 的另外一个常用的必要操作是需要对一个集合进行遍历,通常是在构建 IN 条件语句的时候。

<if test="categoryList != null and categoryList.size()!=0">                and produt_pl IN                <foreach collection="categoryList" item="item" index="index" open="(" close=")" separator=",">                    #{item.categoryNo}                </foreach>            </if>

编译结果:

and produt_pl IN ( ? , ? )

二。插入不成功执行更新
1.oracle

<!--插入不成功,则执行更新-->    <insert id="batchSaveOrUpdate" parameterType="Object">        MERGE INTO        t_store_task T1        USING (        <foreach collection="storeTaskList" item="item" index="index" separator="union">            SELECT #{taskId} task_id,            #{item.storeNo} store_no,            #{item.storeName} store_name,            <choose>                <when test="item.storeSaleTask!=null">                    #{item.storeSaleTask} store_sale_task                </when>                <otherwise>                    0 store_sale_task                </otherwise>            </choose>            FROM DUAL        </foreach>        ) T        ON (T1.task_id = T.task_id AND T1.store_no = T.store_no)        WHEN MATCHED THEN        UPDATE SET T1.store_sale_task=T.store_sale_task        WHEN NOT MATCHED THEN        INSERT        (task_id,store_no,store_name,store_sale_task)        VALUES        (T.task_id,T.store_no,T.store_name,T.store_sale_task)    </insert>

@see http://blog.csdn.net/gjldwz/article/details/17414155

原创粉丝点击