天天看點

mybatis在Oracle資料庫中的批量插入用法——項目經驗

此文章隻記錄mybatis在Oracle資料庫中的批量插入用法:

這裡主要的關注點在于foreach裡面,将需要插入的字段,通過from dual來生成虛表,然後以union all來連接配接即可,項目中親測可用;

<insert id="insertList" parameterType="java.util.List">

        INSERT INTO tb_message_list(

            id,

            userid,

            title,

            sent_time,

            url,

            flag,

            create_by,

            create_date,

            update_by,

            update_date,

            remarks,

            del_flag,

            type

        )

        <foreach collection ="list" item="tbMessageList" separator ="UNION ALL">

            (

                SELECT 

                 #{tbMessageList.id},

                #{tbMessageList.userid},

                #{tbMessageList.title},

                #{tbMessageList.sentTime},

                #{tbMessageList.url},

                #{tbMessageList.flag},

                #{tbMessageList.createBy.id},

                #{tbMessageList.createDate},

                #{tbMessageList.updateBy.id},

                #{tbMessageList.updateDate},

                #{tbMessageList.remarks},

                #{tbMessageList.delFlag},

                #{tbMessageList.type}

                FROM DUAL

            )

        </foreach >

    </insert>