Mybatis中怎么使用in()查询

2023-05-19,

本篇内容主要讲解“Mybatis中怎么使用in()查询”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Mybatis中怎么使用in()查询”吧!

我们在mysql中使用in查询的方式是这样的

那在mybatis中我们使用<foreach>标签来实现包含查询

1 使用数组方式

Mapper:

 Mapper.xml:

<select id="studentList" resultType="com.ywt.springboot.model.Student">
        select *
        from student
        where id in
        <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

 :foreach中的 collection标签中为array,item是遍历ids中的每个元素,默认为item可以自定义。

测试类:

我们可以使用字符串来接收参数,使用逗号分隔每个参数,然后把分隔后的参数放到集合中。

2 使用List集合的方式

Mapper:

 Mapper.xml

<select id="studentList" resultType="com.ywt.springboot.model.Student">
        select *
        from student
        where id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

 使用list方式collection的value必须为list

 测试:

3 第三种我们使用Mybatis-plus框架的条件构造器来进行查询

@Test
    void Test(){
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.in("id",7,9);
        List<Student> students = studentMapper.selectList(qw);
        System.out.println(students.toString());
    }

 测试结果:

[Student(id=7, name=蔡徐坤, age=18), Student(id=9, name=金科徐, age=18)]

附:Mybatis-plus的条件构造器详细使用教程

常用函数:

函数 说明

例子(以下为where后的条件,select * from user where ?)

eq 等于= eq("name","张三") --> name = '张三'
ne 不等于 != ne("name","李四") --> name != '李四'
gt 大于 > gt(age,18) --> age > 18 //年龄大于18岁
ge 大于等于 >= ge(age,18) --> age >=18
lt 小于 < lt(age,20) --> age < 20 //年龄小于20岁
le 小于等于 <= le(age,20) ---> age <= 20
between between 值1 and 值2 between(age,15,25) ---> 匹配15岁到25岁之间(包含15和25)
nobetween not between 值1 and 值2 notBetween(age,35,45)-->匹配不包含35-45之间的(包含35和45)
like like '%值%'

like("name","张") --> like '%张%'

notlike not like '%值%' notLike("name”,"张") --> not like '%张%'
likeLeft like '%值' likeLeft("name","王") ---> like "%王"
likeRight like '值%' likeRight("name","王") ---> like "王%"
isNull 表字段 is NULL isNull("name") ---> name is null
notNull 表字段 is not NULL isNull("name") ---> name is not null
in 表字段in(v1,v2,v3...) in("num",{1,2,3}) ---> num in (1,2,3)
notIn 表字段 not in(v1.v2,v3) notIn("num",{2,3,4}) ---> num not in (2,3,4)

使用构造器完成一个简单的查询

// SQL语句:select * from user where id = ?
// 使用条件构造器QueryWrapper
    @Test
    void queryWrapper(){
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("id",1);
        List<User> users = userMapper.selectList(qw);
        users.forEach(System.out::print);
    }

那么再来一点更多条件的 

// 我们要查询name里姓氏包含 ‘张',并且年龄小于30岁的
// SQL语句:select * from user where name like '张%' and age < 30
 
// 条件构造器:
    @Test
    void queryWrapper(){
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.likeRight("name","张").lt("age","30");
        List<User> users = userMapper.selectList(qw);
        users.forEach(System.out::println);
    }
// 查询出年龄在15-25之间,并且他的名字不为空
// SQL语句:select * from user where name is not null and age between(15,25)
 
//条件构造器
    @Test
    void queryWrapper(){
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.isNotNull("name").between("age",18,25);
        List<User> users = userMapper.selectList(qw);
        users.forEach(System.out::println);
    }
// 查询名字中带有王的,并且年龄不小于30,邮箱为空的
// SQL语句:select * from user where name like '%王%' and age >= 30 and email is null
 
// 条件构造器:
    @Test
    void queryWrapper(){
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.like("name","王").ge("age",30).isNull("email");
        List<User> users = userMapper.selectList(qw);
        users.forEach(System.out::println);
    }

到此,相信大家对“Mybatis中怎么使用in()查询”有了更深的了解,不妨来实际操作一番吧!这里是本站网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

《Mybatis中怎么使用in()查询.doc》

下载本文的Word格式文档,以方便收藏与打印。