Wrapper 是所有条件构造器的父类,作用是生成条件语句,即where后面的sql子句

在调用查询,更新,删除操作时,需要根据条件进行判断,可以使用条件构造器进行组合条件,生成where后面条件子句

QueryWrapper,UpdateWrapper

LambdaQueryWrapper,LambdaUpdateWrapper

UpdateWrapper提供了set方法,可以设置更新后的数据
QueryWrapper,可以对查询,更新,删除操作进行条件组合

LambdaQueryWrapper、LambdaUpdateWrapper支持Lambda表达式,可以通过Lambda表达式获取到对应的字段,省去字段名的编写

有Lambda前缀的,表示支持Lambda表达式

application.yml

server:port: 8080#数据源配置spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatisplus212?characterEncoding=utf-8&serverTimezone=UTCusername: rootpassword: 123456type: com.zaxxer.hikari.HikariDataSourcemybatis-plus:configuration:#配置日志信息log-impl: org.apache.ibatis.logging.stdout.StdOutImpl#配置mapper映射文件的位置,默认在类路径/mapper*.xmlmapper-locations: classpath:/mapper/*.xml#设置指定包下所有类设置类型别名type-aliases-package: com.dzqc.pojoglobal-config:db-config:#统一设置实体类对应的数据表的默认前缀table-prefix: t_#设置主键自增id-type: auto

Wrapper测试类

package com.dzqc;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;import com.dzqc.pojo.User;import com.dzqc.service.IUserService;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import java.util.List;/** * 测试条件构造器 * 组合where后面条件子句 * QueryWrapper * UpdateWrapper */@SpringBootTestpublic class WrapperTest {@Autowiredprivate IUserService userService;@Testpublic void testWapperTest(){//根据id查询用户信息//select*from t_userwhere id=4//创建条件构造器QueryWrapper objectQueryWrapper = new QueryWrapper();//组合条件 连缀操作objectQueryWrapper.eq("u_id",4).likeRight("u_name","李").ge("age",20).isNotNull("email");List list = userService.list(objectQueryWrapper);System.out.println("list = " + list);}//测试 并且 或者 条件@Testpublic void testWrapper(){//更新年龄大于20 或者 邮箱不为空的用户姓名 为小章// UPDATE t_user SET u_name=? WHERE is_delete=0 AND (age > ? OR email IS NULL)//创建 条件构造器UpdateWrapper updateWrapper = new UpdateWrapper();updateWrapper.gt("age",25).or().isNull("email");//更新后用户数据User user = new User();user.setName("李华");boolean update = userService.update(user, updateWrapper);System.out.println("update = " + update);}//测试删除@Testpublic void testWrappper2(){QueryWrapper objectQueryWrapper = new QueryWrapper();objectQueryWrapper.eq("u_id",2).or().isNotNull("age");boolean remove = userService.remove(objectQueryWrapper);System.out.println("remove = " + remove);}@Testpublic void testWrapper3(){LambdaQueryWrapper objectLambdaQueryWrapper = new LambdaQueryWrapper();objectLambdaQueryWrapper.ge(User::getId,2).likeRight(User::getName,"李").ge(User::getAge,24).isNotNull(User::getEmail);List list = userService.list(objectLambdaQueryWrapper);list.forEach(System.out::println);}@Testpublic void testWrapper7(){//更新LambdaUpdateWrapper objectLambdaUpdateWrapper = new LambdaUpdateWrapper();objectLambdaUpdateWrapper.isNull(User::getEmail);//更新后数据objectLambdaUpdateWrapper.set(User::getAge,18);boolean update = userService.update(null, objectLambdaUpdateWrapper);System.out.println("update = " + update);}@Testpublic void testWrapper8(){Integer age=19;//查询年龄不为空的用户信息,进行删除LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper();lambdaUpdateWrapper.eq(User::getName,"王五").or().isNotNull(User::getEmail).isNotNull(User::getAge);boolean remove = userService.remove(lambdaUpdateWrapper);System.out.println("remove = " + remove);}}