• 请不要在回答技术问题时复制粘贴 AI 生成的内容
DimitriYoon
V2EX  ›  程序员

MybatisPlus 用什么方式实现多字段模糊查询比较好?

  •  
  •   DimitriYoon · Sep 17, 2023 · 2443 views
    This topic created in 969 days ago, the information mentioned may be changed or developed.

    有一个实体类需要实现多个字段的模糊查询,字段的数量不是一定的,想要在实体类传进来的时候,对于不为空的字段做模糊查询,有没有什么比较好不怎么屎的实现方法?

    6 replies    2023-09-17 21:18:35 +08:00
    ikas
        1
    ikas  
       Sep 17, 2023
    自己扩展一个 query 方法注入到 mybatisplus 中或者写一个 SelectProvider
    Vkery
        2
    Vkery  
       Sep 17, 2023
    自己写一个工具类,可以通过反射将实体类转为 Query
    itechify
        3
    itechify  
    PRO
       Sep 17, 2023 via Android
    例如?给个例子
    Foxkeh
        4
    Foxkeh  
       Sep 17, 2023
    官方的就支持, 比如查询条件拼接的时候
    eq(StringUtils.hasText(Order::getStatus, req.getStatus())
    改为
    eq(StringUtils.hasText(req.getStatus()), Order::getStatus, req.getStatus())
    Foxkeh
        5
    Foxkeh  
       Sep 17, 2023
    @Foxkeh 不好意思粘贴错了, 是类似这样改
    eq(Order::getStatus, req.getStatus())
    改为
    eq(StringUtils.hasText(req.getStatus()), Order::getStatus, req.getStatus())
    wellerman
        6
    wellerman  
       Sep 17, 2023
    10 个字段内列出来也不是什么问题:
    public LambdaQuery<T> eqIfPresent(SFunction<T, ?> column, Object val) {
    if (val != null) {
    return (LambdaQuery<T>) super.eq(column, val);
    }
    return typedThis;
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1131 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 18:07 · PVG 02:07 · LAX 11:07 · JFK 14:07
    ♥ Do have faith in what you're doing.