LambdaQueryWrapper超详细

时间:2025-03-17 13:12:17
LambdaQueryWrapper是MyBatis-Plus提供的一种查询构建器,可以通过Lambda表达式来构建查询条件

引入依赖
在文件中引入MyBatis-Plus的依赖:

<dependency>
    <groupId></groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version></version>
</dependency>

创建LambdaQueryWrapper对象
创建LambdaQueryWrapper对象时,需要指定实体类的Class类型,如:

LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

构建查询条件
LambdaQueryWrapper有多种构建查询条件的方法,常用的有以下几种:

(1)eq方法:等于查询

(2)gt方法:大于查询

(3)ge方法:大于等于查询

(4)lt方法:小于查询

(5)le方法:小于等于查询

(6)like方法:​​​​​​模糊查询

(7)in方法:IN查询

(8)orderByAsc方法:升序排序

(9)orderByDesc方法:降序排序

(10)last方法:在查询语句的最后面添加SQL语句

("LIMIT 10");

(11)page方法:分页查询

Page<User> page = new Page<>(1, 10);
(User::getName, "张三").page(page);

执行查询
构建好LambdaQueryWrapper对象后,可以通过MyBatis-Plus提供的BaseMapper进行查询操作,如:

List<User> list = (wrapper);

其中,userMapper是一个继承了MyBatis-Plus提供的BaseMapper的接口,可以直接调用selectList方法进行查询操作。