你说得对!我在第一轮对话中提到了“常用条件方法列表”,但没有展开详细解释。现在我把每个常用条件方法都给你完整补上:
📚 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)
.ne(EquipmentB::getSymbol, "VD")
.gt(EquipmentB::getPrice, 100)
.ge(EquipmentB::getLevel, 10)
.lt(EquipmentB::getWeight, 50)
.le(EquipmentB::getDurability, 100);
queryWrapper.like(EquipmentB::getName, "龙")
.likeLeft(EquipmentB::getDesc, "传说")
.likeRight(EquipmentB::getPrefix, "神")
.notLike(EquipmentB::getSuffix, "破损");
queryWrapper.in(EquipmentB::getType, Arrays.asList(1, 2, 3))
.notIn(EquipmentB::getQuality, 6, 7, 8)
.between(EquipmentB::getPrice, 500, 1000)
.notBetween(EquipmentB::getWeight, 80, 120);
queryWrapper.isNull(EquipmentB::getDeleteTime)
.isNotNull(EquipmentB::getUpdateTime);
queryWrapper.orderByAsc(EquipmentB::getLevel)
.orderByDesc(EquipmentB::getCreateTime);
queryWrapper.and(wrapper -> wrapper
.eq(EquipmentB::getType, 1)
.or()
.eq(EquipmentB::getType, 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;
String name = "龙";
LambdaQueryWrapper<EquipmentB> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(status != null, EquipmentB::getStatus, status);
queryWrapper.like(StringUtils.hasText(name), EquipmentB::getName, name);
现在这些常用条件方法都完整了!还有其他想了解的吗?比如 .apply() 动态拼接 SQL、.last() 追加到最后、.nested() 嵌套条件等高级用法?
所有评论(0)