Mybatis中使用in()查询的方式详解
作者:保加利亚的风
当参数有值,添加条件查询,附带一个字符串的in查询,下面这篇文章主要给大家介绍了关于Mybatis中使用in()查询的方式,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
这篇文章我会演示几种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 |
| 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()查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
