|
|
@ -13,6 +13,8 @@ import com.baomidou.mybatisplus.core.enums.SqlMethod; |
|
|
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
|
import com.baomidou.mybatisplus.core.metadata.OrderItem; |
|
|
|
import com.baomidou.mybatisplus.core.metadata.TableInfo; |
|
|
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; |
|
|
|
import com.baomidou.mybatisplus.core.toolkit.*; |
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
|
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper; |
|
|
@ -31,6 +33,7 @@ import java.io.Serializable; |
|
|
|
import java.util.Collection; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
/** |
|
|
|
* 基础服务类,所有Service都要继承 |
|
|
@ -298,4 +301,56 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T> implements Bas |
|
|
|
public boolean deleteBatchIds(Collection<? extends Serializable> idList) { |
|
|
|
return SqlHelper.delBool(baseDao.deleteBatchIds(idList)); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
@Override |
|
|
|
public boolean saveOrUpdate(T entity) { |
|
|
|
if (null != entity) { |
|
|
|
Class<?> cls = entity.getClass(); |
|
|
|
TableInfo tableInfo = TableInfoHelper.getTableInfo(cls); |
|
|
|
Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!"); |
|
|
|
String keyProperty = tableInfo.getKeyProperty(); |
|
|
|
Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!"); |
|
|
|
Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty()); |
|
|
|
return StringUtils.checkValNull(idVal) || Objects.isNull(selectById((Serializable) idVal)) ? insert(entity) : updateById(entity); |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
@Override |
|
|
|
public boolean saveOrUpdateBatch(Collection<T> entityList) { |
|
|
|
return saveOrUpdateBatch(entityList, 100); |
|
|
|
} |
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
@Override |
|
|
|
public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) { |
|
|
|
Assert.notEmpty(entityList, "error: entityList must not be empty"); |
|
|
|
Class<?> cls = currentModelClass(); |
|
|
|
TableInfo tableInfo = TableInfoHelper.getTableInfo(cls); |
|
|
|
Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!"); |
|
|
|
String keyProperty = tableInfo.getKeyProperty(); |
|
|
|
Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!"); |
|
|
|
try (SqlSession batchSqlSession = sqlSessionBatch()) { |
|
|
|
int i = 0; |
|
|
|
for (T entity : entityList) { |
|
|
|
Object idVal = ReflectionKit.getMethodValue(cls, entity, keyProperty); |
|
|
|
if (StringUtils.checkValNull(idVal) || Objects.isNull(selectById((Serializable) idVal))) { |
|
|
|
batchSqlSession.insert(sqlStatement(SqlMethod.INSERT_ONE), entity); |
|
|
|
} else { |
|
|
|
MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>(); |
|
|
|
param.put(Constants.ENTITY, entity); |
|
|
|
batchSqlSession.update(sqlStatement(SqlMethod.UPDATE_BY_ID), param); |
|
|
|
} |
|
|
|
// 不知道以后会不会有人说更新失败了还要执行插入 😂😂😂
|
|
|
|
if (i >= 1 && i % batchSize == 0) { |
|
|
|
batchSqlSession.flushStatements(); |
|
|
|
} |
|
|
|
i++; |
|
|
|
} |
|
|
|
batchSqlSession.flushStatements(); |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|