
中如何根据字段在列表中的情况执行更新操作
在进行批量更新时,需要根据待更新数据的字段列表逐个字段进行判断更新,此时的判断条件就是该字段是否在这个列表中。sql语句中,可以通过if语句来实现这种判断。
如果使用java代码,dao层方法可以定义为:
int batchupdatebyid(
@param("entitylist") list<schooldo> entitylist,
@param("fieldnameslist") list<string> fieldnameslist);
登录后复制
相应的xml文件可以编写为:
<!-- 批量插入 -->
<update id="batchUpdateById">
<foreach collection="entityList" item="entity" index="index1" open="(" close=")" separator=";">
UPDATE school_info SET
<if test='fieldNamesList.contains("schoolNo")'>
schoolNo = #{entity.getSchoolNo}
</if>
<if test='fieldNamesList.contains("schoolRank")'>
schoolRank = #{entity.getSchoolRank}
</if>
where dataId = #{entity.dataId}
</foreach>
</update>
登录后复制
在这个xml文件中,fieldnameslist是包含待更新字段的列表。通过if语句,执行判断条件fieldnameslist.contns(“字段名”),如果为真,则更新该字段。
以上就是SQL语句中如何根据字段在列表中执行更新操作?的详细内容,更多请关注米云网其它相关文章!
