Mybatis批量更新Oracle和Mysql


一、Mybatis批量更新Oracle

Mybatis写Oracle的批量更新要加begin end匿名存储过程,否则程序会报无效字符异常。

1、mapper

void updateTable_oracle(@Param("list") List<Map<String,String>> list);

2、xml

<update id="updateTable_patch">
        <foreach collection="list" item="item" index="index" separator=";" open="begin" close=";end;">
            update STUDENT t set t.student_name='${item.student_name}'
            <where>
            	<if test="item.age!=null and item.age!='">
            		and t.age='$item.age}'
             	</if>
				<if test="item.address!= null and item.address!="">
					and t.address='${item.address)'
             	</if>
                <if test="item.time!=null and item.time!=''">
                    and t.time=to_date('${item.time}','yyyy-mm-dd hh24:mi:ss')
             	</if>
            </where>
        </foreach>
    </update>

二、Mybatis批量更新Mysql

1、mapper

void updateTable_mysql(@Param("list") List<Map<String,String>> list);

2、xml

<update id="updateTable_patch">
        <foreach collection="list" item="item" index="index" separator=";">
            update STUDENT t set t.student_name='${item.student_name}'
            <where>
            	<if test="item.age!=null and item.age!='">
            		and t.age='$item.age}'
             	</if>
				<if test="item.address!= null and item.address!="">
					and t.address='${item.address)'
             	</if>
                <if test="item.time!=null and item.time!=''">
                    and t.time=to_date('${item.time}','yyyy-mm-dd hh24:mi:ss')
             	</if>
            </where>
        </foreach>
    </update>

版权声明:本文为weixin_46146718原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。