溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MyBatis傳入參數為List對象的實現方法

發布時間:2021-03-06 10:28:23 來源:億速云 閱讀:323 作者:小新 欄目:開發技術

這篇文章主要介紹MyBatis傳入參數為List對象的實現方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

SSM框架是JavaWeb必學的框架,雖說基本的增刪改查很簡單,但是當面臨一些特殊情況時,有時還是會顯得手足無措,此篇用來記錄一些特殊場景下Mybatis框架的應用.

傳入參數為List對象

1. 場景復現

首先有如下一張表:

MySQL [test]> select * from t_entry_resource;
+----+-------------+------+----------+--------+--------+---------------------+
| id | resource_id | type | title  | banner | icon | add_date      |
+----+-------------+------+----------+--------+--------+---------------------+
| 11 |     6 | 14  | 分類   | 1.jpg | 2.jpg | 2017-11-17 11:22:30 |
| 12 |     3 | 1  | 測試12  | 3.jpg | 4.jpg | 2017-11-17 11:22:30 |
| 13 |    653 | 1  | 測試34  | 5.jpg | 6.jpg | 2017-11-20 02:32:26 |
| 14 |     1 | 1  | 測試5  | 7.jpg | 8.jpg | 2017-11-20 02:32:51 |
| 15 |    3942 | 3  | 測試6  | 9.jpg | 10.jpg | 2017-11-20 02:34:27 |
+----+-------------+------+----------+--------+--------+---------------------+
5 rows in set (0.01 sec)

如果要根據resource_id和type來批量查詢記錄,該如何編寫Mybatis語句?

2. 解決方案

直接貼出來解決方案如下所示:

Dao層接口:

List<EntryResource> findByRidAndType(List<EntryResource> entryResources);

XML語句:

<select id="findByRidAndType" resultMap="entryResource" parameterType="list">
    SELECT
    *
    FROM
    t_entry_resource a
    WHERE
<foreach collection="list" index="index" item="entryResources" open="(" close=")" separator="or">
      ( `type`=#{entryResources.type} and resource_id=#{entryResources.resourceId} )
</foreach>

</select>

該語句利用了mybatis的foreach動態拼接SQL。

3. foreach屬性

屬性描述
item循環體中的具體對象。支持屬性的點路徑訪問,如item.age,item.info.details。具體說明:在list和數組中是其中的對象,在map中是value。該參數為必選。
collection要做foreach的對象,作為入參時,List<?>對象默認用list代替作為鍵,數組對象有array代替作為鍵,Map對象用map代替作為鍵。當然在作為入參時可以使用@Param("keyName")來設置鍵,設置keyName后,list,array,map將會失效。 除了入參這種情況外,還有一種作為參數對象的某個字段的時候。舉個例子:如果User有屬性List ids。入參是User對象,那么這個collection = "ids"如果User有屬性Ids ids;其中Ids是個對象,Ids有個屬性List id;入參是User對象,那么collection = "ids.id"上面只是舉例,具體collection等于什么,就看你想對那個元素做循環。該參數為必選。
separator元素之間的分隔符,例如在in()的時候,separator=","會自動在元素中間用“,“隔開,避免手動輸入逗號導致sql錯誤,如in(1,2,)這樣。該參數可選。
openforeach代碼的開始符號,一般是(和close=")"合用。常用在in(),values()時。該參數可選。
closeforeach代碼的關閉符號,一般是)和open="("合用。常用在in(),values()時。該參數可選。
index在list和數組中,index是元素的序號,在map中,index是元素的key,該參數可選。

4. foreach的幾種用法

(1) select count(*) from users id in (x1,x2,x3,...)

<select id="countByUserList" resultType="int" parameterType="list">  
select count(*) from users  
 <where>  
  id in  
  <foreach item="item" collection="list" separator="," open="(" close=")" index="">  
   #{item.id, jdbcType=NUMERIC}  
  </foreach>  
 </where>  
</select>

(2) select count(*) from key_cols where col_a = ? AND col_b = ?

<select id="sel_key_cols" resultType="int">  
    select count(*) from key_cols where  
<foreach item="item" index="key" collection="map" open="" separator="AND" close="">
    ${key} = #{item}
</foreach>  
</select>

(3) select * from t_news n where n.tags like ? or n.tags like ?

<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
 select * from t_news n where 
 <foreach collection="listTag" index="index" item="tag" open="" separator="or" close="">
      n.tags like '%'||#{tag}||'%'
 </foreach>
<select>

以上是“MyBatis傳入參數為List對象的實現方法”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女