Python 官方文档:入门教程 => 点击学习
目录foreach标签的collection属性的取值传的是List列表传的是Array数组传的是Mapcollection属性总结mybatis使用foreach标签报错原因解决方
接口代码
List<Emp> findEmpByDeptnos(List<Integer> deptnos);
xml配置代码
<select id="findEmpByDeptnos" resultType="Emp" parameterType="list">
SELECT * FROM emp e
WHERE e.deptno IN
<foreach collection="list" item="deptno" open="(" separator="," close=")">
#{deptno}
</foreach>
</select>
接口代码
List<Emp> findEmpByDeptnos(Integer[] deptnos);
xml配置代码
<select id="findEmpByDeptnos" resultType="Emp" parameterType="int">
SELECT * FROM emp e
WHERE e.deptno IN
<foreach collection="array" item="deptno" open="(" separator="," close=")">
#{deptno}
</foreach>
</select>
接口代码
List<Emp> findEmpByDeptnos(Map<String,List<Integer>> deptnos);
xml配置代码
<select id="findEmpByDeptnos" resultType="Emp" parameterType="map">
SELECT * FROM emp e
WHERE e.deptno IN
<foreach collection="myKey" item="deptno" open="(" separator="," close=")">
#{deptno}
</foreach>
</select>
在使用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>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: MyBatis中foreach标签的collection属性的取值方式
本文链接: https://www.lsjlt.com/news/165779.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0