mybatis 查詢sql中in條件用法詳解(foreach)
foreach屬性主要有item,index,collection,open,separator,close
1、item表示集合中每一個元素進(jìn)行迭代時的別名,
2、index指定一個名字,用于表示在迭代過程中,每次迭代到的位置,
3、open表示該語句以什么開始,
4、separator表示在每次進(jìn)行迭代之間以什么符號作為分隔符,
5、close表示以什么結(jié)束,
6、collection屬性,該屬性是必須指定的,但是在不同情況下,該屬性的值是不一樣的,
主要有一下3種情況:
a、如果傳入的是單參數(shù)且參數(shù)類型是一個List的時候,collection屬性值為list .
b、如果傳入的是單參數(shù)且參數(shù)類型是一個array數(shù)組的時候,collection的屬性值為array .
c、如果傳入的參數(shù)是多個的時候,我們就需要把它們封裝成一個Map了,當(dāng)然單參數(shù)也可以封裝成map,實際上如果你在傳入?yún)?shù)的時候,在MyBatis里面也是會把它封裝成一個Map的,map的key就是參數(shù)名,所以這個時候collection屬性值就是傳入的List或array對象在自己封裝的map里面的key.
<select id="findBy" resultMap="RfCustomerMemMap" parameterType="java.util.Map">
SELECT
<include refid="Column"/>
FROM rfl_customer_mem a LEFT JOIN rfl_loan b ON a.member_no = b.loan_member_no
WHERE a.member_no = #{memberNo} AND b.status IN
<foreach collection="status" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
<if test="name != null and name != ''">
AND name = #{name}
</if>
<if test="idNumber != null and idNumber != ''">
AND id_number = #{idNumber}
</if>
<if test="mobileNo != null and mobileNo != ''">
AND mobile_no = #{mobileNo}
</if>
<if test="loanNo != null and loanNo != ''">
AND loan_no = #{loanNo}
</if>
order by a.id DESC
<if test="offset > -1 and rows > -1">
limit #{offset},#{limit}
</if>
</select>
java調(diào)用查詢sql代碼
public List<LoanMerchantMemEntity> findMerchantMemBy(String merchantName, String merchantNo, String socialCreditCode, String loanNo, int offset, int limit) {
List<LoanMerchantMemEntity> list = new ArrayList<LoanMerchantMemEntity>();
Map<String, Object> filter = new HashMap<String, Object>();
filter.put("merchantName", merchantName);
filter.put("socialCreditCode", socialCreditCode);
filter.put("status", statsList());
filter.put("loanNo", loanNo);
filter.put("offset", offset);
filter.put("limit", limit);
filter.put("merchantNo", merchantNo);
try {
List<LoanMerchantMemEntity> row = loanMerchantMemDao.findBy(filter);
} catch (Exception e) {
LOGGER.error(filter, "查詢企業(yè)會員信息異常", e);
}
return list;
}
static List<String> statsList(){
List<String> statusList = new ArrayList<String>();
statusList.add("SUCCESS");
statusList.add("DUE");
statusList.add("OVER");
return statusList;
}
其中,map中key為status值類型為list,這種使用場景為第三種,即collection為map中的key值
補充:當(dāng)傳入一個String數(shù)組后,在sql中使用foreach語句實現(xiàn)IN查詢
當(dāng)我們從前臺傳遞過來的是一個數(shù)組是,后臺我們要進(jìn)行處理, 因為在數(shù)據(jù)庫中表的字段類型有可能是num 或者varchar;
我這里傳過來的是Map 當(dāng)然也可以使用request.getparameter("name") 這個name為jsp或者h(yuǎn)tm頁面中的id所對應(yīng)的name,
下面代碼中也是的:
String name=(String) params.get("name");
String[] hiddens = name.split(",");
params.put("name", hiddens);
當(dāng)我們經(jīng)過這一部分的處理后,數(shù)據(jù)就存入到map中了,傳入?yún)?shù)后進(jìn)行查詢
AND 條件 in
<foreach collection="name" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
在sql上面,我們進(jìn)行查詢的時候就OK啦!
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
SpringBoot封裝響應(yīng)處理超詳細(xì)講解
這篇文章主要介紹了SpringBoot封裝響應(yīng)處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12
分布式醫(yī)療掛號系統(tǒng)SpringCache與Redis為數(shù)據(jù)字典添加緩存
這篇文章主要為大家介紹了分布式醫(yī)療掛號系統(tǒng)SpringCache與Redis為數(shù)據(jù)字典添加緩存,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
Java實現(xiàn)將漢字轉(zhuǎn)化為漢語拼音的方法
這篇文章主要介紹了Java實現(xiàn)將漢字轉(zhuǎn)化為漢語拼音的方法,實例演示了Java引用pinyin4j庫實現(xiàn)漢子轉(zhuǎn)化成拼音的使用技巧,需要的朋友可以參考下2015-12-12
Java?中?Class?Path?和?Package的使用詳解
這篇文章主要介紹了Java?中?Class?Path和Package的使用詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08

