巴中哪里做网站,推销网站的方法,品牌网络营销,中国十大网络营销公司LambdaQueryWrapper 是 MyBatis Plus 提供的一个强大的查询条件构建工具#xff0c;它允许你使用 Lambda 表达式来构建查询条件#xff0c;从而使代码更加简洁和易读。下面详细介绍 LambdaQueryWrapper 的使用方法及其底层原理。
什么是 LambdaQueryWrapper#xff1f;
La…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 BaseMapperEmployee {
}使用 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) {LambdaQueryWrapperEmployee queryWrapper new LambdaQueryWrapper();queryWrapper.eq(Employee::getName, name);return employeeMapper.selectOne(queryWrapper);}
}2. 多条件查询
public ListEmployee getEmployeesByConditions(String name, String phone) {LambdaQueryWrapperEmployee queryWrapper new LambdaQueryWrapper();queryWrapper.eq(Employee::getName, name).like(Employee::getPhone, phone);return employeeMapper.selectList(queryWrapper);
}3. 排序查询
public ListEmployee getEmployeesOrderByCreateTime() {LambdaQueryWrapperEmployee queryWrapper new LambdaQueryWrapper();queryWrapper.orderByDesc(Employee::getCreateTime);return employeeMapper.selectList(queryWrapper);
}4. 复合条件查询
public ListEmployee getEmployeesByComplexConditions(String name, String phone, Integer status) {LambdaQueryWrapperEmployee 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可以轻松地构建复杂的查询条件满足各种查询需求。