在spring boot mongodb中使用方法查询时如何为所有查询添加默认查询条件 [英] How to add default query criteria for all the queries when using method queries in spring boot mongodb

查看:73
本文介绍了在spring boot mongodb中使用方法查询时如何为所有查询添加默认查询条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对给定的 mongo 数据库执行方法查询并检索数据.但是为此,我必须检查字段 delete=false(软删除)是否用于将它们用于所有查询.

I have a requirement to execute method queries against a given mongo database and retrieve data. But for that, I have to check whether the field delete=false(Soft delete) for taking them for all the queries.

可以通过以下查询来实现

It can be achieved by the following kind of query

例如:OptionalfindByIdAndDeletedIsFalse(String id);

但是正如您所看到的,我们必须为所有查询添加 DeletedIsFalse.我尝试了 如何在 mongo spring boot 中默认为所有查询添加默认条件 但我发现它只能在我们直接使用 mongo 模板运行查询时使用.

But as you can see we have to put DeletedIsFalse for all the queries. I tried the answer provided in How to add default criteria to all the queries by default in mongo spring boot but I could find out that it can be only used then we are running queries directly using the mongo template.

经过一些调试后,我发现即使方法查询是通过 Mogno 模板执行的,它们也使用包保护的类和方法来执行此操作.所以它们不能被继承的类覆盖.我找不到让它们执行的入​​口点以及在哪里为方法查询注入默认条件.

After some debugging, I could find out that even though method queries are executed through the Mogno template, they are using package-protected classes and methods to do it. So they cannot be overridden by an inherited class. I cannot find an entry point for them to executed and where to inject default criteria for the method queries.

例如:如果我们检查MongoTemple的实现,最后是通过一个方法执行

Eg: If we check the implementation of MongoTemple, at the end the execution is happening through a method

<S, T> List<T> doFind(String collectionName, Document query, Document fields, Class<S> sourceClass, Class<T> targetClass, CursorPreparer preparer)

并且该方法是从名为 ExecutableFindOperationSupport 的内部类调用的.所有这些类都是包保护的.

and that method is invoked from an internal class called ExecutableFindOperationSupport. All those classes are package protected.

是否有任何理由让它们受到包保护而不给机会从继承的类中覆盖它们?还有没有其他方法可以使用默认条件运行方法查询而不将它们附加到所有查询中?

Is there any reason to make them package protected and not giving the chance to override them from an inherited class? Also is there any other way of running method queries with default criteria without appending them to all the queries?

推荐答案

扩展 MongoTemplate 的主要问题是我在上一个问题中建议的需要重写很多方法,因为 MongoTemplate 使用所有这些都在其内部工作中.这种方式不够灵活和稳健.

The main problem in extending MongoTemplate as I've suggested in the previous question is need to override a lot of methods because MongoTemplate uses all them in its inner work. This way is not flexible and robust.

想向您推荐另一个解决方案.您可以实现在调用 MongoTemplate 方法之前执行一些代码的 Aspect.您只需为 MongoTemplate 收到的每个查询添加附加条件.

Want to suggest you another solution. You can implement Aspect that executes some code before invoking the MongoTemplate methods. You just add the additional criterion to every query the MongoTemplate receives.

spring-boot-starter-aop 添加到您的依赖项中.在配置类中启用AOP:

Add the spring-boot-starter-aop to your dependencies. Enable AOP in the configuration class:

@Configuration
@EnableAspectJAutoProxy
public class AspectConfiguration {
}

并实现一个可以完成所有工作的小方面:

And implement a small aspect that will do all works:

@Aspect
@Component
public class RepositoryAspect {
    @Before("execution(* org.springframework.data.mongodb.core.MongoTemplate.*(..))()")
    public void before(JoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof Query) {
                Query query = (Query) arg;
                // add your criteria to the query
                return;
            }
        }
    }
}

但请记住,这种方法可能会导致执行查询的性能不佳.如果您构建的是高负载系统,那么设置标准是更好的方法 - 这是快速工作的成本.

But keep in mind that this approach may lead to bad performace of executing queries. If you build a highload system, set your criterion is better way - it's the cost for fast work.

这篇关于在spring boot mongodb中使用方法查询时如何为所有查询添加默认查询条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