mybatis中的動態sql

2021-09-11 21:05:08 字數 965 閱讀 2228

if元素用法:

select  id ,role_name as rolename ,note from t_role where id = #

and role_name like concat('%',#,'%')

choose、when、othersize元素用法:這三個元素充當了switch語句

select role_no, role_name,note from t_role

where 1= 1

and role_no = #

and role_name like concat('%',#,'%')

and note is not null

trim、where、set元素用法:

trim去除多餘的and或者or

select  id ,role_name as rolename ,note from t_role

and id = #

其中prefix:代表語句字首

prefixoverrides:代表去除哪種關鍵字

set用法:

update t_role

role_name = #

where role_no = #

foreach元素用法:

select  * from t_role where role_no in

#

解釋:

collection:是傳遞過來的引數,該引數應該是個類似(1,3,4,5)的格式

item:迴圈的當前元素

index:當前元素在集合中的位置下表

bind元素用法:

類似與其別名:

然後# = '%'_parameter %'

mybatis中的動態SQL

動態sql的基本元素 if 單條件分支判斷 choose,when,otherwise 多條件分支判斷 trim,set,where 用於處理sql拼裝問題 foreach 迴圈語句 bind 定義乙個上下文變數 test 用於判斷條件是否成立 if條件判斷語句 當角色名稱不為空時,根據角色名稱查詢...

mybatis動態sql中foreach標籤的使用

foreach標籤主要用於構建in條件,他可以在sql中對集合進行迭代。如下 delete from user where id in 我們假如說引數為 int ids 那麼列印之後的sql如下 delete form user where id in 1,2,3,4,5 釋義 collection...

mybatis動態sql中foreach標籤的使用

foreach標籤主要用於構建in條件,他可以在sql中對集合進行迭代。如下 delete from user where id in 我們假如說引數為 int ids 那麼列印之後的sql如下 delete form user where id in 1,2,3,4,5 釋義 collection...