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

mybatis-plus @TableLogic 逻辑删除 恢复 自定义方法

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

mybatis-plus @TableLogic 逻辑删除 恢复 自定义方法

目标:

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;


public class RecoverBatch extends AbstractMethod {

    @Override
    public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) {
        final String sql = "";
        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("");
        whereSql.append("#{item.").append(tableInfo.getKeyProperty()).append("}");
        whereSql.append("");
        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;


public interface PteromysMapper extends BaseMapper {

   

    
    int recoverBatch(@Param("list") List 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;




public class PteromysSqlInjector extends DefaultSqlInjector {

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

4.使用

业务mapper继承自定义的PteromysMapper,调用 recoverBatch 方法

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

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

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