動態sql語句
if標籤
例子:select * from product
where name like concat('%',#,'%')
where標籤
標籤會進行自動判斷
如果任何條件都不成立,那麼就在sql語句裡就不會出現where關鍵字
如果有任何條件成立,會自動去掉多出來的 and 或者 or。
例子:select * from product
and name like concat('%',#,'%')
and price > #
set標籤
與where標籤類似,在update語句裡也會碰到多個字段相關的問題
如果任何條件都不成立,sql語句就不會出現set關鍵字
如果有任何條件成立,set標籤會自動去掉最後乙個逗號
update product
name=#,
price=#
where id=#
trim標籤
trim 用來定製想要的功能
trim標籤可以替換where和set標籤:
prefixoverrides:字首覆蓋(去掉多餘的字首)
...
suffixoverrides:字尾覆蓋(去掉多餘的字尾)
...例子:
select * from product
and name like concat('%',#,'%')
and price > #
update product
name=#,
price=#
where id=#
choose標籤:(if else的效果)
mybatis裡面沒有else標籤,但是可以使用when otherwise標籤來達到這樣的效果。
任何when條件成立,就進行條件查詢,否則就使用otherwise條件查詢
例子:select * from product
and name like concat('%',#,'%')
and price > #
and id >1
foreach標籤
通常用於in 這樣的語法裡
例子:select * from product where id in
#呼叫的時候傳入乙個list集合的物件為引數
bind標籤
就像是對傳入的引數做一次字串拼接,方便後續使用
例子:模糊查詢,將傳入的name前後拼接上%
select * from product where name like #
動態SQL語句
動態使用sql語句的幾點技巧 動態sql語句,就是sql語句中引數會變化的sql語句,一般在程式中要根據使用者的需要隨時改變其引數值,對於動態sql語句必須注意以下幾點 先呼叫close方法,關閉query元件。如果query元件已經關閉,呼叫close方法不會出錯,也沒有其它影響。再呼叫clear...
動態SQL語句
動態sql語句 1.if 條件 2.choose,where 和 otherwise 條件 3.where 條件 4.trim 條件 5.foreach 迴圈 6.set 條件 7.bind if 有條件的包含where子句的一部分 比如 select from blog where state a...
動態SQL語句
dao的方法 update的配置方法 使用update標籤和set標籤實現動態生成sql語句 當nickname,birthday,email,pic屬性不為空,而且不為空串,則設定值 步驟在resources目錄下建立資料夾,注意 資料夾不是點號,而是斜槓。在目錄下建立userdao.xml配置檔...