代码拉取完成,页面将自动刷新
本次项目主要是为了进行 mybatis的批量更新 数据实体的验证和探究
本次采用的是 springboot + mybatis + mysql技术
方案一:采用mysql连接允许多条sql执行,将update语句分次执行,使用的是mybatis的foreach语法遍历执行,语句如下:
<!-- 批量更新用户信息 方式一 -->
<update id="updateUserItem" parameterType="java.util.List">
<foreach collection="list" item="item" separator=";">
update sys_user
<set>
<if test="item.username != null and item.username.trim() != ''">
`username` = #{item.username},
</if>
<if test="item.password != null and item.password.trim() != ''">
`password` = #{item.password},
</if>
<if test="item.realName != null and item.realName.trim() != ''">
`real_name` = #{item.realName},
</if>
<if test="item.telephone != null and item.telephone.trim() != ''">
`telephone` = #{item.telephone},
</if>
<if test="item.mail != null and item.mail.trim() != ''">
`mail` = #{item.mail}
</if>
</set>
where `id` = #{item.id}
</foreach>
</update>
方案一:采取case when语法,使用的是mybatis的 trim语法 + foreach语法 执行,语句如下:
<!-- 批量更新用户信息 方式二 -->
<update id="updateUserItem2" parameterType="java.util.List">
update sys_user
<trim prefix="set" suffixOverrides=",">
<trim prefix="username = case" suffix="end,">
<foreach collection="list" item="item">
<if test="item.username != null and item.username.trim() != ''">
when id=#{item.id} then #{item.username}
</if>
</foreach>
</trim>
<trim prefix="password = case" suffix="end,">
<foreach collection="list" item="item">
<if test="item.password != null and item.password.trim() != ''">
when id=#{item.id} then #{item.password}
</if>
</foreach>
</trim>
<trim prefix="real_name = case" suffix="end,">
<foreach collection="list" item="item">
<if test="item.realName != null and item.realName.trim() != ''">
when id=#{item.id} then #{item.realName}
</if>
</foreach>
</trim>
<trim prefix="telephone = case" suffix="end,">
<foreach collection="list" item="item">
<if test="item.telephone != null and item.telephone.trim() != ''">
when id=#{item.id} then #{item.telephone}
</if>
</foreach>
</trim>
<trim prefix="mail = case" suffix="end,">
<foreach collection="list" item="item">
<if test="item.mail != null and item.mail.trim() != ''">
when id=#{item.id} then #{item.mail}
</if>
</foreach>
</trim>
</trim>
<where>
<foreach collection="list" item="item" open="( " separator=") or (" close=" )">
id = #{item.id}
</foreach>
</where>
</update>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。