[MyBatisPlus]条件构造器wapper
Posted 唐火
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[MyBatisPlus]条件构造器wapper相关的知识,希望对你有一定的参考价值。
wapper简介
QueryWrapper
组装查询条件
查询用户名包含a,年龄在20到30之间,邮箱信息不为null的用户信息
package com.xxxx.mybatisplus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxxx.mybatisplus.mapper.UserMapper;
import com.xxxx.mybatisplus.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class MyBatisPlusWrapperTest
@Autowired
private UserMapper userMapper;
@Test
public void test01()
// 查询用户名包含a,年龄在20到30之间,邮箱信息不为null的用户信息
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("user_name","a")
.between("age",20,30)
.isNotNull("email");
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
组装排序条件
查询用户信息,按照年龄的降序排序,若年龄相同,则按照id升序排序
@Test
public void test02()
// 查询用户信息,按照年龄的降序排序,若年龄相同,则按照id升序排序
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("age")
.orderByAsc("uid");
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
组装删除条件
删除邮箱地址为null的用户信息
@Test
public void test03()
// 删除邮箱地址为null的用户信息
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.isNull("email");
int result = userMapper.delete(queryWrapper);
System.out.println("result = "+result);
使用QueryWrapper实现修改功能
将(年龄大于20并且用户名中包含有a) 或邮箱为null的用户信息修改
@Test
public void test04()
// 将(年龄大于20并且用户名中包含有a) 或邮箱为null的用户信息修改
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.gt("age",20)
.like("user_name","a")
.or()
.isNull("email");
User user = new User();
user.setName("小明");
user.setEmail("test@qq.com");
int result = userMapper.update(user, queryWrapper);
System.out.println("result = "+result);
条件的优先级
将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改
@Test
public void test05()
// 优先级不同,我们实现2
// 1.将(年龄大于20并且用户名中包含有a)或邮箱为null的用户信息修改
// 2.将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("user_name","a")
// lambda表达式中的条件优先执行
.and(i -> i.gt("age",20).or().isNull("email"));
User user = new User();
user.setName("小红");
user.setEmail("test@atasdfd.com");
int result = userMapper.update(user, queryWrapper);
System.out.println("result = "+result);
组装select字句
查询用户的用户名,年龄,邮箱信息
@Test
public void test06()
// 查询用户的用户名,年龄,邮箱信息
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("user_name","age","email");
List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
maps.forEach(System.out::println);
组装子查询
查询id小于等于100的用户信息
@Test
public void test07()
// 查询id小于等于100的用户信息
// SELECT uid AS id,user_name AS name,age,email,is_deleted FROM user WHERE is_deleted=0 AND (uid IN (select uid from user where uid <= 100))
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.inSql("uid","select uid from user where uid <= 100");
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
UpdateWrapper
UpdateWrapper实现修改功能
将用户名中含有a并且(年龄大于20或邮箱为null)的用户信息修改
@Test
public void test08()
// 将用户名中含有a并且(年龄大于20或邮箱为null)的用户信息修改
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.like("user_name","a")
.and(i -> i.gt("age",20).or().isNull("email"));
updateWrapper.set("user_name","小黑").set("email","abc@bb.com");
int result = userMapper.update(null, updateWrapper);
System.out.println("result = "+result);
模拟开发中组件条件的情况
@Test
public void test09()
String username = "";
Integer ageBegin = 20;
Integer ageEnd = 30;
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// isNotBlank 判断某个字符串是否不为空字符串,不为null,不为空白符
if (StringUtils.isNotBlank(username))
queryWrapper.like("username",username);
if (ageBegin != null)
queryWrapper.ge("age",ageBegin);
if (ageEnd != null)
queryWrapper.le("age",ageEnd);
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
但是上面的方法比较麻烦,所以我们用下面这种方法!
使用condition组装条件
@Test
public void test10()
String username = "a";
Integer ageBegin = null;
Integer ageEnd = 30;
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like(StringUtils.isNotBlank(username),"user_name",username)
.ge(ageBegin!=null,"age",ageBegin)
.le(ageEnd != null,"age",ageEnd);
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
LambdaQueryWrapper
- 防止我们写字段名的时候写错
@Test
public void test11()
String username = "a";
Integer ageBegin = null;
Integer ageEnd = 30;
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(StringUtils.isNotBlank(username),User::getName,username)
.ge(ageBegin != null,User::getAge,ageBegin)
.le(ageEnd != null,User::getAge,ageEnd);
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
LambdaUpdateWrapper
@Test
public void test12()
// 将用户名中含有a并且(年龄大于20或邮箱为null)的用户信息修改
LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.like(User::getName,"a")
.and(i -> i.gt(User::getAge,20).or().isNull(User::getEmail));
updateWrapper.set(User::getName,"小黑").set(User::getEmail,"abc@bb.com");
int result = userMapper.update(null, updateWrapper);
System.out.println("result = "+result);
以上是关于[MyBatisPlus]条件构造器wapper的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot整合MybatisPlus有关条件构造器的相关查询方法