BaseMapper新增saveOrUpdate方法.
This commit is contained in:
parent
4b9a42bc8a
commit
44009841fb
@ -25,6 +25,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
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;
|
||||
@ -48,6 +49,7 @@ import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiPredicate;
|
||||
|
||||
/*
|
||||
@ -436,6 +438,23 @@ public interface BaseMapper<T> extends Mapper<T> {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 插入(批量)
|
||||
*
|
||||
|
@ -36,15 +36,12 @@ import org.mybatis.spring.SqlSessionUtils;
|
||||
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;
|
||||
@ -59,8 +56,6 @@ import java.util.function.Function;
|
||||
@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
|
||||
@ -197,15 +192,7 @@ public abstract class ServiceImpl<M extends BaseMapper<T>, T> implements IServic
|
||||
*/
|
||||
@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)
|
||||
@ -295,7 +282,7 @@ public abstract class ServiceImpl<M extends BaseMapper<T>, T> implements IServic
|
||||
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));
|
||||
|
@ -551,4 +551,15 @@ class H2UserMapperTest extends BaseTest {
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user