mybatis+批量插入去重
时间: 2023-11-27 11:46:52 浏览: 146
MyBatis批量插入(insert)数据操作
5星 · 资源好评率100%
以下是使用MyBatis实现批量插入去重的步骤:
1.在Mapper.xml文件中定义插入语句,使用INSERT IGNORE INTO语句插入数据,忽略重复数据的插入。
```xml
<insert id="batchInsert" parameterType="java.util.List">
INSERT IGNORE INTO table_name (column1, column2, ...) VALUES
<foreach collection="list" item="item" separator=",">
(#{item.column1}, #{item.column2}, ...)
</foreach>
</insert>
```
2.在Service层中,将数据按照一定的批次进行分割,然后调用Mapper中的批量插入方法。
```java
@Service
public class MyService {
@Autowired
private MyMapper myMapper;
public void batchInsert(List<MyEntity> list) {
int batchSize = 1000;
int totalSize = list.size();
int batchCount = (totalSize + batchSize - 1) / batchSize;
for (int i = 0; i < batchCount; i++) {
int fromIndex = i * batchSize;
int toIndex = Math.min((i + 1) * batchSize, totalSize);
List<MyEntity> subList = list.subList(fromIndex, toIndex);
myMapper.batchInsert(subList);
}
}
}
```
3.在Controller层中,接收请求并调用Service层的批量插入方法。
```java
@RestController
public class MyController {
@Autowired
private MyService myService;
@PostMapping("/batchInsert")
public void batchInsert(@RequestBody List<MyEntity> list) {
myService.batchInsert(list);
}
}
```
阅读全文