Mybatis常用語法彙總

2021-09-29 21:42:23 字數 1260 閱讀 5165

一、動態sql使用

1.1、在專案中涉及多個動態查詢條件,一般我們是通過 where 1 = 1,這樣可以處理where後面對應條件全空的情況,我們可以使用標籤,該標籤可以自動處理,主要是當我們的sql查詢條件以and和or結尾時,會自動去除,如

and title like concat('%',trim(#),'%')   

or name=#

or title like concat('%',trim(#),'%')

1.2、在專案中涉及多個動態update條件時,傳統的專案需要我們去除最後乙個條件的逗號,但是在mybatis中我們可以使用標籤,如

update user

name = #,

email = #,

head_url = #,

link_data = #,

create_time = #,

update_time = #

1.3、動態if else語句,在mybatis中使用choose、when、otherwise來處理,如下**

select * from order where amount gt;= # 

and merchant_id = #;

and area = #;

or 1 = 1;

二、mybatis大於、小於、等於

<

<=

>

>=&'

"<

<=

>

>=&'

" 三、mybatis迴圈標籤

3.1、迴圈查詢in語句,**如下

select * from user where user_id in

#

3.2、批量插入使用迴圈,**如下

insert into user

( name,***,email,remark)

values(#,

#,#,#)

四、重複的sql片段整合在一起使用include標籤

定義:

id, name, url, priority, logo, img

引用:

五、一對一查詢、一對多查詢,檢視對應博文springboot整合mybatis實現一對

一、一對多查詢

MyBatis常用語法

1 將查詢出的結果為null 的轉換為0coalesce sum s.num 0 2 null 賦值ifnull name,0 2 對二進位制位判斷位數 status 二進位製欄位 and s.status 1 0 第一位為1 and s.status 2 0 第二位為1 and s.status ...

mysql常用語法 MySQL常用語法

更新資料 update 表名 set 健名 更改的值 where 建 刪除資料 delete from 表名 where 索引 create index 索引名稱 on 表名 鍵名 建立試圖 create view 檢視名 鍵,鍵 as select matchno,abs won lost fro...

mysql基本常用語法 mysql 常用語法

1.1.開啟 service mysql start 1.2.關閉 service mysql stop 1.3.重啟 service mysql restart 2.1.密碼明文 語法 mysql u使用者名稱 p使用者密碼 舉例 mysql uroot p123456 2.2.密碼密文 語法 m...