在 MyBatis-Plus 中,可以通过使用 @Param 注解和 List 集合来实现批量插入。

首先,在你的 Mapper 接口中定义一个批量插入方法,如下所示:

@Insert("<script>INSERT INTO your_table (column1, column2) VALUES " +
        "<foreach collection='list' item='item' separator=','> " +
        "(#{item.column1}, #{item.column2})" +
        "</foreach></script>")
int batchInsert(@Param("list") List<YourEntity> list);

然后,在你的 Service 中调用该方法来实现批量插入:

List<YourEntity> list = new ArrayList<>();
// 添加要插入的数据到 list 中

int result = yourMapper.batchInsert(list);

这样就可以实现批量插入了。请确保你的实体类和表结构对应,并且在调用批量插入方法之前,已经将要插入的数据添加到了 list

mybatisplus postgresql 开启批处理模式BATCH 批量插入

原文地址: https://www.cveoy.top/t/topic/iBb2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录