av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Mybatis使用IN語句查詢的實現(xiàn)

瀏覽:171日期:2023-10-20 14:34:43
一、簡介

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

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

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

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

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

foreach語句中, collection屬性的參數(shù)類型可以使:List、數(shù)組、map集合

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

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

(一)、selectByIdSet(List idList)

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

List<User> selectByIdSet(List idList); <select resultMap='BaseResultMap'> SELECT <include refid='Base_Column_List' /> from t_user WHERE id IN <foreach collection='list' item='id' index='index' open='(' close=')' separator=','> #{id} </foreach></select>(二)、List<User> selectByIdSet(String[] idList)

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

List<User> selectByIdSet(String[] idList); <select resultMap='BaseResultMap'> SELECT <include refid='Base_Column_List' /> from t_user WHERE id IN <foreach collection='array' item='id' index='index' open='(' close=')' separator=','> #{id} </foreach></select>(三)、參數(shù)有多個時

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

3.1 @Param('xxx')方式

List<User> selectByIdSet(@Param('name')String name, @Param('ids')String[] idList); <select resultMap='BaseResultMap'> SELECT <include refid='Base_Column_List' /> from t_user WHERE 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 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>

到此這篇關于Mybatis使用IN語句查詢的實現(xiàn)的文章就介紹到這了,更多相關Mybatis IN語句查詢內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

相關文章:
主站蜘蛛池模板: 天天拍天天干 | 国产www| 99视频| 欧美一级片在线 | 国产综合视频 | 99热99re6国产在线播放 | 91久久国产综合久久91精品网站 | 免费毛片基地 | 国产精品一区二区三 | 亚洲综合影院 | 97在线观看视频 | 久久精品欧美一区二区三区不卡 | 青青草国产成人av片免费 | 国产超碰在线观看 | 一级片黄色片 | 色窝| 日韩黄网 | 日韩中文字幕在线观看 | 成人三级在线观看 | 激情婷婷综合 | a天堂在线视频 | 日本在线中文 | 91精品国产综合久久久久久 | 国产伦精品一区二区三区照片 | av免费不卡 | 中文字幕在线观看一区 | 亚洲精品一二三区 | 日韩午夜精品 | 艳妇诱春(第5部分)(h) | 少妇一级淫片aaaaaa | 免费中文字幕 | 亚洲免费视频网站 | 91最新网址 | 精品欧美一区二区三区久久久 | 青青草视频污 | 动漫av在线| 99视频网 | 134vcc影院免费观看 | 久久夜色精品国产欧美乱极品 | 婷婷色婷婷 | 国产亚洲欧美在线 |