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

Mybatis傳遞List集合方式

 更新時(shí)間:2025年07月23日 15:46:43   作者:jushisi  
MyBatis傳遞List參數(shù)時(shí),若XML變量名不匹配默認(rèn)的"list"鍵,會(huì)報(bào)錯(cuò),需通過(guò)@Param指定名稱、使用collection屬性或索引方式正確引用

第一種

參數(shù)是常規(guī)的List, 但是xml變量名不是list------報(bào)錯(cuò)

完整錯(cuò)誤如下:

org.apache.ibatis.binding.BindingException: Parameter ‘customerIdList’ not found. Available parameters are [collection, list]

解釋:

  • 當(dāng)我們傳遞一個(gè) List 實(shí)例或者數(shù)組作為參數(shù)對(duì)象傳給 MyBatis。
  • 當(dāng)你這么做的時(shí) 候,MyBatis 會(huì)自動(dòng)將它包裝在一個(gè) Map 中,用名稱在作為鍵。
  • List 實(shí)例將會(huì)以“list” 作為鍵,而數(shù)組實(shí)例將會(huì)以“array”作為鍵。
  • 所以,當(dāng)我們傳遞的是一個(gè)List集合時(shí),mybatis會(huì)自動(dòng)把我們的list集合包裝成以list為Key值的map。
DAO 層:
Long selectCustomerCountList(List customerIdList);
 
XML文件:
<select id="selectCustomerCountList" parameterType="java.util.List" resultType="java.lang.Long">
    select count(1) from np_customer_info where id in
    <foreach item="item" collection="customerIdList" separator="," open="(" close=")" index="">
    	#{item, jdbcType=INTEGER}   
    </foreach>
</select>

======================
注意:DAO 層接口的參數(shù)名與XML 文件中的collection的屬性值一致,是導(dǎo)致的問(wèn)題的主要原因。

第二種

參數(shù)是常規(guī)的List, xml變量名是list------正常

  • 利用Mybatis給我們的封裝進(jìn)行XML配置,將我們的XML中collection屬性值設(shè)置為list。
DAO 層:
Long selectCustomerCountList( List customerIdList);
 
XML文件:
<select id="selectCustomerCountList" parameterType="java.util.List" resultType="java.lang.Long">
    select count(1) from np_customer_info where id in
    <foreach item="item" collection="list" separator="," open="(" close=")" index="">
    	#{item, jdbcType=INTEGER}   
    </foreach>
</select>
    
======================
注意:此時(shí)collection強(qiáng)制指定為list且不可改變

第三種

利用注解@Param指定入?yún)ist的名稱------正常

DAO層:
Long selectCustomerCountList(@Param("customerIdList") List customerIdList);
 
XML文件:
<select id="selectCustomerCountList" parameterType="java.util.List" resultType="java.lang.Long">
    select count(1) from np_customer_info where id in
    <foreach item="item" collection="customerIdList" separator="," open="(" close=")" index="">  
    	#{item, jdbcType=INTEGER}   
    </foreach>
</select>
 
======================
注意: 此時(shí)的DAO層參數(shù)名可以 @Param("customerIdList") 與 collection的屬性值一致

第四種

將List包裝成Map參數(shù)進(jìn)行傳遞------正常

在Service業(yè)務(wù)處理層次上面將參數(shù)進(jìn)行包裝
public Long selectCustomerCountMap(List customerIdList) {  
    Map maps = new HashMap();
    maps.put("customerIds", customerIdList);
    return customerMapper.selectCustomerCountMap(maps);
}
   
DAO層:
Long selectCustomerCountMap(Map maps);
    
XML文件:
<select id="selectCustomerCountMap" parameterType="java.util.Map" resultType="java.lang.Long">
    select count(1) from np_customer_info where id in
    <foreach item="item" collection="customerIds" separator="," open="(" close=")" index="">
    	#{item, jdbcType=INTEGER}   
    </foreach>
</select>
    
==============
注意: 入?yún)㈩愋褪莏ava.util.Map而不再是List ,此時(shí)的collection屬性值為Map中的Key值。

第五種

把List 放入一個(gè)Bean對(duì)象中 ------報(bào)錯(cuò)

Model層(Bean層):
public class CompanyQueryModel {
    private String companyType;
    private List<String> customsCodeList; 
}

DAO層:
List<CompanyResultModel> selectCompanyInfo (CompanyQueryModel companyQueryModel);

XML層:
<select id="selectCompanyInfo" parameterType="com..dto.CompanyQueryModel" resultType="com.dto.CompanyResultModel">
    select * from np_company_info 
	<if test="customsCodeList != null and customsCodeList.size() > 0">
        and v.CUSTOMS_CODE in
        <foreach item="item" index="index" collection="customsCodeList" open="(" separator="," close=")">
            #{item}
        </foreach>
	</if>
</select>

第六種

把List 放入一個(gè)Bean對(duì)象中,利用@Param指定入?yún)ean名稱,Xml取Bean.List------正常

Model層(Bean層):
public class CompanyQueryModel {
    private String companyType;
    private List<String> customsCodeList; 
}

