你说得对!我在第一轮对话中提到了“常用条件方法列表”,但没有展开详细解释。现在我把每个常用条件方法都给你完整补上:


📚 MyBatis-Plus 常用条件方法完整详解

基础比较方法

方法含义SQL 对应示例生成的 SQL
.eq()等于=.eq(EquipmentB::getSymbol, "VD")WHERE symbol = 'VD'
.ne()不等于!=.ne(EquipmentB::getSymbol, "VD")WHERE symbol != 'VD'
.gt()大于>.gt(EquipmentB::getPrice, 100)WHERE price > 100
.ge()大于等于>=.ge(EquipmentB::getPrice, 100)WHERE price >= 100
.lt()小于<.lt(EquipmentB::getPrice, 100)WHERE price < 100
.le()小于等于<=.le(EquipmentB::getPrice, 100)WHERE price <= 100

模糊匹配方法

方法含义SQL 对应示例生成的 SQL
.like()模糊匹配(两边)LIKE '%value%'.like(EquipmentB::getName, "剑")WHERE name LIKE '%剑%'
.likeLeft()左模糊LIKE '%value'.likeLeft(EquipmentB::getName, "剑")WHERE name LIKE '%剑'
.likeRight()右模糊LIKE 'value%'.likeRight(EquipmentB::getName, "剑")WHERE name LIKE '剑%'
.notLike()不匹配NOT LIKE '%value%'.notLike(EquipmentB::getName, "剑")WHERE name NOT LIKE '%剑%'

范围查询方法

方法含义SQL 对应示例生成的 SQL
.in()在集合中IN (value1, value2, ...).in(EquipmentB::getType, Arrays.asList(1,2,3))WHERE type IN (1,2,3)
.notIn()不在集合中NOT IN (...).notIn(EquipmentB::getType, Arrays.asList(1,2,3))WHERE type NOT IN (1,2,3)
.between()区间BETWEEN v1 AND v2.between(EquipmentB::getPrice, 100, 200)WHERE price BETWEEN 100 AND 200
.notBetween()不在区间NOT BETWEEN v1 AND v2.notBetween(EquipmentB::getPrice, 100, 200)WHERE price NOT BETWEEN 100 AND 200

空值判断方法

方法含义SQL 对应示例生成的 SQL
.isNull()为空IS NULL.isNull(EquipmentB::getSymbol)WHERE symbol IS NULL
.isNotNull()不为空IS NOT NULL.isNotNull(EquipmentB::getSymbol)WHERE symbol IS NOT NULL

排序方法

方法含义SQL 对应示例生成的 SQL
.orderByAsc()升序ORDER BY ... ASC.orderByAsc(EquipmentB::getCreateTime)ORDER BY create_time ASC
.orderByDesc()降序ORDER BY ... DESC.orderByDesc(EquipmentB::getCreateTime)ORDER BY create_time DESC
.orderBy(true, true, ...)多字段排序ORDER BY ....orderBy(true, true, EquipmentB::getStatus, EquipmentB::getCreateTime)ORDER BY status ASC, create_time ASC

逻辑分组方法

方法含义示例生成的 SQL
.and()AND 条件.and(wrapper -> wrapper.eq(...).ne(...))AND (col1 = v1 AND col2 != v2)
.or()OR 条件.or(wrapper -> wrapper.eq(...).eq(...))OR (col1 = v1 OR col2 = v2)
.or(boolean)条件性 OR.or(i < 0, wrapper -> wrapper.eq(...))根据条件决定是否添加 OR

完整代码示例

// 创建查询条件
LambdaQueryWrapper<EquipmentB> queryWrapper = new LambdaQueryWrapper<>();

// 基础比较
queryWrapper.eq(EquipmentB::getStatus, 1)                    // status = 1
            .ne(EquipmentB::getSymbol, "VD")                 // symbol != 'VD'
            .gt(EquipmentB::getPrice, 100)                   // price > 100
            .ge(EquipmentB::getLevel, 10)                    // level >= 10
            .lt(EquipmentB::getWeight, 50)                   // weight < 50
            .le(EquipmentB::getDurability, 100);             // durability <= 100

// 模糊匹配
queryWrapper.like(EquipmentB::getName, "龙")                  // name LIKE '%龙%'
            .likeLeft(EquipmentB::getDesc, "传说")            // desc LIKE '%传说'
            .likeRight(EquipmentB::getPrefix, "神")           // prefix LIKE '神%'
            .notLike(EquipmentB::getSuffix, "破损");          // suffix NOT LIKE '%破损%'

// 范围查询
queryWrapper.in(EquipmentB::getType, Arrays.asList(1, 2, 3)) // type IN (1,2,3)
            .notIn(EquipmentB::getQuality, 6, 7, 8)          // quality NOT IN (6,7,8)
            .between(EquipmentB::getPrice, 500, 1000)        // price BETWEEN 500 AND 1000
            .notBetween(EquipmentB::getWeight, 80, 120);     // weight NOT BETWEEN 80 AND 120

// 空值判断
queryWrapper.isNull(EquipmentB::getDeleteTime)               // delete_time IS NULL
            .isNotNull(EquipmentB::getUpdateTime);           // update_time IS NOT NULL

// 排序
queryWrapper.orderByAsc(EquipmentB::getLevel)                // level ASC
            .orderByDesc(EquipmentB::getCreateTime);         // create_time DESC

// 逻辑分组(复杂条件)
queryWrapper.and(wrapper -> wrapper
    .eq(EquipmentB::getType, 1)
    .or()
    .eq(EquipmentB::getType, 2)
);  // 生成:WHERE (type = 1 OR type = 2)

// 执行查询
List<EquipmentB> list = equipmentBMapper.selectList(queryWrapper);

生成的 SQL 示例(完整)

SELECT * FROM equipment_b 
WHERE status = 1 
  AND symbol != 'VD' 
  AND price > 100 
  AND level >= 10 
  AND weight < 50 
  AND durability <= 100 
  AND name LIKE '%龙%' 
  AND desc LIKE '%传说' 
  AND prefix LIKE '神%' 
  AND suffix NOT LIKE '%破损%' 
  AND type IN (1,2,3) 
  AND quality NOT IN (6,7,8) 
  AND price BETWEEN 500 AND 1000 
  AND weight NOT BETWEEN 80 AND 120 
  AND delete_time IS NULL 
  AND update_time IS NOT NULL 
  AND (type = 1 OR type = 2)
ORDER BY level ASC, create_time DESC

条件性查询(动态 SQL)

// 根据条件是否成立来添加查询条件
Integer status = 1;  // 可能为 null
String name = "龙";   // 可能为 null

LambdaQueryWrapper<EquipmentB> queryWrapper = new LambdaQueryWrapper<>();

// 只有 status 不为 null 时才添加 status = ? 条件
queryWrapper.eq(status != null, EquipmentB::getStatus, status);

// 只有 name 不为 null 时才添加 name LIKE ? 条件
queryWrapper.like(StringUtils.hasText(name), EquipmentB::getName, name);

// 结果:如果 status=null 且 name="龙"
// SQL: SELECT * FROM equipment_b WHERE name LIKE '%龙%'

现在这些常用条件方法都完整了!还有其他想了解的吗?比如 .apply() 动态拼接 SQL、.last() 追加到最后、.nested() 嵌套条件等高级用法?

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