欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis中使用not?in與?in的寫法說明

 更新時(shí)間:2022年01月12日 10:06:08   作者:飛翔的丘八  
這篇文章主要介紹了mybatis中使用not?in與?in的寫法說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

使用not in與 in的寫法

首先聲明我不是很喜歡用foreach,所以我的代碼中很少出現(xiàn)foreach。不廢話了,上代碼:

in的用法

我的id是Long類型的

service方法,有一個(gè)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);
? ? }

實(shí)體類.xml

select * from xxx where?
<if test="ids != null"> FIND_IN_SET(id,#{ids}) ? </if>

not in的用法

service方法,有一個(gè)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);
? ? }

實(shí)體類.xml

select * from xxx where?
<if test="notids != null"> NOT FIND_IN_SET(id,#{notids}) ? </if>

使用in查詢時(shí)的注意事項(xiàng)

當(dāng)查詢的參數(shù)只有一個(gè)時(shí) 

findByIds(List<Long> ids)

a 如果參數(shù)的類型是List, 則在使用時(shí),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,則在使用時(shí),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ù)有多個(gè)時(shí)

例如 findByIds(String name, Long[] ids) 

這種情況需要特別注意,在傳參數(shù)時(shí),一定要改用Map方式, 這樣在collection屬性可以指定名稱 

下面是一個(gè)示例

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

完整的示例如下:

例如有一個(gè)查詢功能,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>?

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

  • Mybatis之類型處理器TypeHandler的作用與自定義方式

    Mybatis之類型處理器TypeHandler的作用與自定義方式

    這篇文章主要介紹了Mybatis之類型處理器TypeHandler的作用與自定義方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 淺談synchronized加鎖this和class的區(qū)別

    淺談synchronized加鎖this和class的區(qū)別

    synchronized 是 Java 語言中處理并發(fā)問題的一種常用手段,本文主要介紹了synchronized加鎖this和class的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-11-11
  • RocketMQ消息發(fā)送流程源碼剖析

    RocketMQ消息發(fā)送流程源碼剖析

    這篇文章主要為大家介紹了RocketMQ消息發(fā)送流程源碼剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Java客戶端調(diào)用.NET的WebService實(shí)例

    Java客戶端調(diào)用.NET的WebService實(shí)例

    下面小編就為大家?guī)硪黄狫ava客戶端調(diào)用.NET的WebService實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • Java 使用Calendar計(jì)算時(shí)間的示例代碼

    Java 使用Calendar計(jì)算時(shí)間的示例代碼

    這篇文章主要介紹了Java 使用Calendar計(jì)算時(shí)間的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Java實(shí)現(xiàn)控制臺輸出兩點(diǎn)間距離

    Java實(shí)現(xiàn)控制臺輸出兩點(diǎn)間距離

    這篇文章主要介紹了Java實(shí)現(xiàn)控制臺輸出兩點(diǎn)間距離,涉及了部分編程坐標(biāo)的問題,具有一定參考價(jià)值,需要的朋友可以了解下
    2017-09-09
  • Maven如何修改打包文件名稱

    Maven如何修改打包文件名稱

    這篇文章主要介紹了Maven如何修改打包文件名稱問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Spring中基于xml配置管理Bean的步驟

    Spring中基于xml配置管理Bean的步驟

    Spring容器通常理解為BeanFactory或者ApplicationContext,我們知道spring的IOC容器能夠幫我們創(chuàng)建對象,對象交給spring管理之后我們就不用手動去new對象,這篇文章主要介紹了Spring中基于xml配置管理Bean的步驟,需要的朋友可以參考下
    2023-11-11
  • Java發(fā)送郵件javax.mail的實(shí)現(xiàn)方法

    Java發(fā)送郵件javax.mail的實(shí)現(xiàn)方法

    這篇文章主要為大家介紹了Java發(fā)送郵件javax.mail的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,代碼都有詳細(xì)的注釋,感興趣的小伙伴們可以參考一下
    2016-01-01
  • SWT(JFace) Wizard(Eclipse插件編程必備)

    SWT(JFace) Wizard(Eclipse插件編程必備)

    SWT(JFace)小制作:Wizard(Eclipse插件編程必備)
    2009-06-06

最新評論