mybatis-plus @TableLogic 逻辑删除 恢复 自定义方法
mybatis-plus @TableLogic 逻辑删除 恢复 自定义方法 不写SQL
·
目标:
mybatis-plus @TableLogic 逻辑删除 后的数据不能用 mybatis-plus的方法恢复,又不想写sql
所以用 mybatis-plus 的自定义方法解决这个问题
直接上代码
1.继承 AbstractMethod 拼凑sql
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
/**
* <p>
* </p>
*
* @author L
* @date 2019/6/14
*/
public class RecoverBatch extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
final String sql = "<script>update %s set %s where %s</script>";
final String fieldSql = prepareFieldSql(tableInfo);
final String whereSql = prepareWhereSqlForMysqlBatch(tableInfo);
final String sqlResult = String.format(sql, tableInfo.getTableName(), fieldSql, whereSql);
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
return this.addInsertMappedStatement(mapperClass, modelClass, "recoverBatch", sqlSource, new NoKeyGenerator(), null, null);
}
private String prepareFieldSql(TableInfo tableInfo) {
final StringBuilder valueSql = new StringBuilder();
final TableFieldInfo logicDeleteFieldInfo = tableInfo.getLogicDeleteFieldInfo();
final String logicDeleteColum = logicDeleteFieldInfo.getColumn();
final String logicNotDeleteValue = logicDeleteFieldInfo.getLogicNotDeleteValue();
valueSql.append(logicDeleteColum).append("=").append(logicNotDeleteValue);
return valueSql.toString();
}
private String prepareWhereSqlForMysqlBatch(TableInfo tableInfo) {
final StringBuilder whereSql = new StringBuilder();
whereSql.append(tableInfo.getKeyColumn()).append(" ").append("in").append(" ");
whereSql.append("<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"(\" separator=\",\" close=\")\">");
whereSql.append("#{item.").append(tableInfo.getKeyProperty()).append("}");
whereSql.append("</foreach>");
return whereSql.toString();
}
}
2.自定义Mapper
import java.util.List;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author L
* @since 2018-08-13
*/
public interface PteromysMapper<T> extends BaseMapper<T> {
/**
* 批量恢复
* @param batchList
* @return
*/
int recoverBatch(@Param("list") List<T> batchList);
}
3.方法注册
import java.util.List;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.injector.methods.SelectById;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
/**
* 自定义 SqlInjector
*
* @author miemie
* @since 2018-08-13
*/
public class PteromysSqlInjector extends DefaultSqlInjector {
/**
* 如果只需增加方法,保留MP自带方法
* 可以super.getMethodList() 再add
* @return
*/
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
methodList.add(new RecoverBatch());
return methodList;
}
}
4.使用
业务mapper继承自定义的PteromysMapper,调用 recoverBatch 方法
更多推荐
已为社区贡献3条内容
所有评论(0)