广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Mybatis-plus自定义SQL注入器查询@TableLogic逻辑删除后的数据详解
  • 704
分享到

Mybatis-plus自定义SQL注入器查询@TableLogic逻辑删除后的数据详解

mybatis-plus自定义sql注入器mybatis plus 逻辑删除记录查询 2023-03-09 17:03:34 704人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录1  需求2  解决方案3  方案:3.1  方案1,继承 AbstractMethod拼接sql语句3.2. 方案2,继承&n

1  需求

mybatis-plus使用@TableLogic注解进行逻辑删除数据后,在某些场景下,又需要查询该数据时,又不想写SQL。

2  解决方案

自定义Mybatis-plus的SQL注入器一劳永逸的解决该问题

3  方案:

3.1  方案1,继承 AbstractMethod拼接SQL语句

public class SelectIgnoreLogicDeleteByMap extends AbstractMethod {
 
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
 
        String sqlBase= "<script>SELECT %s FROM %s %s\n</script>";
        String sqlScript = this.sqlWhereByMap(tableInfo);
        String sql = String.fORMat(sqlBase, this.sqlSelectColumns(tableInfo, false), tableInfo.getTableName(), sqlScript);
        SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, Map.class);
        return this.addSelectMappedStatementForTable(mapperClass, "selectIgnoreLogicDeleteByMap", sqlSource, tableInfo);
    }
 
    
    protected String sqlWhereByMap(TableInfo table) {
        String sqlScript;
        sqlScript = SqlScriptUtils.convertChoose("v == null", " ${k} IS NULL ", " ${k} = #{v} ");
        sqlScript = SqlScriptUtils.convertForeach(sqlScript, "cm", "k", "v", "AND");
        sqlScript = SqlScriptUtils.convertWhere(sqlScript);
        sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null and !%s", "cm", "cm.isEmpty"), true);
        return sqlScript;
    }
}

3.2. 方案2,继承 AbstractMethod拼接SQL语句

public class SelectIgnoreLogicDelete extends AbstractMethod {
 
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
 
        String sqlBase = "<script>%s SELECT %s FROM %s %s %s %s\n</script>";
 
        String sql = String.format(sqlBase, this.sqlFirst(), this.sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), this.sqlWhereEntityWrapper(true, tableInfo), this.sqlOrderBy(tableInfo), this.sqlComment());
        SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
        return this.addSelectMappedStatementForTable(mapperClass, "selectIgnoreLogicDelete", sqlSource, tableInfo);
    }
 
    
    protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
        String sqlScript = table.getAllSqlWhere(false, true, "ew.entity.");
        sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", "ew.entity"), true);
        sqlScript = sqlScript + "\n";
        sqlScript = sqlScript + SqlScriptUtils.convertIf(String.format(SqlScriptUtils.convertIf(" AND", String.format("%s and %s", "ew.nonEmptyOfEntity", "ew.nonEmptyOfNormal"), false) + " ${%s}", "ew.sqlSegment"), String.format("%s != null and %s != '' and %s", "ew.sqlSegment", "ew.sqlSegment", "ew.nonEmptyOfWhere"), true);
        sqlScript = SqlScriptUtils.convertWhere(sqlScript) + "\n";
        sqlScript = sqlScript + SqlScriptUtils.convertIf(String.format(" ${%s}", "ew.sqlSegment"), String.format("%s != null and %s != '' and %s", "ew.sqlSegment", "ew.sqlSegment", "ew.emptyOfWhere"), true);
        sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", "ew"), true);
        return newLine ? "\n" + sqlScript : sqlScript;
    }

4.  自定义SQL注入器,注册上述自定义的方法

public class CustomSqlInjector extends DefaultSqlInjector {
 
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
        methodList.add(new SelectIgnoreLogicDeleteByMap());
        methodList.add(new SelectIgnoreLogicDelete());
        return methodList;
    }
}

5.  自定义基础mapper,声明注册的方法

public interface CustomBaseMapper<T> extends BaseMapper<T> {
 
    
    List<T> selectIgnoreLogicDeleteByMap(@Param("cm") Map<String, Object> columnMap);
 
    
    List<T> selectIgnoreLogicDelete(@Param("ew") Wrapper<T> queryWrapper);
}

6. 使用声明的方法

6.1  业务mapper继承自定义的CustomBaseMapper

@Mapper
public interface UserMapper extends CustomBaseMapper<User> {
}

6.2 调用方法selectIgnoreLogicDelete

    @Override
    public List<User> getIgnoreDeleteById(Long userId) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,userId);
        return this.baseMapper.selectIgnoreLogicDelete(queryWrapper);
    }

6.3 调用方法selectIgnoreLogicDeleteByMap

    @Override
    public List<User> getIgnoreDeleteById(Long userId) {
		Map<String, Object> columnMap = new HashMap<>(2);
        columnMap.put("id", userId);
        return this.baseMapper.selectIgnoreLogicDeleteByMap(columnMap);
    }

总结

到此这篇关于Mybatis-plus自定义SQL注入器查询@TableLogic逻辑删除后的数据的文章就介绍到这了,更多相关Mybatis-plus查询@TableLogic逻辑删除后数据内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Mybatis-plus自定义SQL注入器查询@TableLogic逻辑删除后的数据详解

本文链接: https://www.lsjlt.com/news/199132.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作