MyBatis中的模糊查詢

2022-08-28 02:12:11 字數 1127 閱讀 9101

在dml中 , 模糊查詢用 like '%' 實現 , 那麼在mybatis中怎麼去實現呢?

一頓操作猛如虎 , 敲出以下** :

1

<

select

id='test'

>

2select3*

4from

5test

6where

7test_name like %%

8select

>

直接 error 1064 : 缺少單引號 ...

其實很簡單 , 只需要用mysql的函式 --->concat

1

<

select

id='test'

>

2select3*

4from

5test

6where

7test_name like concat('%',#,'%')

8select

>

這樣就可以直接在xml檔案寫  '%' 了~

擴充套件一波知識 :     concat   函式

concat(str1,str2,…)  

返回結果為連線引數產生的字串。如有任何乙個引數為null ,則返回值為 null。或許有乙個或多個引數。

如果所有引數均為非二進位制字串,則結果為非二進位制字串。

如果自變數中含有任一二進位制字串,則結果為乙個二進位制字串。乙個數字引數被轉化為與之相等的二進位制字串格式;

若要避免這種情況,可使用顯式型別 cast, 例如: select concat(cast(int_col as char), char_col)

# 與 $

#是預編譯處理,mybatis在處理#時,它會將sql中的#替換為?,然後呼叫preparedstatement的set方法來賦值,傳入字串後,會在值兩邊加上單引號,使用佔位符的方式提高效率,可以防止sql注入。

${}:表示拼接sql串,將接收到引數的內容不加任何修飾拼接在sql中,可能引發sql注入。

Mybatis中的模糊查詢

1.當我們從資料庫中查詢資料時,大批量的資料會存在相同的資料。比如重名的人,當我們使用姓名查詢該姓名的所有資料時,我們需要在mybatis中使用到模糊查詢的概念。在介面中定義函式 模糊查詢 使用name查詢的資料為物件tb7,返回的不止乙個物件使用list public listquerybynam...

mybatis中LIKE模糊查詢

mybatis中對於使用like來進行模糊查詢的幾種方式 使用 由於 是引數直接注入的,導致這種寫法,大括號裡面不能註明jdbctype,不然會報錯org.mybatis.spring.mybatissystemexception nested exception is org.apache.iba...

Mybatis 模糊查詢

mybatis從入門到精通 書籍筆記 1 使用concat 字串連線函式and user name like concat and user name like concat concat mysql中concat函式可以連線多個引數,oracle中只支援2個引數,所以有些要用多個concat 函式...