1. 程式人生 > >決解Mybatis傳遞List集合報錯 Available parameters are [collection, list]

決解Mybatis傳遞List集合報錯 Available parameters are [collection, list]

完整錯誤如下:
org.apache.ibatis.binding.BindingException: Parameter ‘customerIdList’ not found. Available parameters are [collection, list]

解釋:
當我們傳遞一個 List 例項或者陣列作為引數物件傳給 MyBatis。當你這麼做的時 候,MyBatis 會自動將它包裝在一個 Map 中,用名稱在作為鍵。List 例項將會以“list” 作為鍵,而陣列例項將會以“array”作為鍵。所以,當我們傳遞的是一個List集合時,mybatis會自動把我們的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 層介面的引數名與XML 檔案中的collection的屬性值一致,是導致的問題的主要原因。

解決方法
第一種:利用Mybatis給我們的封裝進行XML配置,將我們的XML中collection屬性值設定為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> ====================== 注意:此時collection強制指定為list且不可改變

第二種: 利用註解@Param指定我們的入參名稱

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>

======================
注意: 此時的DAO層引數名可以 @Param("customerIdList") 與 collection的屬性值一致

第三種:將我們的List包裝成Map引數進行傳遞

在Service業務處理層次上面將引數進行包裝
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>
==============
注意: 入參型別是java.util.Map而不再是List ,此時的collection屬性值為Map中的Key值。

相關推薦

Mybatis傳遞List集合 Available parameters are [collection, list]

完整錯誤如下: org.apache.ibatis.binding.BindingException: Parameter ‘customerIdList’ not found. Available

Parameter 'idList' not found. Available parameters are [collection, list]

今天遇到一個這樣的問題,寫在這裡作為一個記錄,方便以後檢視, 遇到這個錯誤首先檢查所有的型別是不是都是List, 第二,在Mapper.java使用list需要有@parm註解一同使用(我就是因為沒有寫這個註解報錯)

Mybatis:Parameter 'list' not found. Available parameters are [groupList, param1]

ber goods values tor 我們 問題 util 沒有 nbsp GroupDao.java 裏面定義的方法: void batchInsertLog(@Param("groupList") List<MktPromotionIntegralLog>

JSON轉換包含Date日期屬性的物件的List集合問題

JSONObject轉換帶有Date型別物件的集合時格式問題處理辦法 問題描述:當一個List集合中含有包含Date型別的欄位物件時,而伺服器用JSONObject處理時,其中的日期格式不是我們想發的”yyy-MM-dd”格式 解決辦法:既然有大神或者前輩已

mybatis 批量插入list集合 Parameter 'interacId' not found. Available parameters are [list, collection]

  關鍵點在標註的位置:  引數型別  collection為list   list並不是你傳入的引數的變數名,我傳入的變數名為lists 直接寫成  list即可;   不要使用那個原生的 if(

mybatis批量插入“Parameter '__frch_item_0' not found. Available parameters are [list]”

原sql: <select id="queryList" parameterType="java.util.List" > insert into table1(column1,column2,column3,column4,column5) ( <f

mybatis批量插入“Parameter '__frch_item_0' not found. Available parameters are [list]”

原sql: <select id="queryList" parameterType="java.util.List" > insert into table1(column1,column2,column3,column4,column5)

xtrabackup-壓備份文件sh: qpress: command not found

bash logs 正常 工具 erro usr linu comm compress # xtrabackup --decompress --target-dir=/data/compressed xtrabackup version 2.4.8 based on M

MyBatis多個接口參數Available parameters are [0, 1, param1, param2], 及解決方法

pan 解決 而且 crm ger int mybatis 添加 為什麽 1. sql語句如下:  SELECT * FROM tb_crm_user WHERE id = #{userId, jdbcType=INTEGER} AND

zbb20171215 MyBatis: Parameter '*' not found. Available parameters are [1, 0, param1, param2]

class adding data alt num sql throws clas str mapper代理接口中的方法:   public User login(String loginid,String loginpwd) throws Exception; mapp

mybatis 動態sql 插入

method div exce 必須 字段 timeval blog ora eval 1. 值為null必須制定jdbcType 單條執行的話,可以考慮把值為null的字段去掉 2. 值的類型無法解析 比如oracle.sql.TIMESTAMP類型,需轉

Jedis連接池處理

CP pdu 解決 無法 等待 sta 信息 bsp ont 在使用Jedis連接池模式下,比較常見的報錯如下: redis.clients.jedis.exceptions.JedisConnectionException:Could not get a resource

python程序壓rar壓縮包

ould gcc-c++ root 產生 pytho oot exp import 依賴包 運行如下python程序報錯Couldn't find path to unrar library的解決辦法:#!/usr/bin/python #-*- coding:ut

Unknown column 'xxx' in 'field list' 原因

tro 實體類 username 實體 column 檢查 變量名 都是 nbsp Unknown column ‘userName‘ in ‘field list‘ 1.檢查你的數據庫列和實體類的變量名稱是否正確 2.我確認了很多遍沒錯啊!都是userName啊什麽鬼

Java 後臺介面為List<String> :【java.util.List】:Specified class is an interface

  public Response<?> add(HttpServletRequest request,List<String> fkContentList){}報錯: 簡單修改: public Response<?> add

mybatis:parameter ' ' not fount.Available parameters are [0, 1, param1, param2]

mybatis報錯:parameter ’ ’ not fount.Available parameters are [0, 1, param1, param2] 這種原因就是因為引數沒有對應上。解決方法就是在dao層方法引數加上@param(value=’’"),或者在sql語句中按照順序

Linux使用tar壓.tar.bz2檔案:(原因)tar: Child returned status 2 tar: Error is not recoverable: exiting now

tar -jxvf xxxx.tar.bz2 如下報錯: tar (child): bzip2:無法 exec: 沒有那個檔案或目錄 tar (child): Error is not recoverable: exiting now tar: Child returned stat

mysqlExpression #1 of SELECT list is not in GROUP BY clause and contains non

專案執行得好好的,換了個數據庫伺服器後就報這個錯了 版本原因 記錄解決辦法 //執行語句 set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZER

mybatis 呼叫儲存過程 org.apache.ibatis.mapping.ParameterMode.IN

“` org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error resolving ParameterMode.

mybatis多數據源

closed int nod sdk parent ora pub from utf 2018-12-06 16:58:35,709 [ main ] - [ INFO ] [ org.springframework.core.KotlinDetector : 57