MyBatis使用<foreach>标签报错

背景

在使用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>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值