spring boot+mybatis plus環境,單條插入用的是BaseMapper自帶的insert方法
1
2
3
4
5
6
7
8
|
public ApiResult addAnc(Anc anc) { ApiResult result = new ApiResult(); Integer insert = ancMapper.insert(anc); if (insert < 1 ) { return result.failed( "發布失敗,請聯系管理員" ); } return result.success(anc); |
BaseMapper未提供批量插入接口,但是在com.baomidou.mybatisplus.service.IService
中提供了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/** * <p> * 插入(批量),該方法不適合 Oracle * </p> * * @param entityList 實體對象列表 * @return boolean */ boolean insertBatch(List<T> entityList); /** * <p> * 插入(批量) * </p> * * @param entityList 實體對象列表 * @param batchSize 插入批次數量 * @return boolean */ boolean insertBatch(List<T> entityList, int batchSize); |
使用方法,定義一個自己的接口,繼承IService
,泛型為被操作實體類
1
2
3
4
|
@Service public interface WorkIService extends IService<CmpWork> { } |
定義一個實現類,實現上訴接口
1
2
3
|
@Service public class WorkIServiceImpl extends ServiceImpl<WorkMapper, CmpWork> implements WorkIService{ } |
其中WorkMapper
為正常操作的mapper
在業務中測試批量插入操作
1
2
3
4
5
6
7
8
9
|
List<CmpWork> entityList = new ArrayList<>( 1000 ); for ( int i= 1 ;i< 10000 ;i++){ CmpWork work = new CmpWork(); work.setWorkName( "workNametestBatch" +i); work.setWorkID( "testBatch" +i); work.setCreTm(DateUtil.dateToYMDHMS( new Date())); entityList.add(work); } boolean b = workIService.insertBatch(entityList); |
和單條插入的執行對比了一下,在1000條數據級別內,差別不大,批量操作的優勢可能大數據環境下才能顯現吧
到此這篇關于MyBatis-Plus 批量插入的文章就介紹到這了,更多相關MyBatis-Plus 批量插入內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/leisure_life/article/details/98976565