MyBatis使用<foreach>标签报错
程序员文章站
2024-03-20 20:52:52
...
背景
在使用mybatis
过程中,<foreach>
标签算是比较常用的,最近在项目中遇到这样一个问题,使用<foreach>
标签循环拼接SQL
语句时,报了一个错误:
nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘__frch_name_0’ in ‘class com.stand.modules.address.param.GeneralAddressQueryParam’
比较疑惑,这个标签使用了很多次了,还是第一次遇到这样的问题,通过查阅资料,得到了解决方案。
原因
首先贴出涉及到的实体类、Mapper
接口和对应的XML
部分代码
- 用于
Mapper
接口传参的实体类:
public class GeneralAddressQueryParam implements Serializable {
/**
* 地名,多级地名用逗号分隔
*/
private String names;
/**
* 多地名查询条件
*/
private List<String> nameList;
public String getNames() {
return names;
}
public void setNames(String names) {
this.names = names;
}
public List<String> getNameList() {
return nameList;
}
public void setNameList(List<String> nameList) {
this.nameList = nameList;
}
}
-
Mapper
接口
List<GeneralAddressFullNameDTO> multiNameQuery(GeneralAddressQueryParam queryParam);
-
XML
部分代码
<if test="nameList != null and nameList.size() > 0">
<foreach collection="nameList" item="name">
and address like concat('%', #{name}, '%')
</foreach>
</if>
以上就是问题涉及到的部分代码,出错的原因呢,就是在<foreach>
标签中取值出的错,网上查阅资料说是因为parameterType
接收的参数不是List
导致的,具体情况未核实。
解决方案
解决方法比较简单,或一种取值方式即可,将<foreach>
标签中遍历出来的值换做如下方式获取
<if test="nameList != null and nameList.size() > 0">
<foreach collection="nameList" item="name" index="index">
and address like concat('%', #{nameList[${index}]}, '%')
</foreach>
</if>
上一篇: Uva514——Rails
推荐阅读
-
MyBatis使用<foreach>标签报错
-
mybatis的include标签的使用
-
Mybatis中 foreach 嵌套使用 if 标签对象取值问题
-
mybatis使用foreach遍历时同时遍历key和value
-
Oracle+Mybatis的foreach insert批量插入报错的快速解决办法
-
Oracle+Mybatis的foreach insert批量插入报错的快速解决办法
-
MyBatis使用动态SQL标签的小陷阱
-
MyBatis使用动态SQL标签的小陷阱
-
Mybatis foreach标签使用不当导致异常的原因浅析
-
Mybatis foreach标签使用不当导致异常的原因浅析