在場(chǎng)景中,需要進(jìn)行批量更新,其中變量 fieldnameslist 儲(chǔ)存了需要更新的字段名。為了動(dòng)態(tài)更新特定字段,需要使用 sql 中的 if test 語句來判斷字段是否在列表中。
修改后的 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>
該代碼使用 if test 語句判斷 fieldnameslist 是否包含特定的字段名,如果是,則更新該字段為實(shí)體對(duì)象中相應(yīng)的值。這樣就可以根據(jù) fieldnameslist 動(dòng)態(tài)更新指定的字段了。
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載。
THE END