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

Mybatis傳遞List集合方式

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

第一種

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

完整錯(cuò)誤如下:

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

解釋?zhuān)?/strong>

  • 當(dāng)我們傳遞一個(gè) List 實(shí)例或者數(shù)組作為參數(shù)對(duì)象傳給 MyBatis。
  • 當(dāng)你這么做的時(shí) 候,MyBatis 會(huì)自動(dòng)將它包裝在一個(gè) Map 中,用名稱(chēng)在作為鍵。
  • 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的名稱(chēng)------正常

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)㈩?lèi)型是java.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名稱(chēng),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)文章

  • Java反射機(jī)制的適用場(chǎng)景及利弊詳解

    Java反射機(jī)制的適用場(chǎng)景及利弊詳解

    這篇文章主要介紹了Java反射機(jī)制的適用場(chǎng)景及利弊詳解,Spring用到很多反射機(jī)制,在xml文件或者properties里面寫(xiě)好了配置,然后在Java類(lèi)里面解析xml或properties里面的內(nèi)容,得到一個(gè)字符串,然后用反射機(jī)制,需要的朋友可以參考下
    2023-08-08
  • Apache?Log4j2?報(bào)核彈級(jí)漏洞快速修復(fù)方法

    Apache?Log4j2?報(bào)核彈級(jí)漏洞快速修復(fù)方法

    Apache?Log4j2?是一個(gè)基于Java的日志記錄工具,是?Log4j?的升級(jí),是目前最優(yōu)秀的?Java日志框架之一,這篇文章主要介紹了突發(fā)Apache?Log4j2?報(bào)核彈級(jí)漏洞快速修復(fù)方法,需要的朋友可以參考下
    2021-12-12
  • 在Spring?Boot中MyBatis?的自動(dòng)提交行為全解析(最新整理)

    在Spring?Boot中MyBatis?的自動(dòng)提交行為全解析(最新整理)

    在Spring?Boot中,MyBatis的“自動(dòng)提交”行為由Spring框架控制,而不是MyBatis本身,在無(wú)事務(wù)注解的情況下,Spring會(huì)為每個(gè)數(shù)據(jù)庫(kù)操作開(kāi)啟并立即提交一個(gè)獨(dú)立事務(wù),接下來(lái)通過(guò)本文給大家介紹在Spring?Boot中MyBatis的自動(dòng)提交行為解析,感興趣的朋友跟隨小編一起看看吧
    2026-01-01
  • 淺談Springboot整合RocketMQ使用心得

    淺談Springboot整合RocketMQ使用心得

    本篇文章主要介紹了Springboot整合RocketMQ使用心得,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Mybatis查詢語(yǔ)句返回對(duì)象和泛型集合的操作

    Mybatis查詢語(yǔ)句返回對(duì)象和泛型集合的操作

    這篇文章主要介紹了Mybatis查詢語(yǔ)句返回對(duì)象和泛型集合的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 使用Java實(shí)現(xiàn)簡(jiǎn)單的區(qū)塊鏈程序的方法

    使用Java實(shí)現(xiàn)簡(jiǎn)單的區(qū)塊鏈程序的方法

    這篇文章主要介紹了使用Java實(shí)現(xiàn)簡(jiǎn)單的區(qū)塊鏈程序的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • java高并發(fā)下CopyOnWriteArrayList替代ArrayList

    java高并發(fā)下CopyOnWriteArrayList替代ArrayList

    這篇文章主要為大家介紹了java高并發(fā)下CopyOnWriteArrayList替代ArrayList的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • java 排序算法之希爾算法

    java 排序算法之希爾算法

    這篇文章主要介紹了java 排序算法之希爾排序,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • Spring Boot中自定義注解結(jié)合AOP實(shí)現(xiàn)主備庫(kù)切換問(wèn)題

    Spring Boot中自定義注解結(jié)合AOP實(shí)現(xiàn)主備庫(kù)切換問(wèn)題

    這篇文章主要介紹了Spring Boot中自定義注解+AOP實(shí)現(xiàn)主備庫(kù)切換的相關(guān)知識(shí),本篇文章的場(chǎng)景是做調(diào)度中心和監(jiān)控中心時(shí)的需求,后端使用TDDL實(shí)現(xiàn)分表分庫(kù),需要的朋友可以參考下
    2019-08-08
  • Java線程等待喚醒幾種方法小結(jié)

    Java線程等待喚醒幾種方法小結(jié)

    線程等待和喚醒有三種實(shí)現(xiàn)方法,分別是Object類(lèi)中的wait、notify,Condition類(lèi)中的await、signal,LockSupport類(lèi)中的park、unpark方法,感興趣的可以了解一下
    2023-10-10

最新評(píng)論

兰坪| 太仓市| 汉寿县| 沂南县| 中西区| 阿克陶县| 新河县| 孝昌县| 泸溪县| 阿克苏市| 彝良县| 湖北省| 屏山县| 南汇区| 柘城县| 敦煌市| 阳谷县| 色达县| 竹北市| 林芝县| 和硕县| 烟台市| 扬中市| 南京市| 饶河县| 无极县| 英德市| 巍山| 额敏县| 孟村| 黑龙江省| 桃源县| 靖安县| 繁昌县| 岳普湖县| 交口县| 香港| 海阳市| 诸暨市| 清河县| 云霄县|