微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Mybatisplus怎么自定义SQL注入器查询@TableLogic逻辑删除后的数据

这篇文章主要介绍了Mybatisplus怎么自定义sql注入器查询@TableLogic逻辑删除后的数据的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Mybatisplus怎么自定义sql注入器查询@TableLogic逻辑删除后的数据文章都会有所收获,下面我们一起来看看吧。

    1  需求

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

    解决方

    自定义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);
        }
     
        /**
         * 拼接where条件根据map参数
         *
         * @param table 表
         * @return sql
         */
        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);
        }
     
        /**
         * 拼接where条件
         *
         * @param newLine 新行
         * @param table   表
         * @return sql
         */
        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> {
     
        /**
         * 根据map条件查询数据,并忽略逻辑删除
         *
         * @param columnMap 查询条件
         * @return 结果信息
         */
        List<T> selectIgnoreLogicDeleteByMap(@Param("cm") Map<String, Object> columnMap);
     
        /**
         * 根据条件查询数据,并忽略逻辑删除
         *
         * @param queryWrapper 查询条件
         * @return 结果信息
         */
        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);
        }

    关于“Mybatisplus怎么自定义sql注入器查询@TableLogic逻辑删除后的数据”这篇文章内容就介绍到这里,感谢各位的阅读!相信大家对“Mybatisplus怎么自定义sql注入器查询@TableLogic逻辑删除后的数据”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程之家行业资讯频道。

    版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

    相关推荐