本篇内容主要讲解“Mybatis中怎么使用in()查询”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Mybatis中怎么使用in()查询”吧!
我们在mysql中使用in查询的方式是这样的
那在mybatis中我们使用<foreach>标签来实现包含查询
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可以自定义。
测试类:
我们可以使用字符串来接收参数,使用逗号分隔每个参数,然后把分隔后的参数放到集合中。
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
测试:
@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)]
常用函数:
函数 | 说明 | 例子(以下为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()查询”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。