最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis中foreach的使用詳解

 更新時間:2024年11月20日 09:32:24   作者:星空尋流年  
Mybatis中foreach標(biāo)簽的使用詳解,包括屬性說明、代碼示例和總結(jié),感興趣的朋友跟隨小編一起看看吧

一、foreach 屬性使用

<foreach collection="list" index="index" item="mchntCd" open="(" close=")" separator=",">
   #{mchntCd}
</foreach>
  • item:  集合中元素迭代時的別名,該參數(shù)為必選,通過別名進(jìn)行取值
  • index:在list和數(shù)組中,index是元素的序號,在map中,index是元素的key,非必填
  • open: foreach代碼的開始符號,一般是(和close=")"合用。常用在in(),values()時。非必填
  • separator:元素之間的分隔符,例如在in()的時候,separator=","會自動在元素中間用“,“隔開,避免手動輸入逗號導(dǎo)致sql錯誤,如in(1,2,)這樣。非必填
  • close: foreach代碼的關(guān)閉符號,一般是)和open="("合用。常用在in(),values()時。非必填
  • collection: 要做foreach的對象,作為入?yún)?ul>
  • 傳入是集合,也就是接口里面用的  List<String> nameList    那么 使用 collection = “list”
  • 傳入的是數(shù)組,接口里面 String[] namestrs ,那么 使用 collection = “array”
  • 如果傳入的是一個實(shí)體bean,實(shí)體bean里面有一個屬性為 list<String> ids 那么collection = “ids ”,如果實(shí)體bean中對應(yīng)ids是一個對象,ids 里面有一個屬性為 List<Stirng> usernames,那么collection = “ids.usernames”

二、代碼使用         

1、實(shí)體類 list<String>  mchntCds

 mapper接口  ---》 list方式

int queryDiscDerateCount(List<String> mchntCds);

mapper映射xml:

<select id="queryDiscDerateCount"  resultType="Integer">
   select count(*) from t_mchnt_disc_config  where mchnt_cd in
   <foreach collection="list" index="index" item="mchntCd" open="(" close=")"  separator=",">
      #{mchntCd}
   </foreach>
</select>

2、實(shí)體類 list<User>  userlist,實(shí)體類中有 list 屬性

實(shí)體類:

@Data
public class MchntDiscDerateDto {
   private String mchntCd = "";
}

mapper接口

List<TMchntDerateInfoDto> getDiscDerateList(List<MchntDiscDerateDto> discDto);

mapper映射xml:

<select id="getDiscDerateList" parameterType="MchntDiscDerateDto" resultType="TMchntDerateInfoDto">
   select
    t_mchnt_disc_derate_config
      where
         mchnt_cd in
         <foreach collection="list" index="index" item="user" open="(" close=")" separator=",">
            #{user.mchntCd}
         </foreach>
</select>

3、數(shù)組  String[] params     

用 array 

mapper 接口

int queryDiscDerateCount(String[] mchntCds);

mapper映射xml:

<select id="queryDiscDerateCount"  resultType="Integer">
   select count(*) from t_mchnt_disc_derate_config where mchnt_cd in
   <foreach collection="array" index="index" item="mchntCd" open="(" close=")" separator=",">
      #{mchntCd}
   </foreach>
</select>

4、傳入的參數(shù)是實(shí)體類,并且實(shí)體中包含數(shù)組和集合

實(shí)體類:

@Data
public class UserVo {
    private Long id;
    private Long supplierId;
    private Long[] ids;
    private List<Long> clientIdList;
}

mapper接口

List<UserVo> queryList(UserVo select);

mapper映射文件xml

    <select id="queryList" resultType="UserVo" parameterType="UserVo">
        select *
        from bms_bills_memo
        <where>
        and id in
        <foreach collection="ids" open="(" close=")" item="id" separator=",">
            #{id}
        </foreach>
        and
        client_id in
        <foreach collection="clientIdList" separator="," item="detail" open="(" close=")" >
            #{detail}
        </foreach>
        </where>
    </select>

