LambdaQueryWrapper 是 MyBatis Plus 提供的一个强大的查询条件构建工具,它允许你使用 Lambda 表达式来构建查询条件,从而使代码更加简洁和易读。下面详细介绍 LambdaQueryWrapper 的使用方法及其底层原理。
什么是 LambdaQueryWrapper?
LambdaQueryWrapper 是 MyBatis Plus 提供的一个类,用于构建复杂的查询条件。它基于 Lambda 表达式,可以方便地进行条件拼接,支持多种查询条件的组合,如等于、不等于、大于、小于、模糊查询等。
主要特点
- 类型安全:使用 Lambda 表达式,编译器可以检查表达式的正确性,避免了字符串拼接带来的错误。
- 代码简洁:使用 Lambda 表达式,代码更加简洁和易读。
- 灵活多变:支持多种查询条件的组合,满足复杂查询需求。
常用方法
以下是一些常用的 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);
}
底层原理
-
Lambda 表达式:
LambdaQueryWrapper使用 Lambda 表达式来指定查询条件,编译器会在编译时检查表达式的正确性,避免了运行时错误。- 例如,
Employee::getName是一个方法引用,表示Employee类的getName方法。
-
条件拼接:
LambdaQueryWrapper内部维护了一个条件列表,每次调用条件方法(如eq、like等)时,会将条件添加到列表中。- 最终,这些条件会被拼接成一个完整的 SQL 查询语句。
-
SQL 生成:
- 当调用
selectOne、selectList等方法时,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可以轻松地构建复杂的查询条件,满足各种查询需求。
