Mybatis?如何傳入字符串參數(shù),分割并遍歷
如何傳入字符串參數(shù),分割并遍歷
如前臺傳入字符串參數(shù)
String str = "a,b,c,d,e,f";
現(xiàn)需將此參數(shù)作為查詢語句的參數(shù),
Select * from news where id in (${id})使用該語句查詢正常返回結果,但勢必產(chǎn)生sql注入漏洞。
如修改為:
Select * from news where id in (#{id})程序報錯。
正確寫為如下
id in
<foreach collection="str.split(',')" ?item="item" index="index" open="(" separator="," close=")">#{item}</foreach>Mybatis 傳入分割字符串做參數(shù)
需求:更改指定一些客戶的一個字段
設計:傳參兩個(一個需要更改字段,一個客戶id字符串用","隔開)
問題:mybatis中sql語句里條件報錯,原因是用了#{clientIds}傳入sql中是字符串形式
where id in (#{clientIds}) 等于 where id in ("1,2,3,4") 報錯
解決
方法1、客戶id字符串在代碼里分割成list,mybatis中l(wèi)ist遍歷
<foreach collection="clientIdList" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach>方法2、將字符串在mybatis里分割
<foreach collection="clientIds.split(',')" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach>方法3、sql注入,改為where id in (${clientIds})
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot集成Swagger2生成接口文檔的方法示例
我們提供Restful接口的時候,API文檔是尤為的重要,它承載著對接口的定義,描述等,本文主要介紹了SpringBoot集成Swagger2生成接口文檔的方法示例,需要的朋友們下面隨著小編來一起學習學習吧2018-12-12

