04 MyBatisPlus條件構造器

2021-10-07 04:17:15 字數 3269 閱讀 7210

@runwith

(springrunner.

class

)@springboottest

public

class

**注意:**以下條件構造器的方法入參中的column均表示資料庫字段

@test

public

void

testdelete()

sql:update user set deleted=1 where deleted=0 and name is null and age >= ? and email is not null

**注意:**seletone返回的是一條實體記錄,當出現多條時會報錯

@test

public

void

testselectone()

select id,name,age,email,create_time,update_time,deleted,version from user where deleted=0 and name = ?

包含大小邊界

@test

public

void

testselectcount()

select count(1) from user where deleted=0 and age between ? and ?

@test

public

void

testselectlist()

select id,name,age,email,create_time,update_time,deleted,version

from user where deleted=0 and name = ? and id = ? and age = ?

selectmaps返回map集合列表

@test

public

void

testselectmaps()

select id,name,age,email,create_time,update_time,deleted,version

from user where deleted=0 and name not like ? and email like ?

in、notin:

notin

("age",)

--->age not in (1,

2,3)

notin

("age",1

,2,3

)--->age not in (1,

2,3)

insql、notinsql:可以實現子查詢

@test

public

void

testselectobjs()

select id,name,age,email,create_time,update_time,deleted,version

from user where deleted=0 and id in (select id from user where id < 3)

不呼叫or則預設為使用and

@test

public

void

testupdate1()

update user set name=?, age=?, update_time=? where deleted=0 and name like ? or age between ? and ?

這裡使用了lambda表示式,or中的表示式最後翻譯成sql時會被加上圓括號

@test

public

void

testupdate2()

update user set name=?, age=?, update_time=?

where deleted=0 and name like ?

or ( name = ? and age <> ? )

@test

public

void

testselectlistorderby()

select id,name,age,email,create_time,update_time,deleted,version

from user where deleted=0 order by id desc

直接拼接到 sql 的最後

**注意:**只能呼叫一次,多次呼叫以最後一次為準 有sql注入的風險,請謹慎使用

@test

public

void

testselectlistlast()

select id,name,age,email,create_time,update_time,deleted,version

from user where deleted=0 limit 1

@test

public

void

testselectlistcolumn()

select id,name,age from user where deleted=0

@test

public

void

testupdateset()

();

user.setage(99);

//修改條件

.like("name", "h")

.set("name", "老李頭")//除了可以查詢還可以使用set設定修改的字段

.setsql(" email = '[email protected]'");//可以有子查詢

update user set age=?, update_time=?, name=?, email = '[email protected]' where deleted=0 and name like ?

04 MyBatisPlus條件構造器

注意 以下條件構造器的方法入參中的column均表示資料庫字段 test public void testdelete sql update user set deleted 1 where deleted 0 and name is null and age and email is not nu...

Mybatis plus實現條件查詢

教程目錄 教程一 mybatis plus使用教程 教程二 mybatis plus的字段自動填充 教程三 mybatis實現物理刪除和邏輯刪除 教程四 mybatis plus實現樂觀鎖 教程五 mybatis plus實現條件查詢 條件查詢 複雜查詢 test public void tests...

MybatisPlus條件構造器Wrapper

我們寫一些複雜的sql就可以使用它來替代!1 測試一 查詢name不為空的使用者,並且郵箱不為空的使用者,年齡大於等於12 test void contextloads 2 測試二 查詢名字狂神說 bilibili中搜尋,講的很好 test void test2 3 測試三 查詢年齡在 20 30 ...