使用 spring 数据 mongodb 存储库添加可选的查询参数 [英] Add optional query parameter using spring data mongodb repository

查看:51
本文介绍了使用 spring 数据 mongodb 存储库添加可选的查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 spring 数据 mongodb 添加可选的查询参数.

I want to add optional query parameters using spring data mongodb.

控制器代码:

@RestController
private final ActionService actionService;


@RequestMapping(value = "/action/{id}", method = RequestMethod.GET)
public ResponseEntity<List<Action>> getActionList(@PathVariable("id") long id,
            @RequestParam(value = "actionType", required = false) ActionType actionType,
            @RequestParam(value = " ", required = false) String[] params) {

        List<Action> actionList = actionService.getAction(id, actionType, params);
        return new ResponseEntity<>(actionList, HttpStatus.OK);
}

ActionServiceImpl.java

ActionServiceImpl.java

private ActionRepository actionRepository;

public List<Action> getAction(long id, ActionType type, String... var1) {
    return actionRepository.getByActionType(id, type, var1.length > 0 ? var1[0] : "", var1.length > 1 ? var1[1] : "", var1.length > 2 ? var1[2] : "");
}

ActionRepository.java

ActionRepository.java

@Repository
public interface ActionRepository extends MongoRepository<Action, String> {

    @Query(value = "{ 'id' : ?0  , 'actionType' : ?1 ,  'param1' : ?2 , 'param2': ?3 , 'param3' : ?4 } ")
    List<Action> getByActionType(long id, ActionType type, String var1, String var2, String var3);
}

注意:id"是必填字段,动作类型和参数是可选的.无论我是否传递动作类型/参数,我都想根据id"获取数据.目前,我在ActionServiceImpl"中遇到空指针异常,因为我没有传递参数和动作类型.动作类型"是枚举.

Note: 'id' is mandatory field and action type and params are optional. I want to get data based on 'id' whether I pass action type/params or not. Currently, i am getting null pointer exception in 'ActionServiceImpl' as I am not passing params and action Type. 'Action Type' is enumeration.

有人可以帮助我更改 ActionRepository @Query 标记,以便我可以根据 id 获取数据而无需传递 actionType 或 params.例如如果我传递操作类型,则 mongo 查询应根据id $ 或 actionType"给出结果.

Can someone help me to change ActionRepository @Query tag so, that I can get data based on id without passing actionType or params. e.g. if I pass action type then mongo query should give result based on 'id $or actionType'.

推荐答案

您无法使用 @Query 实现此目的.其他可能的选择是

You cannot achieve this using @Query. Other possible alternatives are

  1. 在 Repository 类中创建两个方法.一个只接受 id ,另一个接受 id 和其他参数.而在你的服务类中,你可以根据手头的数据决定调用哪一个.(不可扩展)

使用 QueryDsl.有了它,您可以根据动态拥有的数据创建搜索条件.一些有用的链接
https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#core.extensions.querydsl
http://www.baeldung.com/queries-in-spring-data-mongodb

Use QueryDsl. With this you can create search criteria based on data you have dynamically. Some helpful links
https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#core.extensions.querydsl
http://www.baeldung.com/queries-in-spring-data-mongodb

根据我个人的经验,使用 QueryDsl 是处理这些情况的最佳方法,并且可以轻松扩展以适应需求的进一步变化.

In my personal experience using QueryDsl is the best way to tackle these cases and it can be easily extended for further changes in requirement.

这篇关于使用 spring 数据 mongodb 存储库添加可选的查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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