SpringBoot+MyBatis進(jìn)行XML中循環(huán)處理List參數(shù)的終極指南
重要提醒:使用@Param
注解時,務(wù)必導(dǎo)入正確的包!
import org.apache.ibatis.annotations.Param;
很多開發(fā)者容易錯誤導(dǎo)入Spring的@Param
,導(dǎo)致參數(shù)綁定失?。?/p>
一、為什么需要傳遞List參數(shù)?
最常見的場景是動態(tài)構(gòu)建IN查詢:
SELECT * FROM users WHERE id IN (1, 2, 3, 4)
當(dāng)我們需要根據(jù)前端傳入的多個值查詢時,就需要將List集合作為參數(shù)傳遞給Mapper。
二、基礎(chǔ)版:MyBatis原生List傳參
1. Mapper接口定義(注意@Param導(dǎo)入)
// !?。”仨殞?dǎo)入MyBatis的@Param包?。?! import org.apache.ibatis.annotations.Param; public interface UserMapper { // 使用@Param注解指定參數(shù)名 List<User> selectByIds(@Param("idList") List<Long> idList); }
2. XML映射文件實(shí)現(xiàn)
<select id="selectByIds" resultType="User"> SELECT * FROM users WHERE id IN <foreach collection="idList" item="id" open="(" separator="," close=")"> #{id} </foreach> </select>
核心標(biāo)簽解析:
屬性 | 說明 | 示例值 |
---|---|---|
collection | 傳入的集合參數(shù)名 | idList |
item | 循環(huán)中當(dāng)前元素的別名 | id |
open | 循環(huán)開始前的字符串 | ( |
separator | 元素間的分隔符 | , |
close | 循環(huán)結(jié)束后的字符串 | ) |
3. 實(shí)際生成的SQL
當(dāng)傳入List<Long> ids = Arrays.asList(1L, 2L, 3L)
時:
SELECT * FROM users WHERE id IN (1, 2, 3)
三、避坑重點(diǎn):@Param的正確使用
常見錯誤1:導(dǎo)入錯誤包
// ? 錯誤:導(dǎo)入了Spring的Param包 import org.springframework.data.repository.query.Param; // ? 正確:必須使用MyBatis的Param包 import org.apache.ibatis.annotations.Param;
常見錯誤2:忘記添加@Param注解
// ? 錯誤:缺少@Param注解會導(dǎo)致XML中無法識別參數(shù) List<User> selectByIds(List<Long> idList); // ? 正確:必須添加@Param注解 List<User> selectByIds(@Param("idList") List<Long> idList);
四、MyBatis Plus的優(yōu)雅實(shí)現(xiàn)
1. 使用QueryWrapper(無需XML)
public List<User> getUsersByIds(List<Long> ids) { return userMapper.selectList( new QueryWrapper<User>().in("id", ids) ); }
2. Lambda表達(dá)式寫法(推薦)
public List<User> getUsersByIds(List<Long> ids) { return userMapper.selectList( Wrappers.<User>lambdaQuery() .in(User::getId, ids) ); }
注意:MyBatis Plus的Wrapper方式不需要@Param注解
五、擴(kuò)展應(yīng)用場景
場景1:List處理
// Mapper List<User> selectByNames(@Param("nameList") List<String> nameList); // XML <foreach collection="nameList" item="name" open="(" separator="," close=")"> #{name} </foreach>
場景2:List<實(shí)體對象>
// Mapper List<User> selectByConditions(@Param("userList") List<User> userList); // XML <foreach collection="userList" item="user" separator=" OR "> (name = #{user.name} AND age > #{user.age}) </foreach>
場景3:多List參數(shù)
// Mapper List<User> searchUsers(@Param("ids") List<Long> ids, @Param("names") List<String> names); // XML <where> <if test="ids != null and !ids.isEmpty()"> id IN <foreach collection="ids" item="id" open="(" separator="," close=")"> #{id} </foreach> </if> <if test="names != null and !names.isEmpty()"> AND name IN <foreach collection="names" item="name" open="(" separator="," close=")"> #{name} </foreach> </if> </where>
六、特殊類型處理技巧
1. 枚舉類型處理
// Mapper List<User> selectByStatus(@Param("statusList") List<UserStatus> statusList); // XML <foreach collection="statusList" item="status" open="(" separator="," close=")"> #{status, typeHandler=org.apache.ibatis.type.EnumTypeHandler} </foreach>
2. 日期范圍查詢
// Mapper List<User> selectByDates(@Param("dateList") List<Date> dates); // XML <foreach collection="dateList" item="date" separator=" OR "> create_time BETWEEN #{date} AND DATE_ADD(#{date}, INTERVAL 1 DAY) </foreach>
七、性能優(yōu)化與避坑指南
1. 空集合安全處理
<select id="safeSelect"> SELECT * FROM users <where> <if test="idList != null and !idList.isEmpty()"> id IN <foreach collection="idList" ... /> </if> </where> </select>
2. 大數(shù)據(jù)量分批查詢
// 每500條執(zhí)行一次查詢 public List<User> batchSelect(List<Long> allIds) { List<User> result = new ArrayList<>(); int batchSize = 500; for (int i = 0; i < allIds.size(); i += batchSize) { List<Long> batchIds = allIds.subList(i, Math.min(i + batchSize, allIds.size())); result.addAll(userMapper.selectByIds(batchIds)); } return result; }
3. SQL注入防護(hù)
<!-- 安全寫法:使用#{}預(yù)編譯 --> <foreach collection="names" item="name"> #{name} <!-- 安全 --> </foreach> <!-- 危險(xiǎn)寫法:${}直接拼接 --> <foreach collection="names" item="name"> '${name}' <!-- 存在SQL注入風(fēng)險(xiǎn)! --> </foreach>
八、完整可運(yùn)行示例
Controller
@RestController @RequestMapping("/users") public class UserController { @PostMapping("/by-ids") public List<User> getUsersByIds(@RequestBody List<Long> ids) { return userService.getUsersByIds(ids); } }
Service
@Service @RequiredArgsConstructor public class UserService { private final UserMapper userMapper; public List<User> getUsersByIds(List<Long> ids) { if (ids == null || ids.isEmpty()) { return Collections.emptyList(); } // 超過1000條自動分批 return ids.size() > 1000 ? batchSelect(ids) : userMapper.selectByIds(ids); } }
Mapper XML
<mapper namespace="com.example.mapper.UserMapper"> <select id="selectByIds" resultType="User"> SELECT id, name, email FROM users <where> <if test="idList != null and !idList.isEmpty()"> id IN <foreach collection="idList" item="id" open="(" separator="," close=")"> #{id} </foreach> </if> </where> </select> </mapper>
總結(jié):核心要點(diǎn)回顧
1.必須使用正確的@Param包 import org.apache.ibatis.annotations.Param;
2.XML循環(huán)核心語法
<foreach collection="參數(shù)名" item="元素名" open="開始符" separator="分隔符" close="結(jié)束符"> #{元素名} </foreach>
3.最佳實(shí)踐選擇
- 簡單查詢:MyBatis Plus Wrapper
- 復(fù)雜SQL:MyBatis XML + foreach
- 超大數(shù)據(jù):分批查詢
4.安全防護(hù)
- 始終使用
#{}
防止SQL注入 - 空集合檢查避免全表掃描
5.特殊類型處理
- 枚舉:添加typeHandler
- 日期:指定jdbcType=TIMESTAMP
最后提醒: 當(dāng)遇到參數(shù)綁定問題時,首先檢查@Param導(dǎo)入的包是否正確,這是最常見的錯誤根源!
到此這篇關(guān)于SpringBoot+MyBatis進(jìn)行XML中循環(huán)處理List參數(shù)的終極指南的文章就介紹到這了,更多相關(guān)SpringBoot處理List參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中實(shí)現(xiàn)日期時間字符串轉(zhuǎn)換為Date對象的方法
在 Java 編程中,日期時間的處理是一項(xiàng)常見且重要的任務(wù),無論是數(shù)據(jù)存儲、日志記錄還是業(yè)務(wù)邏輯處理,準(zhǔn)確地表示和操作日期時間都是不可或缺的,本文給大家介紹了Java中實(shí)現(xiàn)日期時間字符串轉(zhuǎn)換為Date對象的方法,需要的朋友可以參考下2025-01-01Spring源碼學(xué)習(xí)之動態(tài)代理實(shí)現(xiàn)流程
這篇文章主要給大家介紹了關(guān)于Spring源碼學(xué)習(xí)之動態(tài)代理實(shí)現(xiàn)流程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Java 實(shí)戰(zhàn)項(xiàng)目錘煉之小區(qū)物業(yè)管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)一個小區(qū)物業(yè)管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11Java程序去調(diào)用并執(zhí)行shell腳本及問題總結(jié)(推薦)
這篇文章主要介紹了Java程序去調(diào)用并執(zhí)行shell腳本及問題總結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06Java8 HashMap的實(shí)現(xiàn)原理分析
Java8之后新增挺多新東西,接下來通過本文給大家介紹Java8 HashMap的實(shí)現(xiàn)原理分析,對java8 hashmap實(shí)現(xiàn)原理相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-03-03