V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
slomo
V2EX  ›  Java

有没有大佬用过 spring-data-r2dbc

  •  
  •   slomo · 2023-03-31 17:49:11 +08:00 · 1204 次点击
    这是一个创建于 385 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是我的代码,不论 ids 列表为不为空,都会报错

        @Query("""
                SELECT SUM(age) FROM user
                where (:ids IS NULL  OR id in (:ids))
        """)
        Mono<Long> sumAges(List<Long> ids);
    

    下面这个当 ids 列表不为空时不报错

        @Query("""
                SELECT SUM(age) FROM user
                where (concat(', :ids,') = '()'  OR id in (:ids))
        """)
        Mono<Long> sumAges(List<Long> ids);
    

    我该怎么改呀,我知道可以在 service 层判断列表空直接返回 Mono.just(0L),但是还有其他许多参数需要判断时呢,总不能疯狂定制方法

    4 条回复    2023-04-09 10:56:31 +08:00
    RedBeanIce
        1
    RedBeanIce  
       2023-03-31 18:37:37 +08:00
    请贴一下最终打印的 sql ,或许你就明白了。
    kwh
        2
    kwh  
       2023-03-31 18:48:57 +08:00
    :ids IS NULL
    能这么写吗? column is NULL 吗?能写 Object is NULL???
    slomo
        3
    slomo  
    OP
       2023-04-03 09:17:39 +08:00
    @kwh 问题在于,如果这个参数不是一个列表,而是比如 string ,integer ,long ,这样都是可以的
    goalidea
        4
    goalidea  
       2023-04-09 10:56:31 +08:00
    1 楼是正确处理问题的方法
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1476 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:14 · PVG 01:14 · LAX 10:14 · JFK 13:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.