mybatis-plus根據(jù)指定字段批量,刪除、修改實現(xiàn)過程
更新時間:2026年04月23日 10:01:30 作者:單人影i
本文介紹了三種MyBatis-Plus進(jìn)行批量刪除和修改的方法,第一種是避免手寫SQL,第二種是手動獲取SqlSessionTemplate,第三種是重寫executeBatch方法,雖然批量處理效率較低,但對于不追求性能且不想手寫SQL的場景,可以嘗試這種方法
mybatis-plus根據(jù)指定字段批量刪除修改
方案一
手寫SQL
- 這個就不說了,就是因為不想手寫SQL 所以才有這篇博客
方案二
手動獲取SqlSessionTemplate 就是把mybatis plus 干的事自己干了
// 這種方法就是把mybatis的活在干一遍,還是一條一條處理的.只是共用一個session連接
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
// 新獲取一個模式為BATCH,自動提交為false的session
SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH,false);
static final BATCH_SIZE = 1000;
// XxxMapper 為 對應(yīng)的mapper文件
XxxMapper xxMapper = session.getMapper(XxxMapper.class);
int size = updateList.size();
try {
for(int i=0; i < size; i++) {
// updateByXxx 寫好的單條數(shù)據(jù)的方法
xxMapper.updateByXxx(updateList.get(i));
if(i % BATCH_SIZE == 0 || i == size-1){
//手動每1000個一提交,提交后無法回滾
session.commit();
//清理緩存,防止溢出
session.clearCache();
}
}
}catch (Exception e) {
session.rollback();
} finally {
session.close();
}
方案三
重寫 executeBatch 方法
// mybatis plus 源碼
@Transactional(rollbackFor = Exception.class)
@Override
public boolean updateBatchById(Collection<T> entityList, int batchSize) {
String sqlStatement = getSqlStatement(SqlMethod.UPDATE_BY_ID);
return executeBatch(entityList, batchSize, (sqlSession, entity) -> {
MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
param.put(Constants.ENTITY, entity);
sqlSession.update(sqlStatement, param);
});
}
mybatis plus 的 executeBatch

參考 mybatis plus 的updateBatchById 方法.
- 調(diào)用處:
//刪除方法 deleteList 是要刪除的主鍵list
List<String> deleteList = new ArrayList<>();
dao.batchDelete(deleteList, delete -> new QueryWrapper<String>().eq("xx", delete));
// 修改方法 OBJ 代碼表對象
List<OBJ> updateList = new ArrayList<>();
dao.batchUpdate(updateList, update -> new LambdaQueryWrapper<OBJ>()
.eq(OBJ::getProductId, update.getProductId()));
- 接口:
boolean batchUpdate(List<OBJ> updateList, Function<OBJ, LambdaQueryWrapper> queryWrapperFunction);
boolean batchDelete(List<String> deleteList, Function<String, QueryWrapper> queryWrapperFunction);
- 重寫方法 實現(xiàn):
@Override
public boolean batchUpdate(List<OBJ> entityList, Function<OBJ, LambdaQueryWrapper> function) {
return this.executeBatch(entityList, DEFAULT_BATCH_SIZE, (sqlSession, entity) -> {
ParamMap param = new ParamMap();
param.put(Constants.ENTITY, entity);
param.put(Constants.WRAPPER, function.apply(entity));
sqlSession.update(this.getSqlStatement(SqlMethod.UPDATE), param);
});
}
@Override
public boolean batchDelete(List<String> deleteList, Function<String, QueryWrapper> function) {
return this.executeBatch(deleteList, DEFAULT_BATCH_SIZE, (sqlSession, entity) -> {
ParamMap param = new ParamMap();
param.put(Constants.ENTITY, entity);
param.put(Constants.WRAPPER, function.apply(entity));
sqlSession.delete(this.getSqlStatement(SqlMethod.DELETE), param);
});
}
// 也可以傳入一個LambdaUpdateWrapper 來制定更新
@Override
public boolean batchUpdate2(List<ContProductAbstractLocalDO> entityList, Function<ContProductAbstractLocalDO, LambdaUpdateWrapper> function) {
return this.executeBatch(entityList, DEFAULT_BATCH_SIZE, (sqlSession, entity) -> {
ParamMap param = new ParamMap();
param.put(Constants.ENTITY, null);
param.put(Constants.WRAPPER, function.apply(entity));
sqlSession.update(this.getSqlStatement(SqlMethod.UPDATE), param);
});
}
示例 : 指定執(zhí)行的SQL 和字段
private LambdaUpdateWrapper<OBJ> getUpdateWrapper(OBJupdate) {
retur new LambdaUpdateWrapper<OBJ>()
.eq(OBJ::getxx, update.xx())
.set(OBJ::getxxx, update.xxxx());
總結(jié)
這種寫法其實批量的效率還是比較慢的,如果對性能沒有要求,并且還不想手寫SQL的,可以試一試。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot配置Redis實現(xiàn)保存獲取和刪除數(shù)據(jù)
本文主要介紹了SpringBoot配置Redis實現(xiàn)保存獲取和刪除數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,感興趣的小伙伴們可以參考一下2021-06-06
mybatis-plus QueryWrapper中or,and的使用及說明
使用MyBatis Plus QueryWrapper時,因同時添加角色權(quán)限固定條件和多字段模糊查詢導(dǎo)致數(shù)據(jù)異常展示,排查發(fā)現(xiàn)OR邏輯覆蓋了權(quán)限條件,通過正確使用and()方法,將條件組合為AND邏輯,解決了權(quán)限過濾失效的問題2025-07-07
對象存儲服務(wù)MinIO快速入門(集成項目的詳細(xì)過程)
MinIO是一個開源的對象存儲服務(wù),支持多種操作系統(tǒng),配置簡單且性能高,它使用糾刪碼進(jìn)行數(shù)據(jù)保護(hù),可以容忍硬件故障,MinIO支持多種語言的SDK和豐富的API,本文介紹對象存儲服務(wù)MinIO快速入門,感興趣的朋友一起看看吧2025-03-03

