Skip to content

Commit

Permalink
BaseMapper新增saveOrUpdate方法.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Apr 10, 2024
1 parent 4b9a42b commit 4400984
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.override.MybatisMapperProxy;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.MybatisBatchUtils;
Expand All @@ -48,6 +49,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiPredicate;

/*
Expand Down Expand Up @@ -436,6 +438,23 @@ default <P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param(C
return page;
}

/**
* 主键存在更新记录,否插入一条记录
*
* @param entity 实体对象 (不能为空)
* @since 3.5.7
*/
default boolean saveOrUpdate(T entity) {
Class<?> entityClass = GenericTypeUtils.resolveTypeArguments(getClass(), BaseMapper.class)[0];
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
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 = tableInfo.getPropertyValue(entity, tableInfo.getKeyProperty());
return StringUtils.checkValNull(idVal) || Objects.isNull(selectById((Serializable) idVal)) ? insert(entity) > 0 : updateById(entity) > 0;
}


/**
* 插入(批量)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.lang.reflect.Proxy;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
Expand All @@ -59,8 +56,6 @@
@SuppressWarnings("unchecked")
public abstract class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {

private final ConversionService conversionService = DefaultConversionService.getSharedInstance();

protected final Log log = LogFactory.getLog(getClass());

@Autowired
Expand Down Expand Up @@ -197,15 +192,7 @@ protected String getSqlStatement(SqlMethod sqlMethod) {
*/
@Override
public boolean saveOrUpdate(T entity) {
if (null != entity) {
TableInfo tableInfo = TableInfoHelper.getTableInfo(this.entityClass);
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 = tableInfo.getPropertyValue(entity, tableInfo.getKeyProperty());
return StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal)) ? save(entity) : updateById(entity);
}
return false;
return getBaseMapper().saveOrUpdate(entity);
}

@Transactional(rollbackFor = Exception.class)
Expand Down Expand Up @@ -295,7 +282,7 @@ protected <E> boolean executeBatch(Collection<E> list, int batchSize, BiConsumer
protected <E> boolean executeBatch(Collection<E> list, BiConsumer<SqlSession, E> consumer) {
return executeBatch(list, DEFAULT_BATCH_SIZE, consumer);
}

@Override
public boolean removeById(Serializable id, boolean useFill) {
return SqlHelper.retBool(getBaseMapper().deleteById(id, useFill));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,4 +551,15 @@ void testUpdateByWrapper() {
Assertions.assertEquals(userMapper.selectById(h2User.getTestId()).getName(), "testUpdateByWrapper");
}

@Test
void testSaveOrUpdate() {
var h2User = new H2User();
userMapper.saveOrUpdate(h2User);
Assertions.assertNotNull(h2User.getTestId());
Assertions.assertNull(h2User.getLastUpdatedDt());
h2User.setName("test");
userMapper.saveOrUpdate(h2User);
Assertions.assertNotNull(h2User.getLastUpdatedDt());
}

}

0 comments on commit 4400984

Please sign in to comment.