slomo
V2EX  ›  Java

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

  •  
  •   slomo · Mar 31, 2023 · 2325 views
    This topic created in 1141 days ago, the information mentioned may be changed or developed.

    这是我的代码,不论 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 replies    2023-04-09 10:56:31 +08:00
    RedBeanIce
        1
    RedBeanIce  
       Mar 31, 2023
    请贴一下最终打印的 sql ,或许你就明白了。
    kwh
        2
    kwh  
       Mar 31, 2023
    :ids IS NULL
    能这么写吗? column is NULL 吗?能写 Object is NULL???
    slomo
        3
    slomo  
    OP
       Apr 3, 2023
    @kwh 问题在于,如果这个参数不是一个列表,而是比如 string ,integer ,long ,这样都是可以的
    goalidea
        4
    goalidea  
       Apr 9, 2023
    1 楼是正确处理问题的方法
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   980 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 18:57 · PVG 02:57 · LAX 11:57 · JFK 14:57
    ♥ Do have faith in what you're doing.