Mybatis傳遞List集合方式
第一種
參數(shù)是常規(guī)的List, 但是xml變量名不是list------報錯
完整錯誤如下:
org.apache.ibatis.binding.BindingException: Parameter ‘customerIdList’ not found. Available parameters are [collection, list]
解釋:
- 當我們傳遞一個 List 實例或者數(shù)組作為參數(shù)對象傳給 MyBatis。
- 當你這么做的時 候,MyBatis 會自動將它包裝在一個 Map 中,用名稱在作為鍵。
- List 實例將會以“list” 作為鍵,而數(shù)組實例將會以“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 層接口的參數(shù)名與XML 文件中的collection的屬性值一致,是導致的問題的主要原因。
第二種
參數(shù)是常規(guī)的List, xml變量名是list------正常
- 利用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指定入?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>
======================
注意: 此時的DAO層參數(shù)名可以 @Param("customerIdList") 與 collection的屬性值一致
第四種
將List包裝成Map參數(shù)進行傳遞------正常
在Service業(yè)務處理層次上面將參數(shù)進行包裝
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 ,此時的collection屬性值為Map中的Key值。
第五種
把List 放入一個Bean對象中 ------報錯
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 放入一個Bean對象中,利用@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 放入一個Bean對象中, XML不用#{item} 改為 #{tagIds[${index}]}
- 這中寫法意思是,取這個數(shù)組中的每一個,因為字段是List。
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>
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
在Spring?Boot中MyBatis?的自動提交行為全解析(最新整理)
在Spring?Boot中,MyBatis的“自動提交”行為由Spring框架控制,而不是MyBatis本身,在無事務注解的情況下,Spring會為每個數(shù)據(jù)庫操作開啟并立即提交一個獨立事務,接下來通過本文給大家介紹在Spring?Boot中MyBatis的自動提交行為解析,感興趣的朋友跟隨小編一起看看吧2026-01-01
java高并發(fā)下CopyOnWriteArrayList替代ArrayList
這篇文章主要為大家介紹了java高并發(fā)下CopyOnWriteArrayList替代ArrayList的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
Spring Boot中自定義注解結合AOP實現(xiàn)主備庫切換問題
這篇文章主要介紹了Spring Boot中自定義注解+AOP實現(xiàn)主備庫切換的相關知識,本篇文章的場景是做調度中心和監(jiān)控中心時的需求,后端使用TDDL實現(xiàn)分表分庫,需要的朋友可以參考下2019-08-08

