mybatis批量更新:
修改数据库连接配置的url
对db.properties中的url进行修改,添加allowMultiQueries=true:
url=jdbc:mysql://localhost:3306/test?allowMultiQueries=true
mapper.xml中设置为:
<update id="updateAll" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update user_c
<set>
userName=#{item.userName}
</set>
<where>
userId=#{item.userId}
</where>
</foreach>
</update>
mybatis批量插入:
批量插入不需要修改数据库连接配置的url
直接在mapper.xml中添加:
<insert id="insertAll" parameterType="java.util.List">
insert into user_c
values
<foreach collection="list" item="item" index="index" separator="," >
(#{item.userId},#{item.userName},#{item.password})
</foreach>
修改数据库连接配置的url
对db.properties中的url进行修改,添加allowMultiQueries=true:
url=jdbc:mysql://localhost:3306/test?allowMultiQueries=true
mapper.xml中设置为:
<update id="updateAll" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update user_c
<set>
userName=#{item.userName}
</set>
<where>
userId=#{item.userId}
</where>
</foreach>
</update>
mybatis批量插入:
批量插入不需要修改数据库连接配置的url
直接在mapper.xml中添加:
<insert id="insertAll" parameterType="java.util.List">
insert into user_c
values
<foreach collection="list" item="item" index="index" separator="," >
(#{item.userId},#{item.userName},#{item.password})
</foreach>
</insert>
如果传递的List为空的话,那么所写的inset语句就会报错,提示的sql语句insert into user_c values ,sql语句就会停止在这里后面就不会有值传递进来,导致values后面没有值,所以在传递List的时候判断一下List是否为空