mybatis中使用not?in與?in的寫法說明
使用not in與 in的寫法
首先聲明我不是很喜歡用foreach,所以我的代碼中很少出現(xiàn)foreach。不廢話了,上代碼:
in的用法
我的id是Long類型的
service方法,有一個Long的集合:
public List<RbacMenu> listByPackageId(List<Long> ids, String systemCode) {
? ? Map<String, Object> map = new HashMap<String, Object>();
? ? if(ids.size()!=0) {
? ? ? ? StringBuilder sbd = new StringBuilder();
? ? ? ? for(Long cateIds:ids){
? ? ? ? ? ? sbd.append(cateIds+",");
? ? ? ? }
? ? ? ? String idStr = sbd.toString();
? ? ? ? idStr = idStr.substring(0,idStr.length()-1);
? ? ? ? map.put("ids", idStr);
? ? }實體類.xml
select * from xxx where?
<if test="ids != null"> FIND_IN_SET(id,#{ids}) ? </if>not in的用法
service方法,有一個Long的集合:
public List<RbacMenu> listByPackageId(List<Long> ids, String systemCode) {
? ? Map<String, Object> map = new HashMap<String, Object>();
? ? if(ids.size()!=0) {
? ? ? ? StringBuilder sbd = new StringBuilder();
? ? ? ? for(Long cateIds:ids){
? ? ? ? ? ? sbd.append(cateIds+",");
? ? ? ? }
? ? ? ? String idStr = sbd.toString();
? ? ? ? idStr = idStr.substring(0,idStr.length()-1);
? ? ? ? map.put("notids", idStr);
? ? }實體類.xml
select * from xxx where?
<if test="notids != null"> NOT FIND_IN_SET(id,#{notids}) ? </if>使用in查詢時的注意事項
當(dāng)查詢的參數(shù)只有一個時
findByIds(List<Long> ids)
a 如果參數(shù)的類型是List, 則在使用時,collection屬性要必須指定為 list
?<select id="findByIdsMap" resultMap="BaseResultMap">
? ? ? ? ?Select
? ? ? ? ?<include refid="Base_Column_List" />
? ? ? ? ?from jria where ID in
? ? ? ? ? ? ? ? ? <foreach item="item" index="index" collection="list"?
? ? ? ? ? ? ? ? ? ? ? ? ?open="(" separator="," close=")">
? ? ? ? ? ? ? ? ? ? ? ? #{item}
? ? ? ? ? ? ? ? </foreach>
? </select>??findByIds(Long[] ids)
b 如果參數(shù)的類型是Array,則在使用時,collection屬性要必須指定為 array
? <select id="findByIdsMap" resultMap="BaseResultMap">
? ? ? ? ? ? ? ? ?select
? ? ? ? ? ? ? ? ?<include refid="Base_Column_List" />
? ? ? ? ? from jria where ID in
? ? ? ? ? ? ? ? ? <foreach item="item" index="index" collection="array"?
? ? ? ? ? ? ? ? ? ? ? ? ?open="(" separator="," close=")">
? ? ? ? ? ? ? ? ? ? ? ? #{item}
? ? ? ? ? ? ? ? </foreach>
? </select>當(dāng)查詢的參數(shù)有多個時
例如 findByIds(String name, Long[] ids)
這種情況需要特別注意,在傳參數(shù)時,一定要改用Map方式, 這樣在collection屬性可以指定名稱
下面是一個示例
? ? ? ? ?Map<String, Object> params = new HashMap<String, Object>(2);
? ? ? ? params.put("name", name);
? ? ? ? ?params.put("ids", ids);
? ? ? ? mapper.findByIdsMap(params);
?
?<select id="findByIdsMap" resultMap="BaseResultMap">
? ? ? ? ? ? ? ? ?select
? ? ? ? ? ? ? ? ?<include refid="Base_Column_List" />
? ? ? ? ? from jria where ID in
? ? ? ? ? ? ? ? ? <foreach item="item" index="index" collection="ids"?
? ? ? ? ? ? ? ? ? ? ? ? ?open="(" separator="," close=")">
? ? ? ? ? ? ? ? ? ? ? ? #{item}
? ? ? ? ? ? ? ? </foreach>
? ?</select>?完整的示例如下:
例如有一個查詢功能,Mapper接口文件定義如下方法:
List<Jria> findByIds(Long... ids);
使用 in 查詢的sql拼裝方法如下:
?<select id="findbyIds" resultMap="BaseResultMap">
? ? ? ? ? ? ? ? ?select
? ? ? ? ? ? ? ? ?<include refid="Base_Column_List" />
? ? ? ? ? from jria where ID in
? ? ? ? ? ? ? ? ? <foreach item="item" index="index" collection="array"?
? ? ? ? ? ? ? ? ? ? ? ? ?open="(" separator="," close=")">
? ? ? ? ? ? ? ? ? ? ? ? #{item}
? ? ? ? ? ? ? ? </foreach>
? </select>?以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis之類型處理器TypeHandler的作用與自定義方式
這篇文章主要介紹了Mybatis之類型處理器TypeHandler的作用與自定義方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
淺談synchronized加鎖this和class的區(qū)別
synchronized 是 Java 語言中處理并發(fā)問題的一種常用手段,本文主要介紹了synchronized加鎖this和class的區(qū)別,具有一定的參考價值,感興趣的可以了解一下2021-11-11
Java客戶端調(diào)用.NET的WebService實例
下面小編就為大家?guī)硪黄狫ava客戶端調(diào)用.NET的WebService實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
Java發(fā)送郵件javax.mail的實現(xiàn)方法
這篇文章主要為大家介紹了Java發(fā)送郵件javax.mail的實現(xiàn)方法,具有一定的參考價值,代碼都有詳細的注釋,感興趣的小伙伴們可以參考一下2016-01-01
SWT(JFace) Wizard(Eclipse插件編程必備)
SWT(JFace)小制作:Wizard(Eclipse插件編程必備)2009-06-06

