MyBatis 動態元素 trim元素

2021-10-05 13:49:20 字數 4638 閱讀 9845

< trim> 標籤的作用:

我感覺trim元素是個很靈活的元素,能將要執行的sql語句修剪得整整齊齊哈哈哈。

trim元素的作用是新增字串以及剪除字串,一般可以把它當成where元素或者set元素來用,另外,只有它體內有sql語句時才會執行。

trim有四個屬性建立了它靈活的基礎:

prefix:字首,當trim標籤中的sql語句被處理完畢後,準備與原sql拼接時,會在trim的sql語句前加上字首所設定的屬性值。

是trim裡的第一句話。

prefixoverrides:前覆蓋,如果trim標籤下的sql語句開頭的字串和它的屬性值相同的話,它會剪除掉他,後面還有相同的則不會管。

suffix:字尾,當trim標籤中的sql語句被處理完畢後,準備與原sql拼接時,會在trim的sql語句的最後面加上字尾所設定的屬性值。

suffixoverrides:後覆蓋,如果trim標籤下的sql語句結尾的字串和它的屬性值相同的話,它會剪除掉他,前面相同的則不會管。

實踐一下:

以下下所有的sql語句其實都寫在cm1.xml中……

1、把它當where使用時:

<?xml version="1.0" encoding="utf-8" ?>

namespace

=>

"t" parametertype

="com.itheima.po.account"

resulttype

="com.itheima.po.account"

>

select * from account

prefix

="where"

prefixoverrides

="and"

>

test

="username != null and username != ''"

>

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

if>

test

="balance != null and balance > 0"

>

and balance > #

if>

trim

>

>

來試著拼接下

基礎的sql語句:

select * from account
當trim標籤下的if標籤的條件都為真時:

處理trim標籤中的sql語句

1、prefixoverrides="and"所以刪除最前面的and

2、and username like concat(』%』,#,』%』)加上and balance > #

3、加上字首「where」

4、最後拼接原sql

select * from account where username like concat(

'%',#,'%') and balance > #

當trim標籤下的if標籤的條件都為假時:

不做任何操作……因為體內沒有內容,所以不會加字首什麼的。

select * from account
看看當set操作怎麼樣

2、把它當set使用時:

"t2"

parametertype

="com.itheima.po.account"

>

update account

prefix

="set"

suffixoverrides

=","

suffix

="where id=2"

>

test

="username != null and username != ''"

>

username=#,

if>

test

="balance != null and balance >= 0 "

>

balance=#,

if>

trim

>

update

>

來試著拼接下

基礎的sql語句:

update account
當trim標籤下的if標籤的條件都為真時:

處理trim標籤中的sql語句

1、suffixoverrides=",「所以刪除最後的」,"

2、username=#,balance=#

3、加上字首「set」

4、加上字尾"where id=2"

4、最後拼接原sql

update account set username=#

,balance=# where id=

2

當trim標籤下只有第乙個if標籤的條件為真時:

1、suffixoverrides=",「所以刪除最後的」,"

2、username=#

3、加上字首「set」

4、加上字尾"where id=2"

4、最後拼接原sql

update account set username=# where id=

2

是不是很簡單。

3、我們在來靈活的測試下他的四個屬性

t3

"t3"

parametertype

="com.itheima.po.account"

resulttype

="com.itheima.po.account"

>

select * from account

prefix

="where 1=1 and 4=4"

prefixoverrides

="and 2=2"

>

and 2=2 and 3=3 and 2=2

trim

>

select

>

拼接後的sql如下

select * from account where 1=1 and 4=4 and 3=3 and 2=2
t4

"t4"

parametertype

="com.itheima.po.account"

resulttype

="com.itheima.po.account"

>

select * from account

prefix

="where 1=1"

suffixoverrides

="and 2=2"

>

and 2=2 and 3=3 and 2=2

trim

>

select

>

拼接後的sql如下

select * from account where 1=1 and 2=2 and 3=3
t5

"t5"

parametertype

="com.itheima.po.account"

resulttype

="com.itheima.po.account"

>

select

prefix

=" username,balance from account"

suffix

="and balance>0"

prefixoverrides

="hhh"

suffixoverrides

="34"

>

hhh where id=234

trim

>

select

>

拼接後的sql如下

select username,balance from account where  id=2 and balance>0
嘿,這只是簡單的測試,完全還能再拓展不是麼。哈,我是拓展不了了,因為能力有限,trim的實踐就到這裡,再見!

mybatis動態sql中的一些元素 if、set、trim、choose、foreach、bind

MyBatis動態SQL中trim標籤的使用引數

mybatis動態sql中trim標籤的使用 mybatis 官方文件 對 動態sql中使用trim標籤的場景及效果介紹比較少。事實上trim標籤有點類似於replace效果。trim 屬性 prefix 字首覆蓋並增加其內容 suffix 字尾覆蓋並增加其內容 prefixoverrides 字首...

mybatis動態sql中的trim標籤的使用

trim標記是乙個格式化的標記,可以完成set或者是where標記的功能,如下 1 select from user 0 and name 0 and gender 假如說name和gender的值都不為null的話列印的sql為 select from user wherename xx and ...

mybatis動態sql中的trim標籤的使用

trim標記是乙個格式化的標記,可以完成set或者是where標記的功能,如下 select from user where prefixoverride and or 0 and name if 0 and gender if 假如說name和gender的值都不為null的話列印的sql為 se...