Skip to content

Commit

Permalink
BaseMapper逻辑删除支持填充.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Apr 10, 2024
1 parent 68a5042 commit be2f0c3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
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.Constants;
import com.baomidou.mybatisplus.core.toolkit.MybatisBatchUtils;
import com.baomidou.mybatisplus.core.toolkit.MybatisUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.reflect.GenericTypeUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.ognl.OgnlOps;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.Serializable;
Expand Down Expand Up @@ -106,7 +112,31 @@ public interface BaseMapper<T> extends Mapper<T> {
*
* @param id 主键ID
*/
int deleteById(Serializable id);
default int deleteById(Serializable id) {
return deleteById(id, true);
}

/**
* 根据 ID 删除
*
* @param useFill 是否填充
* @param id 主键ID
* @since 3.5.7
*/
default int deleteById(Serializable id, boolean useFill) {
if (useFill) {
Class<?> entityClass = GenericTypeUtils.resolveTypeArguments(getClass(), BaseMapper.class)[0];
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
if (tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill()) {
T instance = tableInfo.newInstance();
tableInfo.setPropertyValue(instance, tableInfo.getKeyProperty(), OgnlOps.convertValue(id, tableInfo.getKeyType()));
return this.deleteById(instance);
}
}
MybatisMapperProxy<?> mybatisMapperProxy = (MybatisMapperProxy<?>) Proxy.getInvocationHandler(this);
SqlSession sqlSession = mybatisMapperProxy.getSqlSession();
return sqlSession.delete(mybatisMapperProxy.getMapperInterface().getName() + Constants.DOT + SqlMethod.DELETE_BY_ID.getMethod(), id);
}

/**
* 根据实体(ID)删除
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;
import java.util.Optional;

import com.baomidou.mybatisplus.test.h2.mapper.H2UserMapper;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.ibatis.plugin.Interceptor;
Expand Down Expand Up @@ -631,6 +632,24 @@ void testRemove() {
userService.removeById(h2User);
userService.removeByIds(Arrays.asList(10000L, h2User));
userService.removeByIds(Arrays.asList(10000L, h2User), false);
h2User = new H2User("test");
H2UserMapper h2UserMapper = (H2UserMapper) userService.getBaseMapper();
h2UserMapper.insert(h2User);
h2UserMapper.deleteById(h2User.getTestId());
h2User = h2UserMapper.getById(h2User.getTestId());
Assertions.assertNotNull(h2User.getLastUpdatedDt());

h2User = new H2User("test");
h2UserMapper.insert(h2User);
h2UserMapper.deleteById(h2User.getTestId(), false);
h2User = h2UserMapper.getById(h2User.getTestId());
Assertions.assertNull(h2User.getLastUpdatedDt());

h2User = new H2User("test");
h2UserMapper.insert(h2User);
h2UserMapper.deleteById(String.valueOf(h2User.getTestId()));
h2User = h2UserMapper.getById(h2User.getTestId());
Assertions.assertNotNull(h2User.getLastUpdatedDt());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,7 @@ public interface H2UserMapper extends SuperMapper<H2User> {

@Select("select count(*) from h2user")
Long selectCountLong();

@Select("select * from h2user where test_id = #{id}")
H2User getById(@Param("id") Long id);
}

0 comments on commit be2f0c3

Please sign in to comment.