MybatisPlus条件构造器wrapper方法的使用
Posted 浪子尘晨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MybatisPlus条件构造器wrapper方法的使用相关的知识,希望对你有一定的参考价值。
目录
(4)like、notLike、likeLeft、likeRight
一、Wrapper介绍
在Mybatis-Plus(MP)中,简单的查询可以使用MP已经封装好的API来实现,但复杂的查询语句可以通过MP提供的Wrapper来进行封装。
Wrapper继承体系
Wrapper:条件构造抽象类,最顶端父类.
AbstractWrapper:用于查询条件封装,生成sql的where条件
QueryWrapper:Entity对象封装操作类,不是lambda语法
UpdateWrapper:Update条件封装,用于Entity对象更新操作
AbstractLambdaWrapper:Lambda语法使用Wrapper统一处理解析lambda获取column
LambdaQueryWrapper:用于Lambda语法使用的查询Wrapper
LambdaUpdateWrapper:Lambda更新封装Wrapper
二、QueryWrapper的方法
三、目录结构
(1)目录
1、实体类
@TableName("animal") ---> 关联的表名
@Data---->引入Lombok插件即可使用
@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")--->日期格式转换.
@DateTimeFormat注解加不加都可以,因为使用的是String类型.
package com.wrapper.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
/**
* 动物实体类
*/
@TableName("animal")
@EqualsAndHashCode(callSuper = false)
@Data
public class Animal
/**
* 动物名称
*/
private String name;
/**
* 年龄
*/
private int age;
/**
* 动物别名
*/
private String alias;
/**
* 动物描述
*/
private String description;
/**
* 动物信息入机时间
*/
@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")
private String inputtime;
/**
* 更新时间
*/
@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")
private String updatetime;
2、Mapper接口层
package com.wrapper.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wrapper.entity.Animal;
import org.apache.ibatis.annotations.Mapper;
/**
* 接口
*/
@Mapper
public interface AnimalMapper extends BaseMapper<Animal>
3、AnimalServiceImpl服务实现层
package com.wrapper.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wrapper.entity.Animal;
import com.wrapper.mapper.AnimalMapper;
import com.wrapper.service.IAnimalService;
import org.springframework.stereotype.Service;
/**
* 服务实现层
*/
@Service
public class AnimalServiceImpl extends ServiceImpl<AnimalMapper, Animal> implements IAnimalService
4、IAnimalService服务类
package com.wrapper.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.wrapper.entity.Animal;
/**
* 服务类
*/
public interface IAnimalService extends IService<Animal>
5、application.yml配置
spring:
datasource:
username: root
password: 123456
#?serverTimezone=UTC解决时区的报错
url: jdbc:mysql://localhost:3306/wapper_test_20211201?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
6、pom.xml配置
这里引用了thymeleaf和freemarker模板引擎依赖.去掉也可以.项目中并没有用到.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wrapper</groupId>
<artifactId>wrapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>wrapper</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>$spring-boot.version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.wrapper.WrapperApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
(2)数据库
CREATE TABLE `animal` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`name` varchar(20) NOT NULL COMMENT '动物名称',
`age` int DEFAULT NULL COMMENT '年龄',
`alias` varchar(40) NOT NULL COMMENT '动物别名',
`description` varchar(300) DEFAULT '' COMMENT '动物描述',
`inputtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '动物信息入机时间',
`updatetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '动物信息更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
四、实例
(1)eq、ne
eq:等于
@Test
void contextEq()
QueryWrapper<Animal> queryWrapper = new QueryWrapper<>();
//eq:等于
//查询name等于狮子的数据.
QueryWrapper<Animal> eq = queryWrapper.eq("name", "狮子");
Animal one = animalService.getOne(eq);
System.out.println(one);
Animal(name=狮子, age=4, alias=辛巴, description=影视, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:09)
ne:不等于
@Test
void contextNe()
QueryWrapper<Animal> queryWrapper = new QueryWrapper<>();
//ne:不等于
//查询name不等于蓝猫的数据
QueryWrapper<Animal> ne = queryWrapper.ne("name", "蓝猫");
//name不等于蓝猫的数据有多条,所以使用list
List<Animal> list = animalService.list(ne);
System.out.println(list);
[Animal(name=西伯利亚猩猩, age=1, alias=黑猩猩, description=西伯利亚, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:02), Animal(name=东北虎, age=2, alias=大脑虎, description=东北地区, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=孔雀, age=3, alias=白孔雀, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=狮子, age=4, alias=辛巴, description=影视, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:09), Animal(name=边境牧羊犬, age=5, alias=边牧, description=家中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:12), Animal(name=西伯利亚雪橇犬, age=6, alias=二哈, description=富足家庭中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:14), Animal(name=大熊猫, age=8, alias=国宝, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:19)]
(2)gt、ge、lt、le
gt:大于,ge:大于等
@Test
void contextGt()
QueryWrapper<Animal> queryWrapper = new QueryWrapper<>();
//gt:大于 查询年龄age大于4的数据
QueryWrapper<Animal> gt = queryWrapper.gt("age", 5);
List<Animal> list1 = animalService.list(gt);
System.out.println(list1);
System.out.println("===============================");
//ge:大于等于 查询年龄大于等于5的数据
QueryWrapper<Animal> ge = queryWrapper.ge("age", 6);
List<Animal> list2 = animalService.list(ge);
System.out.println(list2);
[Animal(name=西伯利亚雪橇犬, age=6, alias=二哈, description=富足家庭中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:14), Animal(name=蓝猫, age=7, alias=猫, description=家养, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:16), Animal(name=大熊猫, age=8, alias=国宝, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:19)]
===============================
[Animal(name=西伯利亚雪橇犬, age=6, alias=二哈, description=富足家庭中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:14), Animal(name=蓝猫, age=7, alias=猫, description=家养, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:16), Animal(name=大熊猫, age=8, alias=国宝, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:19)]
lt:小于,le:小于等于
@Test
void contextLt()
//第一种写法 lt:小于 查询年龄小于6的数据
QueryWrapper<Animal> queryWrapper = new QueryWrapper<>();
QueryWrapper<Animal> lt = queryWrapper.lt("age", 6);
List<Animal> list3 = animalService.list(lt);
System.out.println(list3);
//第二种写法
List<Animal> list4 = animalService.list(new LambdaQueryWrapper<Animal>().lt(Animal::getAge, 6));
System.out.println(list4);
System.out.println("=======================");
//第一种写法 le:小于等于 查询年龄小于等于5的数据
QueryWrapper<Animal> queryWrapper1 = new QueryWrapper<>();
QueryWrapper<Animal> le = queryWrapper1.le("age",5);
List<Animal> list5 = animalService.list(le);
System.out.println(list5);
//第二种写法
List<Animal> list6 = animalService.list(new LambdaQueryWrapper<Animal>().le(Animal::getAge,5));
System.out.println(list6);
[Animal(name=西伯利亚猩猩, age=1, alias=黑猩猩, description=西伯利亚, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:02), Animal(name=东北虎, age=2, alias=大脑虎, description=东北地区, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=孔雀, age=3, alias=白孔雀, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=狮子, age=4, alias=辛巴, description=影视, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:09), Animal(name=边境牧羊犬, age=5, alias=边牧, description=家中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:12)]
[Animal(name=西伯利亚猩猩, age=1, alias=黑猩猩, description=西伯利亚, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:02), Animal(name=东北虎, age=2, alias=大脑虎, description=东北地区, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=孔雀, age=3, alias=白孔雀, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=狮子, age=4, alias=辛巴, description=影视, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:09), Animal(name=边境牧羊犬, age=5, alias=边牧, description=家中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:12)]
=======================
[Animal(name=西伯利亚猩猩, age=1, alias=黑猩猩, description=西伯利亚, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:02), Animal(name=东北虎, age=2, alias=大脑虎, description=东北地区, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=孔雀, age=3, alias=白孔雀, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=狮子, age=4, alias=辛巴, description=影视, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:09), Animal(name=边境牧羊犬, age=5, alias=边牧, description=家中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:12)]
[Animal(name=西伯利亚猩猩, age=1, alias=黑猩猩, description=西伯利亚, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:02), Animal(name=东北虎, age=2, alias=大脑虎, description=东北地区, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=孔雀, age=3, alias=白孔雀, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=狮子, age=4, alias=辛巴, description=影视, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:09), Animal(name=边境牧羊犬, age=5, alias=边牧, description=家中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:12)]
(3)between、notbetween
between:在值1和值2之间,notbetween:不在值1和值2之间
@Test
void contextBetween()
//between:在值1和值2之间. 查询年龄为5到7之间的数据
List<Animal> list7 = animalService.list(new LambdaQueryWrapper<Animal>().between(Animal::getAge,5,7));
System.out.println(list7);
System.out.println("================");
//notbetween:不在值1和值2之间
List<Animal> list8 = animalService.list(new LambdaQueryWrapper<Animal>().notBetween(Animal::getAge,2,8));
System.out.println(list8);
[Animal(name=边境牧羊犬, age=5, alias=边牧, description=家中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:12), Animal(name=西伯利亚雪橇犬, age=6, alias=二哈, description=富足家庭中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:14), Animal(name=蓝猫, age=7, alias=猫, description=家养, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:16)]
================
[Animal(name=西伯利亚猩猩, age=1, alias=黑猩猩, description=西伯利亚, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:02)]
(4)like、notLike、likeLeft、likeRight
like:’%值%’,notLike:’%值%’
@Test
void contextLike()
//like:%值% sql中的模糊查询 查询名字中带有犬的数据
List<Animal> list9 = animalService.list(new LambdaQueryWrapper<Animal>().like(Animal::getName,"犬"));
System.out.println(list9);
System.out.println("=============================");
//notLike:%值% 查询名字不带有猫的数据
List<Animal> list10 = animalService.list(new LambdaQueryWrapper<Animal>().notLike(Animal::getName,"猫"));
System.out.println(list10);
[Animal(name=边境牧羊犬, age=5, alias=边牧, description=家中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:12), Animal(name=西伯利亚雪橇犬, age=6, alias=二哈, description=富足家庭中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:14)]
=============================
[Animal(name=西伯利亚猩猩, age=1, alias=黑猩猩, description=西伯利亚, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:02), Animal(name=东北虎, age=2, alias=大脑虎, description=东北地区, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=孔雀, age=3, alias=白孔雀, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=狮子, age=4, alias=辛巴, description=影视, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:09), Animal(name=边境牧羊犬, age=5, alias=边牧, description=家中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:12), Animal(name=西伯利亚雪橇犬, age=6, alias=二哈, description=富足家庭中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:14)]
likeLeft:’%值’,likeRight:'值%'
@Test
void contextLeftRight()
//likeLeft:%值 左边数据模糊,右边为"虎". 查询name(动物名称)中最后一个为"虎"的数据
List<Animal> list11 = animalService.list(new LambdaQueryWrapper<Animal>().likeLeft(Animal::getName,"虎"));
System.out.println(list11);
System.out.println("===========================");
//likeRight:值% 左边为"大",右边数据模糊. 查询名字第一个为"大"的数据
List<Animal> list12 = animalService.list(new LambdaQueryWrapper<Animal>().likeRight(Animal::getName,"大"));
System.out.println(list12);
[Animal(name=东北虎, age=2, alias=大脑虎, description=东北地区, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27)]
===========================
[Animal(name=大熊猫, age=8, alias=国宝, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:19)]
(5)isNull、isNotNull
isNull:字段IS NULL(空),isNotNull:字段IS NOT NULL(不为空)
@Test
void contextNull()
//isNull:为空 查询alias(动物别名)为空的数据
List<Animal> list13 = animalService.list(new LambdaQueryWrapper<Animal>().isNull(Animal::getAlias));
System.out.println(list13);
System.out.println("=============================");
//isNotNull:不为空 查询alias(动物别名)不为空的数据
List<Animal> list14 = animalService.list(new LambdaQueryWrapper<Animal>().isNotNull(Animal::getAlias));
System.out.println(list14);
[]
=============================
[Animal(name=西伯利亚猩猩, age=1, alias=黑猩猩, description=西伯利亚, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:02), Animal(name=东北虎, age=2, alias=大脑虎, description=东北地区, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=孔雀, age=3, alias=白孔雀, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=狮子, age=4, alias=辛巴, description=影视, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:09), Animal(name=边境牧羊犬, age=5, alias=边牧, description=家中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:12), Animal(name=西伯利亚雪橇犬, age=6, alias=二哈, description=富足家庭中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:14), Animal(name=蓝猫, age=7, alias=猫, description=家养, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:16), Animal(name=大熊猫, age=8, alias=国宝, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:19)]
(6)in、notIn
in:指定查询的值,notIn:不介于指定的值;in:字段 IN (v0, v1, …),notIn:字段 NOT IN (value.get(0), value.get(1), …)
@Test
void contextIn()
//in:指定查询的值 查询age(年龄)为3,4的数据
List<Animal> list15 = animalService.list(new LambdaQueryWrapper<Animal>().in(Animal::getAge,3,4));
System.out.println(list15);
System.out.println("===========================");
//notNot:不介于指定的值 查询age(年龄)不为1,2,3,4,5,6的数据
List<Animal> list16 = animalService.list(new LambdaQueryWrapper<Animal>().notIn(Animal::getAge,1,2,3,4,5,6));
System.out.println(list16);
[Animal(name=孔雀, age=3, alias=白孔雀, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=狮子, age=4, alias=辛巴, description=影视, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:09)]
===========================
[Animal(name=蓝猫, age=7, alias=猫, description=家养, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:16), Animal(name=大熊猫, age=8, alias=国宝, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:19)]
(7)inSql、notInSql
inSql:字段 IN ( sql语句 ),notInSql:字段 NOT IN ( sql语句 )
@Test
void contextInSql()
//inSql:字段In (sql语句) 通过name查询age < 2 的数据
List<Animal> list17 = animalService.list(new LambdaQueryWrapper<Animal>().inSql(Animal::getName,"select name from animal where age < 2"));
System.out.println(list17);
System.out.println("===========================");
//notInSql:字段notIn (sql语句) 通过那么查询age > 7 的数据
List<Animal> list18 = animalService.list(new LambdaQueryWrapper<Animal>().notInSql(Animal::getName,"select name from animal where age < 7"));
System.out.println(list18);
[Animal(name=西伯利亚猩猩, age=1, alias=黑猩猩, description=西伯利亚, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:02)]
===========================
[Animal(name=蓝猫, age=7, alias=猫, description=家养, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:16), Animal(name=大熊猫, age=8, alias=国宝, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:19)]
(8)or、and
or:拼接OR,AND嵌套
注意事项:
主动调用or表示紧接着下一个方法不是用and连接!(不调用or则默认为使用and连接)
@Test
void contextOrAnd()
//or:或 查询name(动物名称)等于狮子或者age(年龄)小于等于2的数据
List<Animal> list19 = animalService.list(new LambdaQueryWrapper<Animal>().eq(Animal::getName,"狮子").or().le(Animal::getAge,2));
System.out.println(list19);
System.out.println("=======================");
//and:并且 查询anme(动物名称)最后一个为"犬"并且age(年龄)大于5的数据
List<Animal> list20 = animalService.list(new LambdaQueryWrapper<Animal>().likeLeft(Animal::getName,"犬").gt(Animal::getAge,5));
System.out.println(list20);
[Animal(name=西伯利亚猩猩, age=1, alias=黑猩猩, description=西伯利亚, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:02), Animal(name=东北虎, age=2, alias=大脑虎, description=东北地区, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=狮子, age=4, alias=辛巴, description=影视, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:09)]
=======================
[Animal(name=西伯利亚雪橇犬, age=6, alias=二哈, description=富足家庭中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:14)]
不使用and默认就是and
(9)exists、notExists
exists:拼接EXISTS(sql语句),notExists:拼接NOT EXISTS(sql语句)
@Test
void contextExists()
//exists:拼接 查询age(年龄)大于6 的数据
List<Animal> list21 = animalService.list(new LambdaQueryWrapper<Animal>().exists("select name,alias,description from animal where age > 6"));
System.out.println(list21);
查询结果显示 数据库版本有问题 说明不支持该版本的MySQL.导致数据结果不对.
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2021-12-01 18:00:53.425 INFO 20440 --- [ main] com.alibaba.druid.pool.DruidDataSource : dataSource-1 inited
[Animal(name=西伯利亚猩猩, age=1, alias=黑猩猩, description=西伯利亚, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:02), Animal(name=东北虎, age=2, alias=大脑虎, description=东北地区, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=孔雀, age=3, alias=白孔雀, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=狮子, age=4, alias=辛巴, description=影视, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:09), Animal(name=边境牧羊犬, age=5, alias=边牧, description=家中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:12), Animal(name=西伯利亚雪橇犬, age=6, alias=二哈, description=富足家庭中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:14), Animal(name=蓝猫, age=7, alias=猫, description=家养, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:16), Animal(name=大熊猫, age=8, alias=国宝, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:19)]
(10)orderByAsc、orderByDesc
orderByAsc:排序:ORDER BY 字段, … ASC,orderByDesc:排序:ORDER BY 字段, … DESC
@Test
void contextOrderByAsc()
//orderByAsc:升序 通过age(年龄)升序排序
List<Animal> list22 = animalService.list(new LambdaQueryWrapper<Animal>().orderByAsc(Animal::getAge));
System.out.println(list22);
System.out.println("=============================");
//orderByDesc:降序 通过age(年龄)降序排序
List<Animal> list23 = animalService.list(new LambdaQueryWrapper<Animal>().orderByDesc(Animal::getAge));
System.out.println(list23);
[Animal(name=西伯利亚猩猩, age=1, alias=黑猩猩, description=西伯利亚, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:02), Animal(name=东北虎, age=2, alias=大脑虎, description=东北地区, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=孔雀, age=3, alias=白孔雀, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=狮子, age=4, alias=辛巴, description=影视, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:09), Animal(name=边境牧羊犬, age=5, alias=边牧, description=家中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:12), Animal(name=西伯利亚雪橇犬, age=6, alias=二哈, description=富足家庭中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:14), Animal(name=蓝猫, age=7, alias=猫, description=家养, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:16), Animal(name=大熊猫, age=8, alias=国宝, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:19)]
=============================
[Animal(name=大熊猫, age=8, alias=国宝, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:19), Animal(name=蓝猫, age=7, alias=猫, description=家养, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:16), Animal(name=西伯利亚雪橇犬, age=6, alias=二哈, description=富足家庭中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:14), Animal(name=边境牧羊犬, age=5, alias=边牧, description=家中, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:12), Animal(name=狮子, age=4, alias=辛巴, description=影视, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:09), Animal(name=孔雀, age=3, alias=白孔雀, description=北京动物园, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=东北虎, age=2, alias=大脑虎, description=东北地区, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 11:48:27), Animal(name=西伯利亚猩猩, age=1, alias=黑猩猩, description=西伯利亚, inputtime=2021-10-27 15:30:50, updatetime=2021-12-01 15:25:02)]
文章知识点与官方知识档案匹配,可进一步学习相关知识
以上是关于MybatisPlus条件构造器wrapper方法的使用的主要内容,如果未能解决你的问题,请参考以下文章
MybatisPlus条件构造器Wrapper分页查询自定义SQLService层接口代码生成器
Java--MybatisPlus Wrapper构造器;分页;MP代码生成器