SoFunction
Updated on 2025-04-21

Mybatis-plus query statement with brackets (.or,.and) problem

Mybatis-plus query statement with brackets (.or(), .and())

Java code

QueryWrapper<Entity> wrapper = new QueryWrapper<>();
("id", ())
(QueryWrapper -> ("name", name).or().eq("mark", mark)); 
(wrapper);

Equivalent sql

SELECT id FROM t_entity WHERE (id <> ? AND (name = ? OR mark = ?))

Each and is equivalent to a bracket

Mybatis plus brackets to implement split-side query

LambdaQueryWrapper&lt;Entity Class&gt; wrapper = new LambdaQueryWrapper&lt;&gt;();
  • (Entity class:: field name, parameter)
  • .and(condition, pr -> (entity class:: field name, parameter)
  • .or(condition).like(entity class:: field name, parameter));

Give an example

LambdaQueryWrapper<Entity> lambdaQueryWrapper = new LambdaQueryWrapper();
(Entity::getStatus, BaseServiceConstants.STATUS_0)
        .like((()) && !appFlag, Entity::getName, ())
        .like((()) && !appFlag, Entity::getSupervisor, ())
        .eq((()), Entity::getCode, ())
        .and((()) && appFlag, pr -> pr .like( Entity::getName, ())
                .or().like(Entity::getSupervisor, ()));

By defining the boolean variable appFlag, we can distinguish the query from the PC or the APP side, and the APP side implements a single parameter multi-corresponding query

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.