5、傳入多個list或者array,不使用實(shí)體進(jìn)行封裝。用注解@Params, collection使用到Param中定義的別名

mapper接口

List<UserVo> queryList(@Param("idArray") Long[] array, @Param("clientIdList") List<Long> list);

mapper映射文件xml

    <select id="queryList" resultType="UserVo">
        select *
        from t_user_inf
        <where>
            and id in
            <foreach collection="idArray" open="(" close=")" item="id" separator=",">
                #{id}
            </foreach>
            and
            client_id in
            <foreach collection="clientIdList" separator="," item="detail" open="(" close=")" >
                #{detail}
            </foreach>
        </where>
    </select>

6、map參數(shù)

當(dāng)我們傳入的參數(shù)為 Map<String,Objject>的時候,那么能不能正常使用 foreach呢,答案是肯定的,因?yàn)閷ο笠彩穷愃朴趍ap結(jié)構(gòu)啊

    /**
     *  map 獲取數(shù)據(jù)
     * @param map
     * @return
     */
    List<SysUser> getUserByIds(Map<String,Object> map);
 
 
    // xml
    <select id="getUserByIds" resultMap="BaseResultMap">
    select
        <include refid="Base_Column_List" />
    from t_sys_user
    where status = #{status}
    and id in
    <foreach collection="ids" item="id" open="(" close=")" separator=",">
        #{id}
    </foreach>
  </select>

