日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mybatis使用IN语句查询

發布時間:2023/12/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis使用IN语句查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

https://blog.csdn.net/u011781521/article/details/79669180

?

?

Mybatis使用IN語句查詢

lfendo?2018-03-23 16:45:03??201525??收藏?140

分類專欄:?MyBatis?文章標簽:?mybatis?JAVA?sql

版權

一、簡介

?

在SQL語法中如果我們想使用in的話直接可以像如下一樣使用:

select * from HealthCoupon where useType in ( '4' , '3' )

但是如果在MyBatis中的使用in的話,像如下去做的話,肯定會報錯:

?

Map<String, Object> selectByUserId(@Param("useType") String useType)<select id="selectByUserId" resultMap="BaseResultMap" parameterType="java.lang.String">select * from HealthCoupon where useType in (#{useType,jdbcType=VARCHAR})</select>
  • ?

  • 其中useType="2,3";這樣的寫法,看似很簡單,但是MyBatis不支持。。但是MyBatis中提供了foreach語句實現IN查詢,foreach語法如下:

    foreach語句中, collection屬性的參數類型可以使:List、數組、map集合 ? collection: 必須跟mapper.java中@Param標簽指定的元素名一樣 ? item: 表示在迭代過程中每一個元素的別名,可以隨便起名,但是必須跟元素中的#{}里面的名稱一樣。index:表示在迭代過程中每次迭代到的位置(下標)open:前綴, sql語句中集合都必須用小括號()括起來 ? close:后綴separator:分隔符,表示迭代時每個元素之間以什么分隔
  • ?

  • 正確的寫法有以下幾種寫法:

    ?

    (一)、selectByIdSet(List idList)

    ?

    如果參數的類型是List, 則在使用時,collection屬性要必須指定為 list

    ?

    List<User> selectByIdSet(List idList);<select id="selectByIdSet" resultMap="BaseResultMap">SELECT<include refid="Base_Column_List" />from t_userWHERE id IN<foreach collection="list" item="id" index="index" open="(" close=")" separator=",">#{id}</foreach> </select>
  • ?


  • (二)、List<User> selectByIdSet(String[] idList)

    ?

    如果參數的類型是Array,則在使用時,collection屬性要必須指定為 array

    List<User> selectByIdSet(String[] idList);<select id="selectByIdSet" resultMap="BaseResultMap">SELECT<include refid="Base_Column_List" />from t_userWHERE id IN<foreach collection="array" item="id" index="index" open="(" close=")" separator=",">#{id}</foreach> </select>
  • ?


  • (三)、參數有多個時

    ?

    當查詢的參數有多個時,有兩種方式可以實現,一種是使用@Param("xxx")進行參數綁定,另一種可以通過Map來傳參數。

    ?

    3.1?@Param("xxx")方式

    List<User> selectByIdSet(@Param("name")String name, @Param("ids")String[] idList);<select id="selectByIdSet" resultMap="BaseResultMap">SELECT<include refid="Base_Column_List" />from t_userWHERE name=#{name,jdbcType=VARCHAR} and id IN<foreach collection="idList" item="id" index="index"open="(" close=")" separator=",">#{id}</foreach> </select>
  • ?

  • ?

    3.2 Map方式

    Map<String, Object> params = new HashMap<String, Object>(2); params.put("name", name); params.put("idList", ids); mapper.selectByIdSet(params);<select id="selectByIdSet" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from t_user where name = #{name}and ID in <foreach item="item" index="index" collection="idList" open="(" separator="," close=")"> #{item} </foreach> </select>
  • ?

    • 點贊43
    • 評論

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    總結

    以上是生活随笔為你收集整理的Mybatis使用IN语句查询的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。