slomo
V2EX  ›  Java

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

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