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 {@Autowiredprivate UserMapper userMapper;@Testpublic 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升序排序
@Testpublic 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的用户信息
@Testpublic 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的用户信息修改
@Testpublic 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)的用户信息修改
@Testpublic 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字句
查询用户的用户名,年龄,邮箱信息
@Testpublic 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的用户信息
@Testpublic 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)的用户信息修改
@Testpublic 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);}
模拟开发中组件条件的情况
@Testpublic 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组装条件
@Testpublic 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
- 防止我们写字段名的时候写错
@Testpublic 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
@Testpublic 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);}