實(shí)際調(diào)用:

     @Test
    public void testMapUsers() {
        Map<String,Object> params = new HashMap<>();
        params.put("status", "1");
        params.put("ids", Arrays.asList(1,2,3,4,5,6,7,8));
        List<SysUser> all = sysUserDao.getUserByIds(params);
        try {
            System.out.println(new JsonMapper().writeValueAsString(all));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

調(diào)用結(jié)果:

三、總結(jié)

    1、mapper 接口中添加 @Param注解的場合,list,array將會失效;

    2、使用了 @Param注解 collection的內(nèi)容需要使用Param中的別名來指定你需要用到的list或者array

擴(kuò)展:Mybatis中foreach的使用

首先我們要明白的是foreach的本質(zhì)就是把數(shù)據(jù)庫能執(zhí)行的sql在xml中按照一定語法來進(jìn)行拼接,所以拼接之前,我們了解一下foreach標(biāo)簽中幾個常見元素的作用
1.collection
‌List或Array‌:如果傳入的參數(shù)類型是List或Array,collection屬性的默認(rèn)值分別是list和array。如果需要自定義集合名稱。
‌Map‌:如果傳入的參數(shù)是Map,collection屬性可以指定遍歷Map的keys、values或entrySet
2.item
集合遍歷中每一個元素的別名
3.open
拼接sql時最前面拼接的字符串
4.separator
拼接sql時候兩個元素之間的分隔字符串
5.close
拼接sql時最后面拼接的字符串
6.index
index‌:在List或Array中,index為元素的序號索引;在Map中,index為遍歷元素的key值。
舉一個簡單的例子
一個簡單的sql

select * from blog where title is not null and (id=1 or id=2 or id=3)

1.我們使用map集合作為參數(shù)實(shí)現(xiàn)拼接

<select id="queryBlogForeach" parameterType="map" resultType="blog"> select * from blog <where> title is not null <foreach collection="ids" item="id" open="and (" separator="or" close=")"> id=#{id} </foreach> </where> </select>

2.我們使用list集合作為參數(shù)實(shí)現(xiàn)拼接

<select id="queryBlogForeach2" parameterType="list" resultType="blog"> select * from blog <where> title is not null <foreach collection="list" item="id" open="and (" separator="or" close=")"> id=#{id} </foreach> </where> </select>

到此這篇關(guān)于Mybatis中foreach的使用的文章就介紹到這了,更多相關(guān)Mybatis foreach使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 關(guān)于Java虛擬機(jī)HotSpot

    關(guān)于Java虛擬機(jī)HotSpot

    這篇文章主要介紹了關(guān)于Java虛擬機(jī)HotSpot,在Java類中的一些方法會被由C/C++編寫的HotSpot虛擬機(jī)的C/C++函數(shù)調(diào)用,不過由于Java方法與C/C++函數(shù)的調(diào)用約定不同,所以并不能直接調(diào)用,需要JavaCalls::call()這個函數(shù)輔助調(diào)用,下面我們來看看文章對內(nèi)容的具體介紹
    2021-11-11
  • 基于SpringMVC實(shí)現(xiàn)網(wǎng)頁登錄攔截

    基于SpringMVC實(shí)現(xiàn)網(wǎng)頁登錄攔截

    SpringMVC的處理器攔截器類似于Servlet開發(fā)中的過濾器Filter,用于對處理器進(jìn)行預(yù)處理和后處理。因此,本文將為大家介紹如何通過SpringMVC實(shí)現(xiàn)網(wǎng)頁登錄攔截功能,需要的小伙伴可以了解一下
    2021-12-12
  • SpringMVC的工程搭建步驟實(shí)現(xiàn)

    SpringMVC的工程搭建步驟實(shí)現(xiàn)

    這篇文章主要介紹了SpringMVC的工程搭建步驟實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Java Web實(shí)現(xiàn)登錄頁面驗(yàn)證碼驗(yàn)證功能

    Java Web實(shí)現(xiàn)登錄頁面驗(yàn)證碼驗(yàn)證功能

    這篇文章主要介紹了Java Web登錄頁面驗(yàn)證碼驗(yàn)證功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • Java反射概念與使用實(shí)例代碼

    Java反射概念與使用實(shí)例代碼

    JAVA反射機(jī)制是在運(yùn)行狀態(tài)中,對于任意一個類,都能夠知道這個類的所有屬性和方法,下面這篇文章主要給大家介紹了關(guān)于Java反射概念與使用的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • Java中解壓縮文件的方法詳解(通用)

    Java中解壓縮文件的方法詳解(通用)

    在軟件開發(fā)和數(shù)據(jù)處理領(lǐng)域,文件的解壓縮和壓縮是常見的任務(wù),下面這篇文章主要給大家介紹了關(guān)于Java中解壓縮文件的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-06-06
  • java仿QQ連連看游戲

    java仿QQ連連看游戲

    這篇文章主要為大家詳細(xì)介紹了java仿QQ連連看游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • spring-boot-maven-plugin?配置有啥用

    spring-boot-maven-plugin?配置有啥用

    這篇文章主要介紹了spring-boot-maven-plugin?配置是干啥的,這個是SpringBoot的Maven插件,主要用來打包的,通常打包成jar或者war文件,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • Java面試題之MD5加密的安全性詳解

    Java面試題之MD5加密的安全性詳解

    MD5 是 Message Digest Algorithm 的縮寫,譯為信息摘要算法,它是 Java 語言中使用很廣泛的一種加密算法。本文將通過示例討論下MD5的安全性,感興趣的可以了解一下
    2022-10-10
  • java 中file.encoding的設(shè)置詳解

    java 中file.encoding的設(shè)置詳解

    這篇文章主要介紹了java 中file.encoding的設(shè)置詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04

最新評論

鹰潭市| 平南县| 聂荣县| 大化| 墨玉县| 廉江市| 通辽市| 德兴市| 东乡| 忻城县| 广平县| 桐乡市| 安顺市| 闻喜县| 海城市| 阜康市| 金昌市| 临汾市| 启东市| 元朗区| 河津市| 霍城县| 台湾省| 全州县| 英山县| 汤阴县| 全南县| 阜南县| 芜湖市| 丰镇市| 神农架林区| 南开区| 西和县| 白玉县| 普洱| 会宁县| 纳雍县| 河北区| 上蔡县| 淮南市| 定陶县|