亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Mybatis中使用in()查詢的方式詳解

 更新時間:2022年08月18日 10:27:53   作者:保加利亞的風(fēng)  
當(dāng)參數(shù)有值,添加條件查詢,附帶一個字符串的in查詢,下面這篇文章主要給大家介紹了關(guān)于Mybatis中使用in()查詢的方式,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下

這篇文章我會演示幾種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
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)造器完成一個簡單的查詢

// 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)文章

最新評論