プログラミング逆引き辞典

~ 多言語対応のプログラミングレシピ ~

MyBatis バルクアップデート(MySql編)

■Mapper.java

メソッド名を定義する
 

【基本構文】

//メソッドの戻り値はレコード数なので不要な場合は「void」でも良い
int メソッド名(@Param("パラメーター名") List型 変数名);

 

【使用例】

int bulkUpdate(@Param("listSchedule") List<TbSchedule> listSchedule);

 
 


■Mapper.xml

メソッド内容を定義する
 

【基本構文】

<update id="メソッド名" parameterType="パラメーターの型">
    update テーブル名
        set カラム1 = elt(キー,
        <foreach collection="Mapper.javaで設定したパラメーター名" item="変数" separator="区切り文字">
            #{変数.カラム1}
        </foreach>
        ),
        カラム2 = elt(キー,
        <foreach collection="Mapper.javaで設定したパラメーター名" item="変数" separator="区切り文字">
            #{変数.カラム2}
        </foreach>
        )
</update>

 

【使用例】

<!-- 
    <update></update>内にバルクアップデートの内容を記述
    <foreach></foreach>で繰り返し処理を記述
-->
<update id="bulkUpdate" parameterType="java.util.List">
    update tb_schedule
        set month_nbr = elt(id,
        <foreach collection="listSchedule" item="list" separator=",">
            #{list.monthNbr}
        </foreach>
        ),
        todo = elt(id,
        <foreach collection="listSchedule" item="list" separator=",">
            #{list.todo}
        </foreach>
        )
</update>

 
 

※一部のレコードをバルクアップデートする場合

【基本構文】

<!-- 
    <update></update>内にバルクアップデートの内容を記述
    <foreach></foreach>で繰り返し処理を記述
-->

<update id="メソッド名" parameterType="パラメーターの型">
    update テーブル名
        set カラム1 = elt(field(キー,
        <foreach collection="Mapper.javaで設定したパラメーター名" item="変数" separator="区切り文字">
            #{変数.キー}
        </foreach>
        ),
        <foreach collection="lMapper.javaで設定したパラメーター名" item="変数" separator="区切り文字">
            #{変数.カラム1}
        </foreach>
        ),
        カラム2 = elt(field(キー,
        <foreach collection="Mapper.javaで設定したパラメーター名" item="変数" separator="区切り文字">
            #{変数.キー}
        </foreach>
        ),
        <foreach collection="lMapper.javaで設定したパラメーター名" item="変数" separator="区切り文字">
            #{変数.カラム2}
        </foreach>
        )
        where id in (
        <foreach collection="Mapper.javaで設定したパラメーター名" item="変数" separator="区切り文字">
            #{変数.キー}
        </foreach>
        )
</update>

 

【使用例】

    <update id="bulkUpdate" parameterType="java.util.List">
        update tb_schedule
            set month_nbr = elt(field(id,
            <foreach collection="listSchedule" item="list" separator=",">
                #{list.id}
            </foreach>
            ),
            <foreach collection="listSchedule" item="list" separator=",">
                #{list.monthNbr}
            </foreach>
            ),
            todo = elt(field(id,
            <foreach collection="listSchedule" item="list" separator=",">
                #{list.id}
            </foreach>
            ),
            <foreach collection="listSchedule" item="list" separator=",">
                #{list.todo}
            </foreach>
            )
            where id in (
            <foreach collection="listSchedule" item="list" separator=",">
                #{list.id}
            </foreach>
            )
    </update>

 
 
上記バルクアップデートで使用している「ELT関数」と「FIELD関数」の解説はこちら
MySQL ELT関数とFIELD関数