前言:聊聊今天在开发中关于MySql遇到的问题。
问题所在:之前写好的批量更新操作,明明可以执行,突然更新不了。
找了半天,还被前辈教育了,才发现问题所在。是因为前辈把查询的id给换掉了,导致我进行批量插入的时候找不到唯一的索引。
之前的xml
注意where那个条件,sql的大致意思是,根据id和storeid为条件进行批量修改
<update id="updatePrintTemplate">
<foreach collection="printTemplateList" item="printTemplate"
separator=";">
update `print_template`
<set>
`storeId`=#{printTemplate.storeId},
`templateKey`=#{printTemplate.templateKey},
`templateName`=#{printTemplate.templateName},
`printNum`=#{printTemplate.printNum},
`detailsStruct`=#{printTemplate.detailsStruct},
showDetailsStruct=#{printTemplate.showDetailsStruct},
detailsSort=#{printTemplate.detailsSort},
showDetailsSort=#{printTemplate.showDetailsSort},
detailsSplit=#{printTemplate.detailsSplit},
showDetailsSplit=#{printTemplate.showDetailsSplit},
oneDetailsOneOrder=#{printTemplate.oneDetailsOneOrder},
showOneDetailsOneOrder=#{printTemplate.showOneDetailsOneOrder},
stateFlag=#{printTemplate.stateFlag},
createTime=#{printTemplate.createTime},
frontBackFlag=#{printTemplate.frontBackFlag}
</set>
where id = #{printTemplate.id} AND `storeId`=#{printTemplate.storeId}
</foreach>
</update>
然而前端传回来的id是1-24,根本不是之前传的那个id值,所以导致我不能进行批量的插入操作。
总而言之就是条件不唯一了,导致我更新失败。
于是乎,我不得不换一个唯一索引进行判断,总不能让前辈为我改值吧,所以我修改了where中的操作条件。
这样就有了唯一的索引,供我进行批量插入的依据。