最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis傳入?yún)?shù)為L(zhǎng)ist對(duì)象的實(shí)現(xiàn)

 更新時(shí)間:2021年03月05日 16:05:28   作者:靜水流深  
這篇文章主要介紹了MyBatis傳入?yún)?shù)為L(zhǎng)ist對(duì)象的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

SSM框架是JavaWeb必學(xué)的框架,雖說(shuō)基本的增刪改查很簡(jiǎn)單,但是當(dāng)面臨一些特殊情況時(shí),有時(shí)還是會(huì)顯得手足無(wú)措,此篇用來(lái)記錄一些特殊場(chǎng)景下Mybatis框架的應(yīng)用.

傳入?yún)?shù)為L(zhǎng)ist對(duì)象

1. 場(chǎng)景復(fù)現(xiàn)

首先有如下一張表:

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

如果要根據(jù)resource_id和type來(lái)批量查詢(xún)記錄,該如何編寫(xiě)Mybatis語(yǔ)句?

2. 解決方案

直接貼出來(lái)解決方案如下所示:

Dao層接口:

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

XML語(yǔ)句:

<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>

該語(yǔ)句利用了mybatis的foreach動(dòng)態(tài)拼接SQL。

3. foreach屬性

屬性 描述
item 循環(huán)體中的具體對(duì)象。支持屬性的點(diǎn)路徑訪(fǎng)問(wèn),如item.age,item.info.details。具體說(shuō)明:在list和數(shù)組中是其中的對(duì)象,在map中是value。該參數(shù)為必選。
collection 要做foreach的對(duì)象,作為入?yún)r(shí),List<?>對(duì)象默認(rèn)用list代替作為鍵,數(shù)組對(duì)象有array代替作為鍵,Map對(duì)象用map代替作為鍵。當(dāng)然在作為入?yún)r(shí)可以使用@Param("keyName")來(lái)設(shè)置鍵,設(shè)置keyName后,list,array,map將會(huì)失效。 除了入?yún)⑦@種情況外,還有一種作為參數(shù)對(duì)象的某個(gè)字段的時(shí)候。舉個(gè)例子:如果User有屬性L(fǎng)ist ids。入?yún)⑹荱ser對(duì)象,那么這個(gè)collection = "ids"如果User有屬性Ids ids;其中Ids是個(gè)對(duì)象,Ids有個(gè)屬性L(fǎng)ist id;入?yún)⑹荱ser對(duì)象,那么collection = "ids.id"上面只是舉例,具體collection等于什么,就看你想對(duì)那個(gè)元素做循環(huán)。該參數(shù)為必選。
separator 元素之間的分隔符,例如在in()的時(shí)候,separator=","會(huì)自動(dòng)在元素中間用“,“隔開(kāi),避免手動(dòng)輸入逗號(hào)導(dǎo)致sql錯(cuò)誤,如in(1,2,)這樣。該參數(shù)可選。
open foreach代碼的開(kāi)始符號(hào),一般是(和close=")"合用。常用在in(),values()時(shí)。該參數(shù)可選。
close foreach代碼的關(guān)閉符號(hào),一般是)和open="("合用。常用在in(),values()時(shí)。該參數(shù)可選。
index 在list和數(shù)組中,index是元素的序號(hào),在map中,index是元素的key,該參數(shù)可選。

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>

5. 參考文獻(xiàn)

https://www.cnblogs.com/dflmg/p/6398033.html

http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html

到此這篇關(guān)于MyBatis傳入?yún)?shù)為L(zhǎng)ist對(duì)象的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis傳入?yún)?shù)為L(zhǎng)ist對(duì)象內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

离岛区| 柳河县| 南城县| 吴桥县| 乐昌市| 灵台县| 安平县| 沁源县| 东山县| 丰城市| 长乐市| 齐齐哈尔市| 东山县| 项城市| 分宜县| 同江市| 屯留县| 玛曲县| 嘉鱼县| 南城县| 鸡东县| 游戏| 若羌县| 嘉黎县| 新郑市| 徐汇区| 泊头市| 麻江县| 兴和县| 新闻| 邻水| 东至县| 沧源| 泌阳县| 游戏| 尚志市| 彰化县| 阿城市| 凌云县| 青海省| 营口市|