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

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

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

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

    6 replies  •  2023-09-17 21:18:35 +08:00
    ikas
    ikas
    Sep 17, 2023
    1
    自己扩展一个 query 方法注入到 mybatisplus 中或者写一个 SelectProvider
    Vkery
    Vkery
    Sep 17, 2023
    2
    自己写一个工具类,可以通过反射将实体类转为 Query
    itechify
    itechify
    Sep 17, 2023 via Android
    3
    例如?给个例子
    Foxkeh
    Foxkeh
    Sep 17, 2023
    4
    官方的就支持, 比如查询条件拼接的时候
    eq(StringUtils.hasText(Order::getStatus, req.getStatus())
    改为
    eq(StringUtils.hasText(req.getStatus()), Order::getStatus, req.getStatus())
    Foxkeh
    Foxkeh
    Sep 17, 2023
    5
    @Foxkeh 不好意思粘贴错了, 是类似这样改
    eq(Order::getStatus, req.getStatus())
    改为
    eq(StringUtils.hasText(req.getStatus()), Order::getStatus, req.getStatus())
    wellerman
    wellerman
    Sep 17, 2023
    6
    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   ·   Privacy   ·   Solana   ·   2859 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 10:17 · PVG 18:17 · LAX 02:17 · JFK 05:17
    ♥ Do have faith in what you're doing.