我不清楚您要实现什么,插入和更新是两个不同的操作,对于插入具有批量功能是有意义的,对于更新它在我看来没有,实际上在 SQL 中您可以写这样的东西
UPDATE
SomeTable
SET SomeColumn = SomeValue
WHERE AnotherColumn = AnotherValue
这意味着将 AnotherColumn 等于 AnotherValue 的所有行更新为 SomeValue 值 SomeColumn。
在Slick 中,这是一个简单的filter 结合map 和update
table
.filter(_.someCulomn === someValue)
.map(_.FieldToUpdate)
.update(NewValue)
如果您想更新整行,只需删除 map 并将 Row 对象传递给 update 函数即可。
编辑:
如果您想更新不同的案例类,我会认为这些案例类是您的架构中定义的行,如果是这种情况,您可以将它们直接传递给更新函数,因为它是这样定义的:
def update(value: T)(implicit session: Backend#Session): Int
对于第二个问题,我无法为您提供解决方案,查看JdbcInvokerComponent trait 看起来更新函数会立即调用 execute 方法
def update(value: T)(implicit session: Backend#Session): Int = session.withPreparedStatement(updateStatement) { st =>
st.clearParameters
val pp = new PositionedParameters(st)
converter.set(value, pp, true)
sres.setter(pp, param)
st.executeUpdate
}
可能是因为您实际上可以在每个表的时间运行一个更新查询,而不是像 SO question 中所述的那样对多个表进行多次更新,但您当然可以更新同一个表上的多行。