mapper:
<update id="updateSurplusAmountByPrimaryKeyAndMaterialTypeId"
parameterType="java.util.List">
update db_logistics.table_inventory_material
set surplusAmount=
<foreach collection="list" item="item" index="index"
separator=" " open="case" close="end">
when inventoryId=#{item.inventoryId} and materialTypeId=#
{item.materialTypeId} then #{item.surplusAmount,jdbcType=INTEGER}
</foreach>
where inventoryId in
<foreach collection="list" index="index" item="item"
separator="," open="(" close=")">
#{item.inventoryId,jdbcType=BIGINT}
</foreach>
</update>
fun updateSurplusAmountByPrimaryKeyAndMaterialTypeId(records:
List<InventoryMaterial>): Int
data class InventoryMaterial(
var inventoryId: Int = 0,
var materialTypeId: Int = 0,
var surplusAmount: Int = 0,
var consumeSpeed: Float = 0f,
var consumeAlarmDayCount: Int = 0,
var updateDataTime: LocalDateTime = LocalDateTime.now())
错误
“引起:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列'surplusAmount'不能为null”
当我将when inventoryId=#{item.inventoryId} and materialTypeId=#{item.materialTypeId}
修改为when inventoryId=#{item.inventoryId}
时,错误消失了。但我需要拖车参数inventoryId和materialTypeId可以决定inventoryId。有人可以给我一些答案吗?
执行的查询通过table_inventory_material
从inventoryId
中选择记录,并使用每个materialTypeId
的指定数据更新它们。
问题发生的原因是,对于您在列表中传递的某些inventoryId
,数据库具有比您作为参数传递的列表中提供的更多materialTypeId
s。
所以生成的case语句为缺少的NULL
产生materialTypeId
,并且尝试将surplusAmount
设置为NULL
导致错误。