经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
MyBatis中foreach标签的collection属性的取值方式
来源:jb51  时间:2022/8/23 15:36:55  对本文有异议

foreach标签的collection属性的取值

传的是List列表

接口代码

  1. List<Emp> findEmpByDeptnos(List<Integer> deptnos);

xml配置代码

  1. <select id="findEmpByDeptnos" resultType="Emp" parameterType="list">
  2. ? ? SELECT * FROM emp e
  3. ? ? WHERE e.deptno IN
  4. ? ? <foreach collection="list" item="deptno" open="(" separator="," close=")">
  5. ? ? ? ? #{deptno}
  6. ? ? </foreach>
  7. </select>

传的是Array数组

接口代码

  1. List<Emp> findEmpByDeptnos(Integer[] deptnos);

xml配置代码

  1. <select id="findEmpByDeptnos" resultType="Emp" parameterType="int">
  2. ? ? SELECT * FROM emp e
  3. ? ? WHERE e.deptno IN
  4. ? ? <foreach collection="array" item="deptno" open="(" separator="," close=")">
  5. ? ? ? ? #{deptno}
  6. ? ? </foreach>
  7. </select>

传的是Map

接口代码

  1. List<Emp> findEmpByDeptnos(Map<String,List<Integer>> deptnos);

xml配置代码

  1. <select id="findEmpByDeptnos" resultType="Emp" parameterType="map">
  2. ? ? SELECT * FROM emp e
  3. ? ? WHERE e.deptno IN
  4. ? ? <foreach collection="myKey" item="deptno" open="(" separator="," close=")">
  5. ? ? ? ? #{deptno}
  6. ? ? </foreach>
  7. </select>

collection属性总结

  • 如果传入的参数是List,则填写list
  • 如果传入的参数是数组形式,则填写array
  • 如果是多参数传参,传的是map,则填写列表的键key

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接口传参的实体类:

  1. public class GeneralAddressQueryParam implements Serializable {
  2.  
  3. ? ? /**
  4. ? ? ?* 地名,多级地名用逗号分隔
  5. ? ? ?*/
  6. ? ? private String names;
  7.  
  8. ? ? /**
  9. ? ? ?* 多地名查询条件
  10. ? ? ?*/
  11. ? ? private List<String> nameList;
  12.  
  13. ? ? public String getNames() {
  14. ? ? ? ? return names;
  15. ? ? }
  16.  
  17. ? ? public void setNames(String names) {
  18. ? ? ? ? this.names = names;
  19. ? ? }
  20.  
  21. ? ? public List<String> getNameList() {
  22. ? ? ? ? return nameList;
  23. ? ? }
  24.  
  25. ? ? public void setNameList(List<String> nameList) {
  26. ? ? ? ? this.nameList = nameList;
  27. ? ? }
  28. }

Mapper接口

  1. List<GeneralAddressFullNameDTO> multiNameQuery(GeneralAddressQueryParam queryParam);

XML部分代码

  1. <if test="nameList != null and nameList.size() > 0">
  2. ? ? <foreach collection="nameList" item="name">
  3. ? ? ?? ?and address like concat('%', #{name}, '%')
  4. ? ? </foreach>
  5. </if>

以上就是问题涉及到的部分代码,出错的原因呢,就是在<foreach>标签中取值出的错,网上查阅资料说是因为parameterType接收的参数不是List导致的,具体情况未核实。

解决方案

解决方法比较简单,或一种取值方式即可,将<foreach>标签中遍历出来的值换做如下方式获取

  1. <if test="nameList != null and nameList.size() > 0">
  2. ? ? <foreach collection="nameList" item="name" index="index">
  3. ? ? ?? ?and address like concat('%', #{nameList[${index}]}, '%')
  4. ? ? </foreach>
  5. </if>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号