mybatis-plus如何根據(jù)任意字段saveOrUpdateBatch
使用場景
mybatisplus Iservice接口下的saveOrUpdateBatch方法默認是根據(jù)主鍵來決定是要更新還是插入的,
如果要根據(jù)其他字段(必須是唯一約束,唯一約束字段可以是多個)更新的話,則需要在項目的service層重寫該方法。
方法源碼
@Transactional(
rollbackFor = {Exception.class}
)
public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!", new Object[0]);
String keyProperty = tableInfo.getKeyProperty();
Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!", new Object[0]);
return SqlHelper.saveOrUpdateBatch(this.entityClass, this.mapperClass, this.log, entityList, batchSize, (sqlSession, entity) -> {
Object idVal = ReflectionKit.getFieldValue(entity, keyProperty);
return StringUtils.checkValNull(idVal) || CollectionUtils.isEmpty(sqlSession.selectList(this.getSqlStatement(SqlMethod.SELECT_BY_ID), entity));
}, (sqlSession, entity) -> {
MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap();
param.put("et", entity);
sqlSession.update(this.getSqlStatement(SqlMethod.UPDATE_BY_ID), param);
});
}
從源碼中可以看出實現(xiàn)saveOrUpdateBatch的主要方法就是SqlHelper.saveOrUpdateBatch
public static <E> boolean saveOrUpdateBatch(Class<?> entityClass, Class<?> mapper, Log log, Collection<E> list, int batchSize, BiPredicate<SqlSession, E> predicate, BiConsumer<SqlSession, E> consumer) {
String sqlStatement = getSqlStatement(mapper, SqlMethod.INSERT_ONE);
return executeBatch(entityClass, log, list, batchSize, (sqlSession, entity) -> {
if (predicate.test(sqlSession, entity)) {
sqlSession.insert(sqlStatement, entity);
} else {
consumer.accept(sqlSession, entity);
}
});
}
該方法的最后兩個參數(shù)predicate,consumer
predicate 這個函數(shù)是用于判斷是否要進行插入操作 true插入,false:則通過consumer 函數(shù)執(zhí)行更新
方法改造
注意:寫在項目操作對應(yīng)表的service層
首先在service層定義接口
boolean saveOrUpdateBatchByAgentIdAndPeriodAndType(List<Entity> list);
類為數(shù)據(jù)庫表對應(yīng)的實體類,agentId,period,type,這個三個字段為表的唯一約束,即當表中存在這三個字段組合對應(yīng)的記錄時則進行更新操作,不存在則進行插入操作
service層接口實現(xiàn)
@Transactional(rollbackFor = Exception.class)
@DS("XXXX")//如果為多數(shù)據(jù)源,這里要指明具體操作的數(shù)據(jù)源名稱
public boolean saveOrUpdateBatchByAgentIdAndPeriodAndType(List<Entity> list) {
return SqlHelper.saveOrUpdateBatch(entityClass, this.mapperClass, super.log, list, DEFAULT_BATCH_SIZE, (sqlSession, entity) -> {//這里主要是查詢唯一約束對應(yīng)的記錄是否存在
LambdaQueryWrapper<Entity> queryWrapper = Wrappers.<Entity>lambdaQuery()
.eq(Entity::getAgentId, entity.getAgentId()).eq(Entity::getPeriod,entity.getPeriod())
.eq(Entity::getType,entity.getType());
Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(1);
map.put(Constants.WRAPPER, queryWrapper);
return CollectionUtils.isEmpty(sqlSession.selectList(getSqlStatement(SqlMethod.SELECT_LIST), map));
}, (sqlSession, entity) -> {
LambdaUpdateWrapper<Entity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.eq(Entity::getAgentId, entity.getAgentId()).eq(Entity::getPeriod,entity.getPeriod())
.eq(Entity::getType,entity.getType());
Map<String, Object> param = CollectionUtils.newHashMapWithExpectedSize(2);
param.put(Constants.ENTITY, entity);
param.put(Constants.WRAPPER, lambdaUpdateWrapper);
sqlSession.update(getSqlStatement(SqlMethod.UPDATE), param);
});
}
非批量的saveOrUpdate也可以按照這種方式進行改造
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中把一個文件夾下的所有文件復(fù)制到另一個文件夾的完整實現(xiàn)方案
這篇文章主要介紹了如何使用Java原生API實現(xiàn)文件夾復(fù)制,支持多級目錄、空文件夾和文件覆蓋等場景,并提供了基于File和FileChannel的實現(xiàn)方案,此外,還介紹了使用Java 7的Files工具類進行簡化實現(xiàn),需要的朋友可以參考下2026-01-01
Spring裝配Bean之用Java代碼安裝配置bean詳解
這篇文章主要給大家介紹了關(guān)于Spring裝配Bean之用Java代碼安裝配置bean的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用spring具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
基于javamelody監(jiān)控springboot項目過程詳解
這篇文章主要介紹了基于javamelody監(jiān)控springboot項目過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
SpringMVC實現(xiàn)全局異常處理器的經(jīng)典案例
文章介紹了如何使用@ControllerAdvice和相關(guān)注解實現(xiàn)SpringMVC的全局異常處理,通過統(tǒng)一的異常處理類和自定義業(yè)務(wù)異常類,可以將所有控制器的異常集中處理,并以JSON格式返回給前端,感興趣的朋友一起看看吧2025-03-03
SpringBoot + 微信公眾號JSAPI支付功能的實現(xiàn)
這篇文章主要介紹了SpringBoot + 微信公眾號JSAPI支付功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
Spring Boot 訪問安全之認證和鑒權(quán)詳解
這篇文章主要介紹了Spring Boot 訪問安全之認證和鑒權(quán),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11

