一開始是在mybatis中查詢報錯:
MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'xxxxx' not found. Available parameters are [payment, page, param1, param2]"
只有在查一個參數的時候報錯另一個參數的時候又不會,就很奇怪,后來查看到一篇文章得到啟發:
https://blog.csdn.net/zhangvivid/article/details/80990595
我的接口中有兩個參數,然后我就試着加了@Param注解,就真的沒有報錯了
這讓我想起上幾天mybatis中有另一個bug:接口傳參為List 時寫 <if>標簽總是出錯,后來也是在傳參中加了@Param 才解決的。
接口:
List<AttachmentDetail> findFileListByInfoId(@Param("list") List<AttachmentInfo> attachmentInfos);
xml:
<select id="findFileListByInfoId"
resultType="xxx.commonservice.hdfs.entity.AttachmentDetail">
SELECT
det.id,
det.file_name,
det.file_suffix,
det.file_size
FROM
hdfs_attachment_detail det
WHERE 1=1 and
<if test="list != null and list.size() > 0">
det.attachment_info_id in
<foreach item="item" collection="list" separator="," open="(" close=")" index="">#{item.id}
</foreach>
</if>
ORDER BY det.created_date DESC
</select>
總結:
對於mybatis的接口類中的方法,
1.當方法參數數量大於1時,需要在方法參數中使用@Param把函數參數與Mapper.xml文件中的參數關聯。
2.接口傳參為List時需要使用@Param