Mybatis foreach 請求引數是物件集合
阿新 • • 發佈:2019-01-06
需求 引數是物件集合
/** * 查詢對應客戶的聯絡人資訊 * @param qeury * @return */ public List<CustomerContactInfoList> getCustomerContactInfoList(List<CustomerContactInfoList> qeury);
對應實現
<select id="getCustomerContactInfoList" parameterType="java.util.List" resultType="com.zhongan.crm.dataobject.extend.CustomerContactInfoList"> select tci.name as "customerName", tcp.name as "name", tcp.phone as "phone" FROM t_customer_info tci join t_contact_person tcp ON tci.id=tcp.customer_info_id where 1=1 <if test="list!=null"> <foreach collection="list" item="item"index="index" open="and (" separator="or " close=")"> <if test="item.customerName!=null and ''!=item.customerName"> tci.name=#{item.customerName} </if> <if test="item.name!=null and ''!=item.name"> and tcp.name=#{item.name} </if><if test="item.phone!=null and ''!=item.phone"> and tcp.phone=#{item.phone} </if> </foreach> </if> </select>
- foreach元素的屬性主要有 item,index,collection,open,separator,close。
- item表示集合中每一個元素進行迭代時的別名,
- index指定一個名字,用於表示在迭代過程中,每次迭代到的位置,
- open表示該語句以什麼開始,
- separator表示在每次進行迭代之間以什麼符號作為分隔符,
- close表示以什麼結束。
在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,但是在不同情況下,該屬性的值是不一樣的,主要有一下3種情況:
1.如果傳入的是單引數且引數型別是一個List的時候,collection屬性值為list
2.如果傳入的是單引數且引數型別是一個array陣列的時候,collection的屬性值為array
- 3.如果傳入的引數是多個的時候,我們就需要把它們封裝成一個Map或者Object。