MyBatis批量更新数据

数据库: MySQL5.6
MyBitis:3.2.8
MyBatis批量更新的方法:
*dao.java中的方法为:

void updateQueueHasnNull(@Param("urlQueue")List<Map<String, String>> urlQueue);

*mapper.xml

<!--  更新URL的hash状态值 -->
<update id="updateQueueHasnNull" parameterType="java.util.List">
    <foreach collection="urlQueue" item="map" index="index" separator=";" open="" close="">
        update spider_url_queue 
        set hash = #{map.hash, jdbcType=VARCHAR} 
        where id = #{map.id, jdbcType=VARCHAR} 
    </foreach>
</update>

对xml中的foreach参数说明:
foreach进行list集合遍历
collection:是传入的集合参数名称
item:集合中每一个元素进行迭代时的别名
index:用于表示在迭代过程中,每次迭代到的位置
open:该语句以什么开始
close:以什么结束
separator:在每次进行迭代之间以什么符号作为分隔符

在更新过程中,可能会出现多个更新语句执行时错误,解决方法:
在MyBatis连接Mysql数据库时,在url中设置:allowMultiQueries=true
例如:

jdbc:mysql://127.0.0.1:3306/spider?allowMultiQueries=true

猜你喜欢

转载自blog.csdn.net/qq_26710805/article/details/79973305