当前位置: 首页 > news >正文

lambdaQueryWrapper详细解释

LambdaQueryWrapper 是 MyBatis Plus 提供的一个强大的查询条件构建工具,它允许你使用 Lambda 表达式来构建查询条件,从而使代码更加简洁和易读。下面详细介绍 LambdaQueryWrapper 的使用方法及其底层原理。

什么是 LambdaQueryWrapper

LambdaQueryWrapper 是 MyBatis Plus 提供的一个类,用于构建复杂的查询条件。它基于 Lambda 表达式,可以方便地进行条件拼接,支持多种查询条件的组合,如等于、不等于、大于、小于、模糊查询等。

主要特点

  1. 类型安全:使用 Lambda 表达式,编译器可以检查表达式的正确性,避免了字符串拼接带来的错误。
  2. 代码简洁:使用 Lambda 表达式,代码更加简洁和易读。
  3. 灵活多变:支持多种查询条件的组合,满足复杂查询需求。

常用方法

以下是一些常用的 LambdaQueryWrapper 方法:

  • eq:等于
  • ne:不等于
  • gt:大于
  • ge:大于等于
  • lt:小于
  • le:小于等于
  • like:模糊查询(包含)
  • notLike:模糊查询(不包含)
  • in:在某个集合内
  • notIn:不在某个集合内
  • isNull:为空
  • isNotNull:不为空
  • orderByAsc:升序排序
  • orderByDesc:降序排序

示例代码

假设我们有一个 Employee 实体类和对应的 EmployeeMapper 接口:

package com.itheima.reggie.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;import java.time.LocalDateTime;@Data
@TableName("employee")
public class Employee {@TableId(type = IdType.AUTO)private Long id;private String name;private String username;private String password;private String phone;private String email;private Integer status;private LocalDateTime createTime;private LocalDateTime updateTime;private Long createUser;private Long updateUser;
}
package com.itheima.reggie.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.reggie.entity.Employee;public interface EmployeeMapper extends BaseMapper<Employee> {
}

使用 LambdaQueryWrapper 的示例

1. 等于条件查询
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class EmployeeService {@Autowiredprivate EmployeeMapper employeeMapper;public Employee getEmployeeByName(String name) {LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(Employee::getName, name);return employeeMapper.selectOne(queryWrapper);}
}
2. 多条件查询
public List<Employee> getEmployeesByConditions(String name, String phone) {LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(Employee::getName, name).like(Employee::getPhone, phone);return employeeMapper.selectList(queryWrapper);
}
3. 排序查询
public List<Employee> getEmployeesOrderByCreateTime() {LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.orderByDesc(Employee::getCreateTime);return employeeMapper.selectList(queryWrapper);
}
4. 复合条件查询
public List<Employee> getEmployeesByComplexConditions(String name, String phone, Integer status) {LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(Employee::getName, name).like(Employee::getPhone, phone).eq(Employee::getStatus, status).orderByDesc(Employee::getCreateTime);return employeeMapper.selectList(queryWrapper);
}

底层原理

  1. Lambda 表达式

    • LambdaQueryWrapper 使用 Lambda 表达式来指定查询条件,编译器会在编译时检查表达式的正确性,避免了运行时错误。
    • 例如,Employee::getName 是一个方法引用,表示 Employee 类的 getName 方法。
  2. 条件拼接

    • LambdaQueryWrapper 内部维护了一个条件列表,每次调用条件方法(如 eqlike 等)时,会将条件添加到列表中。
    • 最终,这些条件会被拼接成一个完整的 SQL 查询语句。
  3. SQL 生成

    • 当调用 selectOneselectList 等方法时,LambdaQueryWrapper 会将条件列表转换为 SQL 语句,并执行查询。
    • 例如,queryWrapper.eq(Employee::getName, name).like(Employee::getPhone, phone) 会生成类似以下的 SQL 语句:
      SELECT * FROM employee WHERE name = ? AND phone LIKE ?
      

总结

LambdaQueryWrapper 是 MyBatis Plus 提供的一个强大的查询条件构建工具,它使用 Lambda 表达式来构建查询条件,使代码更加简洁和易读。通过 LambdaQueryWrapper可以轻松地构建复杂的查询条件,满足各种查询需求。

http://www.lryc.cn/news/486105.html

相关文章:

  • 【工控】线扫相机小结 第三篇
  • golang中的init函数
  • 理解和选择Vue的组件风格:组合式API与选项式API详解
  • Java基础——高级技术
  • 什么是SSL VPN?其中的协议结构是怎样的?
  • 程序员高频率面试题-整理篇
  • 第二十二章 TCP 客户端 服务器通信 - TCP设备的OPEN和USE命令关键字
  • CSS 语法规范
  • Linux开发常用命令
  • Linux第92步_如何编写“设备树”下的platform设备驱动
  • 从零开始学习 sg200x 多核开发之 eth0 MAC 地址修改
  • JMeter与大模型融合应用之JMeter日志分析服务化实战应用
  • AtCoder Beginner Contest 380(A-F)
  • 多线程-阻塞队列
  • el-table合并单元格之后,再进行隔行换色的且覆盖表格行鼠标移入的背景色的实现
  • java模拟键盘实现selenium上下左右键 table中的左右滚动条实现滚动
  • SDF,一个从1978年运行至今的公共Unix Shell
  • 前馈神经网络 (Feedforward Neural Network, FNN)
  • 【Python进阶】Python中的数据库交互:使用SQLite进行本地数据存储
  • ZooKeeper单机、集群模式搭建教程
  • 函数指针示例
  • vue如何实现组件切换
  • 计算机视觉 1-8章 (硕士)
  • 整数唯一分解定理
  • Grass脚本2倍速多账号
  • 15分钟学 Go 第 56 天:架构设计基本原则
  • HTML5 Video(视频)
  • 开源模型应用落地-qwen模型小试-Qwen2.5-7B-Instruct-tool usage入门-串行调用多个tools(三)
  • MySQL:表设计
  • 173. 二叉搜索树迭代器【 力扣(LeetCode) 】