exists 和 notExists

注意:本教程使用的数据库脚本、数据模型和环境信息请参考 “MyBatis Plus环境准备” 章节,点击下载示例源码

本章节将介绍 exists 和 notExists 条件的用法,这两个条件对应了 SQL 语句中的 exists 和 not exists 关键字。它们定义如下:

exists(拼接 EXISTS  sql语句 )

exists(String existsSql)
exists(boolean condition, String existsSql)

参数说明:

  • existsSql:exists SQL 语句

  • condition:用于指定当前这个条件是否有效;如果为 true,则应用当前条件;如果为 false,则忽略当前条件

实例:如果在 user_contact 中存在 user_id 等于 1 的记录,则返回 user 表的所有记录。如下:

QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
wrapper.exists("select id from user_contact where user_id=1");

上面实例将最终执行下面SQL语句:

SELECT user_id,name,sex,age,face,salary,borthday 
FROM user 
WHERE (EXISTS (select id from user_contact where user_id=1))

注意:实际应用中,我们希望查询 user 表中的 user_id 在 user_contact 表中存在,说明用户存在联系方式。如下:

SELECT user_id,name,sex,age,face,salary,borthday
FROM `user` u
WHERE (EXISTS (select id from user_contact where user_id=u.user_id));

上面 SQL 语句只返回 user 表的 user_id 在 user_contact 表中存在的 user 记录。

notExists(拼接 NOT EXISTS sql语句)

notExists(String notExistsSql)
notExists(boolean condition, String notExistsSql)

参数说明:

  • notExistsSql:not exists SQL 语句

  • condition:用于指定当前这个条件是否有效;如果为 true,则应用当前条件;如果为 false,则忽略当前条件

实例:如果在 user_contact 表中不存在 user_id 等于 1 的记录,则返回 user 表中所有的记录。如下:

QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
wrapper.notExists("select id from user_contact where user_id=1");

上面实例将最终执行下面SQL语句:

SELECT user_id,name,sex,age,face,salary,borthday 
FROM user 
WHERE (NOT EXISTS (select id from user_contact where user_id=1))

注意:实际应用中,我们希望查询 user 表中的 user_id 在 user_contact 表中不存在,说明用户没有联系方式。如下:

SELECT user_id,name,sex,age,face,salary,borthday
FROM `user` u
WHERE (not EXISTS (select id from user_contact where user_id=u.user_id));

上面 SQL 语句只返回 user 表中的 user_id 在 user_contact 表中不存在的 user 记录。

示例代码

package com.hxstrive.mybatis_plus.simple_mapper.condition;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxstrive.mybatis_plus.mapper.SimpleMapper;
import com.hxstrive.mybatis_plus.model.UserBean;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
class Condition13Test {

    @Autowired
    private SimpleMapper simpleMapper;

    @Test
    void contextLoads() {
        QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
        wrapper.exists("select id from user_contact where user_id=1");
        List<UserBean> userBeanList = simpleMapper.selectList(wrapper);
        System.out.println("result size=" + userBeanList.size());

        wrapper = new QueryWrapper<>();
        wrapper.notExists("select id from user_contact where user_id=1");
        userBeanList = simpleMapper.selectList(wrapper);
        System.out.println("result size=" + userBeanList.size());
    }

}

运行上面代码,我们将执行如下SQL语句,如下:

Preparing: SELECT user_id,name,sex,age,face,salary,borthday FROM user WHERE (EXISTS (select id from user_contact where user_id=1))
Parameters: 

Preparing: SELECT user_id,name,sex,age,face,salary,borthday FROM user WHERE (NOT EXISTS (select id from user_contact where user_id=1))
Parameters:

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号