MyBatis 批量更新 MySQL 表数据,使用 foreach 语句提高效率
使用 MyBatis 进行批量更新操作可以通过编写 Mapper.xml 文件来实现。
首先,创建一个 Mapper 接口,包含批量更新方法的定义:
public interface YourMapper {
void batchUpdate(List<YourEntity> entities);
}
然后,在 Mapper.xml 文件中编写对应的 SQL 语句:
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="entity" separator=";">
UPDATE your_table
SET o = #{entity.o},
h = #{entity.h},
l = #{entity.l},
c = #{entity.c},
vol = #{entity.vol},
confirm = #{entity.confirm},
k5 = #{entity.k5},
k10 = #{entity.k10},
k20 = #{entity.k20},
k40 = #{entity.k40}
WHERE instid = #{entity.instid} AND ts = #{entity.ts}
</foreach>
</update>
其中,your_table 为要更新的表名,YourEntity 为实体类,包含 instid、ts、o、h、l、c、vol、confirm、k5、k10、k20、k40 等属性。
最后,在对应的 Mapper 接口中添加方法的实现:
@Mapper
public interface YourMapper {
void batchUpdate(List<YourEntity> entities);
}
这样就可以使用 MyBatis 进行批量更新操作了。在调用 batchUpdate 方法时,传入包含多个实体对象的 List,即可批量更新数据库中的数据。
原文地址: https://www.cveoy.top/t/topic/qoh6 著作权归作者所有。请勿转载和采集!