Mybatis中使用in()查询的方式详解

Maha ·
更新时间:2024-09-20
· 1673 次阅读

目录

1 使用数组方式

2 使用List集合的方式

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

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

总结

这篇文章我会演示几种mybatis中使用in查询的方式。

1 数组、字符串

2 集合

3 使用Myabtis-plus框架的条件构造器来实现

我们在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
betweenbetween 值1 and 值2between(age,15,25) ---> 匹配15岁到25岁之间(包含15和25)
nobetweennot between 值1 and 值2notBetween(age,35,45)-->匹配不包含35-45之间的(包含35和45)
likelike '%值%'

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

notlikenot like '%值%'notLike("name”,"张") --> not like '%张%'
likeLeftlike '%值'likeLeft("name","王") ---> like "%王"
likeRightlike '值%'likeRight("name","王") ---> like "王%"
isNull表字段 is NULLisNull("name") ---> name is null
notNull表字段 is not NULLisNull("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()查询内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



IN mybatis

需要 登录 后方可回复, 如果你还没有账号请 注册新账号