DAO層:
List<CompanyResultModel> selectCompanyInfo(@Param("model") CompanyQueryModel companyQueryModel);

XML層:
<select id="selectCompanyInfo" parameterType="com..dto.CompanyQueryModel" resultType="com.dto.CompanyResultModel">
    select * from np_company_info 
	<if test="model.customsCodeList != null and model.customsCodeList.size() > 0">
        and v.CUSTOMS_CODE in
        <foreach item="item" index="index" collection="model.customsCodeList" open="(" separator="," close=")">
            #{item}
        </foreach>
    </if>
</select>

第七種

把List 放入一個(gè)Bean對(duì)象中, XML不用#{item} 改為 #{tagIds[${index}]}

  • 這中寫(xiě)法意思是,取這個(gè)數(shù)組中的每一個(gè),因?yàn)樽侄问荓ist。
Bean層:
public class AlarmConditionDTO {
    private List<String> orgIds;  
    private List<String> tagIds;   
    private String alertType;
}

DAO層:
List<Map<String,String>> selectDeviceCountByCondition(AlarmConditionDTO alarmConditionDTO);

XML層:
<select id="selectDeviceCountByCondition" resultType="java.util.Map">
    SELECT * from md_tag_target_relation_device 
    where 1=1
    <if test="tagIds != null and tagIds.size()>0">
        and tag_id IN
        <foreach collection="orgIds" index="index" open="(" close=")" separator="," item="item">
            #{tagIds[${index}],jdbcType=VARCHAR}
        </foreach>
    </if>
    <if test="orgIds != null and orgIds.size()>0">
        and d.region_code IN
        <foreach collection="orgIds" index="index" open="(" close=")" separator="," item="item">
            #{orgIds[${index}],jdbcType=VARCHAR}
        </foreach>
    </if>

總結(jié)

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

相關(guān)文章

  • maven項(xiàng)目在實(shí)踐中的構(gòu)建管理之路的方法

    maven項(xiàng)目在實(shí)踐中的構(gòu)建管理之路的方法

    這篇文章主要介紹了maven項(xiàng)目在實(shí)踐中的構(gòu)建管理之路的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-05-05
  • java實(shí)現(xiàn)發(fā)牌小程序

    java實(shí)現(xiàn)發(fā)牌小程序

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)發(fā)牌小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Java將文件壓縮成zip并下載

    Java將文件壓縮成zip并下載

    這篇文章主要為大家詳細(xì)介紹了Java如何將文件壓縮成zip和下載,并處理base64和url,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下
    2025-04-04
  • 在Spring?Boot使用Undertow服務(wù)的方法

    在Spring?Boot使用Undertow服務(wù)的方法

    Undertow是RedHAT紅帽公司開(kāi)源的產(chǎn)品,采用JAVA開(kāi)發(fā),是一款靈活,高性能的web服務(wù)器,提供了NIO的阻塞/非阻塞API,也是Wildfly的默認(rèn)Web容器,這篇文章給大家介紹了在Spring?Boot使用Undertow服務(wù)的方法,感興趣的朋友跟隨小編一起看看吧
    2023-05-05
  • springboot上傳文件,url直接訪問(wèn)資源問(wèn)題

    springboot上傳文件,url直接訪問(wèn)資源問(wèn)題

    這篇文章主要介紹了springboot上傳文件,url直接訪問(wèn)資源問(wèn)題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 解決Maven parent.relativePath帶給我的坑

    解決Maven parent.relativePath帶給我的坑

    在Linux環(huán)境下使用Maven進(jìn)行項(xiàng)目打包時(shí),可能會(huì)遇到“當(dāng)前目錄沒(méi)有pom文件”的錯(cuò)誤,需要確認(rèn)在包含pom.xml文件的項(xiàng)目目錄下執(zhí)行Maven命令,另外,如果遇到“parent.relativePath points at wrong local POM”錯(cuò)誤,可能是父模塊依賴問(wèn)題
    2024-09-09
  • Mybatisplus集成springboot完成分頁(yè)查詢功能(示例代碼)

    Mybatisplus集成springboot完成分頁(yè)查詢功能(示例代碼)

    今天小編給大家分享Mybatisplus集成springboot完成分頁(yè)查詢功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2023-11-11
  • Java編程一道多線程問(wèn)題實(shí)例代碼

    Java編程一道多線程問(wèn)題實(shí)例代碼

    這篇文章主要介紹了Java編程一道多線程問(wèn)題實(shí)例代碼,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • 詳解Spring Boot整合Mybatis實(shí)現(xiàn) Druid多數(shù)據(jù)源配置

    詳解Spring Boot整合Mybatis實(shí)現(xiàn) Druid多數(shù)據(jù)源配置

    本篇文章主要介紹了詳解Spring Boot整合Mybatis實(shí)現(xiàn) Druid多數(shù)據(jù)源配置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • SpringBoot中的server.context-path的實(shí)現(xiàn)

    SpringBoot中的server.context-path的實(shí)現(xiàn)

    本文主要介紹了SpringBoot中的server.context-path的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08

最新評(píng)論