溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Mybatis中怎么使用in()查詢

發(fā)布時(shí)間:2022-08-27 09:21:36 來源:億速云 閱讀:317 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“Mybatis中怎么使用in()查詢”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Mybatis中怎么使用in()查詢”吧!

我們在mysql中使用in查詢的方式是這樣的

Mybatis中怎么使用in()查詢

那在mybatis中我們使用<foreach>標(biāo)簽來實(shí)現(xiàn)包含查詢

1 使用數(shù)組方式

Mapper:

Mybatis中怎么使用in()查詢

 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可以自定義。

測試類:

Mybatis中怎么使用in()查詢

我們可以使用字符串來接收參數(shù),使用逗號(hào)分隔每個(gè)參數(shù),然后把分隔后的參數(shù)放到集合中。

Mybatis中怎么使用in()查詢

2 使用List集合的方式

Mapper:

Mybatis中怎么使用in()查詢

 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

 測試:

Mybatis中怎么使用in()查詢

3 第三種我們使用Mybatis-plus框架的條件構(gòu)造器來進(jìn)行查詢

@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
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)

使用構(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í)!

向AI問一下細(xì)節(jié)

免責(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)容。

AI