栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【mybatis-plus】mybatis-plus 删除并自动填充

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【mybatis-plus】mybatis-plus 删除并自动填充

mybatis-plus中做逻辑删除是 只是简单的修改逻辑删除的字段, 如果想要在删除的同时添加删除人,和删除时间该如何实现呢?对于填充数据 我们可以使用 mybatis-plus的自动填充功能

但是不是所有的删除都会触发自动填充功能

在mybatis-plus老版本中 删除是不能触发自动填充的
需要使用官方的Sql 注入器LogicDeleteByIdWithFill 而且调用方法为deleteByIdWithFill
在这里不具体赘述了

@Component
public class MySqlInjector extends DefaultSqlInjector {
	@Override
	public List getMethodList(Class mapperClass, TableInfo tableInfo) {
		List methods = super.getMethodList(mapperClass, tableInfo);
		methods.add(new LogicDeleteByIdWithFill());
		return methods;
	}
}

在新版本中 deleteById 默认实现了自动填充,不需要用户再手动添加sql注入器


如上图 mybatis源码中我们可以看出, 我们在使用deleteById(Entity et) 时,就可以触发自动填充
可是这也不能适用所有场景,deleteById 虽然可以实现填充的效果,但是where筛选只会根据id进行删除.
如果某些业务里面需要添加其他删除条件时无法实现了.所以我们要自己写一个sql注入器

第一步 编写注入器,这里面注意MAPPER_METHOD 是mapper中定义的方法名 ,名称随意保持一致就行

public class LogicDeleteWithFill extends AbstractMethod {

	
	private static final String MAPPER_METHOD = "deleteWithFill";

	private static final long serialVersionUID = -402373469518227818L;

	@Override
	public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) {
		String sql;
		SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE;
		if (tableInfo.isWithLogicDelete()) {
			List fieldInfos = tableInfo.getFieldList().stream()
					.filter(i -> i.getFieldFill() == FieldFill.UPDATE || i.getFieldFill() == FieldFill.INSERT_UPDATE)
					.collect(toList());
			if (CollectionUtils.isNotEmpty(fieldInfos)) {
				String sqlSet = "SET " + fieldInfos.stream().map(i -> i.getSqlSet(ENTITY_DOT)).collect(joining(EMPTY))
						+ tableInfo.getLogicDeleteSql(false, false);
				sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet,
						sqlWhereEntityWrapper(true, tableInfo), sqlComment());
			}
			else {
				sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo),
						sqlWhereEntityWrapper(true, tableInfo), sqlComment());
			}
			SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
			return addUpdateMappedStatement(mapperClass, modelClass, MAPPER_METHOD, sqlSource);
		}
		else {
			sqlMethod = SqlMethod.DELETE;
			sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
			SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
			return addDeleteMappedStatement(mapperClass, MAPPER_METHOD, sqlSource);
		}
	}


}

第二步 注册注入器

@Component
public class MySqlInjector extends DefaultSqlInjector {

	@Override
	public List getMethodList(Class mapperClass, TableInfo tableInfo) {
		List methods = super.getMethodList(mapperClass, tableInfo);
		methods.add(new LogicDeleteWithFill());
		return methods;
	}

}

第三步 在mapper中添加方法
只要在你的mapper中添加这个方法即可 不需要为该方法添加xml方法
注意这里的方法名要和sql注入器中的MAPPER_METHOD 保持一致
实体类类型与所在mapper保持一致(该例中的Dictionary)
完成这一步 就可以正常使用了

public interface DictionaryMapper extends baseMapper {

	
	int deleteWithFill(@Param(Constants.ENTITY) Dictionary param, @Param(Constants.WRAPPER) Wrapper wrapper);

}

测试

sql打印

优化 (可有可无) 为了方便使用 可以创建自己的service serviceImpl mapper
这样就不需要在每个mapper接口中添加 deleteWithFill 方法了

mapper 接口
public interface MybaseMapper extends baseMapper {

	
	int deleteWithFill(@Param(Constants.ENTITY) T param, @Param(Constants.WRAPPER) Wrapper wrapper);

}
service 接口
public interface MyService extends IService {

	
	boolean batchDelete(List idList, T t);

	
	boolean deleteWithFill(T t, Wrapper wrapper);

}

serviceImpl 实现类
public class MyServiceImpl, T extends baseEntity> extends ServiceImpl implements MyService {

		
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean batchDelete(List idList, T t) {
		return deleteWithFill(t, new LambdaQueryWrapper<>(t).in(T::getId, idList));
	}

	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteWithFill(T t, Wrapper wrapper) {
		return SqlHelper.retBool(baseMapper.deleteWithFill(t, wrapper));
	}
}
实体父类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class baseEntity implements Serializable {

	private static final long serialVersionUID = -3925407267780334782L;

	
	private Long id;

	
	@JsonInclude(Include.NON_NULL)
	@TableField(fill = FieldFill.INSERT)
	private Long createId;

	
	@JsonInclude(Include.NON_NULL)
	@TableField(fill = FieldFill.INSERT)
	private LocalDateTime createTime;

	
	@JsonIgnore
	@TableField(fill = FieldFill.INSERT_UPDATE, select = false)
	private Long updateId;

	
	@JsonIgnore
	@TableField(fill = FieldFill.INSERT_UPDATE, select = false)
	private LocalDateTime updateTime;

	
	@JsonIgnore
	private Boolean deleted;

}
业务模块继承自定义的即可


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/600941.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号