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

免费发布产品网站阿里巴巴国际站

免费发布产品网站,阿里巴巴国际站,微模板如何建微网站,莲都网站建设LambdaQueryWrapper 是 MyBatis Plus 提供的一个强大的查询条件构建工具,它允许你使用 Lambda 表达式来构建查询条件,从而使代码更加简洁和易读。下面详细介绍 LambdaQueryWrapper 的使用方法及其底层原理。 什么是 LambdaQueryWrapper? La…

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.hkea.cn/news/642392/

相关文章:

  • 广东省建设教育协会官方网站首页html简单网页代码
  • 个人网站意义阿里指数官网最新版本
  • 网站开发方式有哪四种搜索引擎优化课程总结
  • 申请做网站、论坛版主app推广接单
  • 青海网站建设广州seo优化推广
  • 物流公司网站制作模板上海网站关键词排名
  • 广西建设人才网搜索引擎优化的目标
  • 比汉斯设计网站素材图片搜索识图入口
  • php网站架设教程英雄联盟韩国
  • 做毕设好的网站百度客服电话24小时
  • 上海手机网站建设电话咨询seo综合查询系统
  • wordpress 4.6 中文版沈阳seo
  • 文件管理软件天津搜索引擎优化
  • 九亭网站建设全国疫情高峰时间表最新
  • 青岛网站建设公司武汉seo收费
  • mvc网站建设的实验报告怎么做优化
  • 有官网建手机网站千锋教育培训多少钱费用
  • b2c交易模式的网站有哪些百度营销客户端
  • flash 学习网站重庆网站seo多少钱
  • 年终总结ppt模板免费下载网站小红书seo排名规则
  • 自己架设网站口碑营销的产品有哪些
  • 湖北省网站备案最快几天天津百度推广排名优化
  • app在线开发制作平台seo网络优化前景怎么样
  • 商务网站的基本情况网站建设工作总结
  • 山西建设厅网站网络销售怎么聊客户
  • 软装素材网站有哪些seo网络排名优化哪家好
  • 邯郸市做网站建设网络口碑营销案例分析
  • 罗湖网站建设联系电话西安核心关键词排名
  • 如何编写网站电脑清理软件十大排名
  • 怎么给企业制作网站seo关键词排名优化哪好