Mybatis使用IN語(yǔ)句查詢(xún)的實(shí)現(xiàn)
在SQL語(yǔ)法中如果我們想使用in的話(huà)直接可以像如下一樣使用:
select * from HealthCoupon where useType in ( ’4’ , ’3’ )
但是如果在MyBatis中的使用in的話(huà),像如下去做的話(huà),肯定會(huì)報(bào)錯(cuò):
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';這樣的寫(xiě)法,看似很簡(jiǎn)單,但是MyBatis不支持。。但是MyBatis中提供了foreach語(yǔ)句實(shí)現(xiàn)IN查詢(xún),foreach語(yǔ)法如下:
foreach語(yǔ)句中, collection屬性的參數(shù)類(lèi)型可以使:List、數(shù)組、map集合
collection: 必須跟mapper.java中@Param標(biāo)簽指定的元素名一樣 item: 表示在迭代過(guò)程中每一個(gè)元素的別名,可以隨便起名,但是必須跟元素中的#{}里面的名稱(chēng)一樣。 index:表示在迭代過(guò)程中每次迭代到的位置(下標(biāo)) open:前綴, sql語(yǔ)句中集合都必須用小括號(hào)()括起來(lái) close:后綴 separator:分隔符,表示迭代時(shí)每個(gè)元素之間以什么分隔正確的寫(xiě)法有以下幾種寫(xiě)法:
(一)、selectByIdSet(List idList)如果參數(shù)的類(lèi)型是List, 則在使用時(shí),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ù)的類(lèi)型是Array,則在使用時(shí),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ù)有多個(gè)時(shí)
當(dāng)查詢(xún)的參數(shù)有多個(gè)時(shí),有兩種方式可以實(shí)現(xiàn),一種是使用@Param('xxx')進(jìn)行參數(shù)綁定,另一種可以通過(guò)Map來(lái)傳參數(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>
到此這篇關(guān)于Mybatis使用IN語(yǔ)句查詢(xún)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Mybatis IN語(yǔ)句查詢(xún)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 關(guān)于DB2 9數(shù)據(jù)庫(kù)優(yōu)點(diǎn)及缺點(diǎn)詳細(xì)分析2. 快速刪除ORACLE重復(fù)記錄3. Microsoft Office Access隱藏和顯示字段的方法4. mysql啟動(dòng)時(shí)報(bào)錯(cuò) ERROR! Manager of pid-file quit without5. Sql Server 2000數(shù)據(jù)庫(kù)日志日益龐大的解決方法6. Docker部署Mysql集群的實(shí)現(xiàn)7. Windows下不能啟動(dòng)mysql服務(wù)--錯(cuò)誤總結(jié)8. mysql-bin.000001文件的來(lái)源及處理方法9. MySQL decimal unsigned更新負(fù)數(shù)轉(zhuǎn)化為010. 用最簡(jiǎn)單的方法復(fù)制或遷移Oracle數(shù)據(jù)庫(kù)
