您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Mybatis中怎么使用in()查詢”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Mybatis中怎么使用in()查詢”吧!
我們在mysql中使用in查詢的方式是這樣的
那在mybatis中我們使用<foreach>標(biāo)簽來實(shí)現(xiàn)包含查詢
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標(biāo)簽中為array,item是遍歷ids中的每個(gè)元素,默認(rèn)為item可以自定義。
測試類:
我們可以使用字符串來接收參數(shù),使用逗號(hào)分隔每個(gè)參數(shù),然后把分隔后的參數(shù)放到集合中。
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()); }
測試結(jié)果:
[Student(id=7, name=蔡徐坤, age=18), Student(id=9, name=金科徐, age=18)]
常用函數(shù):
函數(shù) | 說明 | 例子(以下為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) |
使用構(gòu)造器完成一個(gè)簡單的查詢
// SQL語句:select * from user where id = ? // 使用條件構(gòu)造器QueryWrapper @Test void queryWrapper(){ QueryWrapper<User> qw = new QueryWrapper<>(); qw.eq("id",1); List<User> users = userMapper.selectList(qw); users.forEach(System.out::print); }
那么再來一點(diǎn)更多條件的
// 我們要查詢name里姓氏包含 ‘張',并且年齡小于30歲的 // SQL語句:select * from user where name like '張%' and age < 30 // 條件構(gòu)造器: @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) //條件構(gòu)造器 @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 // 條件構(gòu)造器: @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()查詢”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。