MyBatis使用<foreach>標(biāo)簽like查詢報(bào)錯(cuò)解決問題
動(dòng)態(tài)SQL使用可查看官方文檔:
一、foreach標(biāo)簽 IN查詢
<foreach>標(biāo)簽使用場(chǎng)景最多的就是在構(gòu)建 IN 條件語句的時(shí)候進(jìn)行遍歷集合。
實(shí)例如下:
List<UserDO> getByIds(@Param("ids") List<Long> ids);
<select id="getByIds" resultMap="BaseResultMap">
SELECT * FROM t_user
WHERE id IN
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
單元測(cè)試:訪問是OK。
@Test
public void testGetByIds() {
List<Long> ids = Arrays.asList(1L, 2L, 3L);
List<UserDO> userDOList = userMapper.getByIds(ids);
System.out.println(userDOList);
}
二、foreach標(biāo)簽 like查詢
按照上面的使用,如果有個(gè)業(yè)務(wù)需要用到 LIKE遍歷查詢時(shí)。
我們通過在<foreach>標(biāo)簽中構(gòu)建 LIKE 條件語句進(jìn)行遍歷集合時(shí),竟然出錯(cuò)了
實(shí)例代碼:
List<UserDO> getLikeByUsername(@Param("userNameList") List<String> userNameList);
<select id="getLikeByUsername" resultMap="BaseResultMap">
SELECT * FROM t_user
WHERE
<foreach collection="userNameList" item="userName" separator="OR" open="(" close=")">
user_name LIKE concat('%', #{userName}, '%')
</foreach>
</select>
單元測(cè)試:
@Test
public void testGetLikeByUsername() {
List<String> userNameList = Arrays.asList("趙", "后", "趙云");
List<UserDO> userDOList = userMapper.getLikeByUsername(userNameList);
System.out.println(userDOList);
}
報(bào)錯(cuò)信息如下:

在 foreach標(biāo)簽中取值出的錯(cuò),網(wǎng)上查閱資料說是因?yàn)?parameterType接收的參數(shù)不是List導(dǎo)致的,具體情況未核實(shí)。
解決方案
解決方法比較簡(jiǎn)單,或一種取值方式即可,將 foreach標(biāo)簽中遍歷出來的值換如下方式獲取。
user_name LIKE concat('%',concat(#{userName},'%')) -- 個(gè)人比較推薦使用
user_name LIKE concat(‘%', #{userNameList[${idx}]}, ‘%')SQL語句如下:
<select id="getLikeByUsername" resultMap="BaseResultMap">
SELECT * FROM t_user
WHERE
<foreach collection="userNameList" item="userName" separator="OR" open="(" close=")" index="idx">
user_name LIKE concat('%',concat(#{userName},'%'))
-- user_name LIKE concat('%', #{userNameList[${idx}]}, '%')
</foreach>
</select>
如果你想使用注解方式的話,mapper如下:
@Select({"<script>",
"SELECT * FROM t_user\n" +
" WHERE\n" +
" <foreach collection=\"userNameList\" item=\"userName\" separator=\"OR\" open=\"(\" close=\")\">\n" +
" user_name LIKE concat('%',concat(#{userName},'%'))\n" +
" </foreach>",
"</script>"})
List<UserDO> getLikeByUsername2(@Param("userNameList") List<String> userNameList);
再次訪問單元測(cè)試OK。

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA2020 1.1中Plugins加載不出來的問題及解決方法
這篇文章主要介紹了IDEA2020 1.1中Plugins加載不出來的問題,本文還給大家提到了IDEA 2020.1.1 找不到程序包和符號(hào)的問題,感興趣的朋友跟隨小編一起看看吧2020-06-06
JavaGUI界面實(shí)現(xiàn)頁面跳轉(zhuǎn)方法
這篇文章主要給大家介紹了關(guān)于JavaGUI界面實(shí)現(xiàn)頁面跳轉(zhuǎn)的相關(guān)資料, GUI是指圖形用戶界面,指采用圖形方式顯示的計(jì)算機(jī)操作用戶界面,需要的朋友可以參考下2023-07-07
基于dubbo中Listener的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄赿ubbo中Listener的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
SpringBoot實(shí)現(xiàn)六邊形架構(gòu)的三種不同方式詳解
六邊形架構(gòu),也被稱為端口與適配器架構(gòu)或洋蔥架構(gòu),是一種將業(yè)務(wù)邏輯與外部依賴解耦的架構(gòu)模式,本文將介紹在SpringBoot中實(shí)現(xiàn)六邊形架構(gòu)的三種不同方式2025-06-06
MyBatis-Plus 默認(rèn)不更新null的4種方法
本文主要介紹了MyBatis-Plus 默認(rèn)不更新null的4種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2026-01-01
IntelliJ IDEA中設(shè)置數(shù)據(jù)庫連接全局共享的步驟詳解
本文詳解IntelliJ IDEA數(shù)據(jù)庫連接全局共享設(shè)置,通過創(chuàng)建/選擇數(shù)據(jù)源并設(shè)為全局,實(shí)現(xiàn)多項(xiàng)目間統(tǒng)一連接,提升效率、減少配置錯(cuò)誤,感興趣的可以了解一下2025-07-07

