Mybatis中使用in()查詢的方式詳解
這篇文章我會演示幾種mybatis中使用in查詢的方式。
1 數(shù)組、字符串
2 集合
3 使用Myabtis-plus框架的條件構(gòu)造器來實現(xiàn)
我們在mysql中使用in查詢的方式是這樣的
那在mybatis中我們使用<foreach>標(biāo)簽來實現(xiàn)包含查詢
1 使用數(shù)組方式
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中的每個元素,默認(rèn)為item可以自定義。
測試類:
我們可以使用字符串來接收參數(shù),使用逗號分隔每個參數(shù),然后把分隔后的參數(shù)放到集合中。
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框架的條件構(gòu)造器來進行查詢
@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)]
附:Mybatis-plus的條件構(gòu)造器詳細(xì)使用教程
常用函數(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)造器完成一個簡單的查詢
// 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); }
那么再來一點更多條件的
// 我們要查詢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); }
總結(jié)
到此這篇關(guān)于Mybatis中使用in()查詢方式的文章就介紹到這了,更多相關(guān)Mybatis使用in()查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用SpringBoot進行優(yōu)雅的數(shù)據(jù)驗證
這篇文章主要介紹了如何使用SpringBoot進行優(yōu)雅的數(shù)據(jù)驗證,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Java代碼實現(xiàn)Map和Object互轉(zhuǎn)及Map和Json互轉(zhuǎn)
這篇文章主要介紹了Java代碼實現(xiàn)map和Object互轉(zhuǎn)及Map和json互轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2016-05-05SpringBoot中默認(rèn)緩存實現(xiàn)方案的示例代碼
這篇文章主要介紹了SpringBoot中默認(rèn)緩存實現(xiàn)方案,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08Java流程控制之循環(huán)結(jié)構(gòu)while、do...while
這篇文章主要介紹了Java流程控制之循環(huán)結(jié)構(gòu)while及do...while,文章除了講解循環(huán)結(jié)構(gòu)while和do...while之外,還講解了他們之間的區(qū)別,下面我們就一起進入文章講解更多詳細(xì)內(nèi)容吧2021-12-12Java使用字節(jié)流實現(xiàn)圖片音頻的復(fù)制
今天帶大家學(xué)習(xí)Java的相關(guān)知識,文章圍繞著Java如何使用字節(jié)流實現(xiàn)圖片音頻的復(fù)制展開,文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-06-06JavaWeb 文件的上傳和下載功能簡單實現(xiàn)代碼
這篇文章主要介紹了JavaWeb 文件的上傳和下載功能簡單實現(xiàn)代碼,需要的朋友可以參考下2017-04-04