Mybatis-Plus的条件构造器

条件构造器

在这里插入图片描述

1、组装查询条件

创建测试类MybatisPlusWrapperTest

package com.example.mybatisplus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mapper.UserMapper;
import com.example.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 test1(){
        //查询用户名包含a,年龄在20-30岁,邮箱信息不是null的用户信息
        /*
        SELECT uid AS id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 
         AND (name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL)
         */
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name","a")
                        .between("age",20,30)
                                .isNotNull("email");
        List<User> list = userMapper.selectList(queryWrapper);
        list.forEach(System.out::println);
    }

}

在这里插入图片描述

2、组装排序条件

@Test
    public void test2(){
        //查询用户信息,按照年龄的降序排序,若年龄相同,则按照id升序
       //SELECT uid AS id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 ORDER BY age DESC,id ASC
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("age")
                .orderByAsc("id"); 
        List<User> list = userMapper.selectList(queryWrapper);
        list.forEach(System.out::println);
    }

在这里插入图片描述

3、组装删除条件

@Test
    public void test3(){
        //删除邮箱为null的用户信息
        //UPDATE t_user SET is_deleted=1 WHERE is_deleted=0 AND (email IS NOT NULL)
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.isNotNull("email");
        int delete = userMapper.delete(queryWrapper);
        System.out.println("result"+delete);
    }

在这里插入图片描述

使用QueryWrapper实现修改功能

原数据表

在这里插入图片描述

 @Test
    public void test4(){
        //将(年龄大于20并且用户名包含有a)或者邮箱为null的用户信息修改
        //UPDATE t_user SET name=?, email=? WHERE is_deleted=0 AND (age > ? AND name LIKE ? OR email IS NOT NULL)
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.gt("age",20)
                .like("name","a")
                .or()
                .isNotNull("email");
        User user=new User();
        user.setName("小明");
        user.setEmail("xiaoming@qq.com");
        int result = userMapper.update(user,queryWrapper);
        System.out.println("result:"+result);
    }

在这里插入图片描述
在这里插入图片描述

4、条件的优先级

@Test
    public void test5(){
        //将用户名中包含a并且(年龄大于20或邮箱为null)的用户信息修改
        //lambda中的条件优先执行
        //UPDATE t_user SET name=?, email=? WHERE is_deleted=0 AND (name LIKE ? AND (age > ? OR email IS NULL))
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name","a")
                .and(i->i.gt("age",20).or().isNull("email"));
        User user=new User();
        user.setName("小红");
        user.setEmail("xiaohong@qq.com");
        int result = userMapper.update(user, queryWrapper);
        System.out.println("result:"+result);
    }

在这里插入图片描述

5、组装select语句

@Test
    public void test6(){
        //查询用户的用户名、年龄、邮箱信息
        //SELECT name,age,email FROM t_user WHERE is_deleted=0
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("name","age","email");
        List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
        maps.forEach(System.out::println);
    }

在这里插入图片描述

6、组装子查询

现数据表
在这里插入图片描述

@Test
    public void test7(){
        //SELECT uid AS id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (uid IN (select uid from t_user where uid<=100))
        //查询id<=100的用户信息(使用子查询)
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.inSql("uid", "select uid from t_user where uid<=100");
        List<User> list = userMapper.selectList(queryWrapper);
        list.forEach(System.out::println);
    }

在这里插入图片描述

使用UpdateWraper实现修改功能

 @Test
    public void test8(){
        将用户名中包含a并且(年龄大于20或邮箱为null)的用户信息修改
        //UPDATE t_user SET name=?,email=? WHERE is_deleted=0 AND (name LIKE ? AND (age > ? OR email IS NULL))
        UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
        updateWrapper.like("name","a")
                .and(i->i.gt("age",20).or().isNull("email"));
        updateWrapper.set("name","小黑").set("email","abc@12.com");
        int result = userMapper.update(null, updateWrapper);
        System.out.println("result:"+result);
    }

在这里插入图片描述

模拟开发中的组装条件情况

@Test
    public void test9(){
        //SELECT uid AS id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (name LIKE ? AND age >= ? AND age <= ?)
        String name="";
        Integer ageBegin=20;
        Integer ageEnd=30;
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isBlank(name)){
            //isBlank判断某个字符是否不为空字符串,不为null,不为空白符
            queryWrapper.like("name",name);
        }
        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 name="";
        Integer ageBegin=20;
        Integer ageEnd=30;
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isBlank(name),"name",name)
                .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(){
        //SELECT uid AS id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (name LIKE ? AND age >= ? AND age <= ?)
        String name="a";
        Integer ageBegin=20;
        Integer ageEnd=30;
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(name),User::getName,name)
                .ge(ageBegin!=null,User::getAge,ageBegin)
                .le(ageEnd!=null,User::getAge,ageEnd);
        List<User> list = userMapper.selectList(lambdaQueryWrapper);
        list.forEach(System.out::println);
    }

LambdaUpdateWrapper

@Test
    public void test12(){
        将用户名中包含a并且(年龄大于20或邮箱为null)的用户信息修改
        //UPDATE t_user SET name=?,email=? WHERE is_deleted=0 AND (name LIKE ? AND (age > ? OR email IS 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@12.com");
        int result = userMapper.update(null, updateWrapper);
        System.out.println("result:"+result);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值