Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 2641|回复: 0

mybatis问题。foreach循环遍历数组报错情况,及其解决方法

[复制链接]
  • TA的每日心情
    奋斗
    2024-9-22 15:19
  • 签到天数: 795 天

    [LV.10]以坛为家III

    2050

    主题

    2108

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    724084
    发表于 2021-4-23 12:13:40 | 显示全部楼层 |阅读模式

    根据条件查询数据列表,mybatis查询代码如下

    如果只查询属于特定部门拥有的数据权限。这需要用 String[ ] codes保存当前部门及其子部门的部门编码。

    所以需要在mybatis中遍历编码数组。

    失败1

    <select id="findList" resultType="xx.entity.Xxxx">
            SELECT ${sqlMap.column.toSql()}
            FROM ${sqlMap.table.toSql()}
            <where>
                ${sqlMap.where.toSql()}
                <if test="codes != null and codes.length > 0">
                    AND u5.office_code in
                    <foreach item="code" index="index" collection="codes" open="(" separator="," close=")">
                        ${code}
                    </foreach>
                </if>
            </where>
            ORDER BY ${sqlMap.order.toSql()}
        </select>

    上面的代码会在仅查询部门范围的数据时报错。

    Error querying database.  Cause: java.sql.SQLException: 在将 varchar 值 'SD' 转换成数据类型 int 时失败。

    原因是 ${ } 符号获取数组值时,不会在值两边添加引号 ' '  查询数据库时,可能默认为这个in里面的是int数值,所以报类型转换异常。

    还原真实sql如下 (数据库的部门编码是字符串值。)

    select * from data a
    left join user u on u.user_code=a.createBy
    left join employee e on e.emp_code=u.ref_code
    left join office o on o.office_code=e.office_code
    where a.status='0' and o.office_code in (001016,003,004,005,006,008,007)

    失败2

    就在想,既然是没加引号的问题,那我改用 #{ }这个符号来取值不就好的

           <if test="codes != null and codes.length > 0">
                    AND u5.office_code in
                    <foreach item="code" index="index" collection="codes" open="(" separator="," close=")">
                        #{code}
                    </foreach>
                </if>

    再次尝试还是不行。这次报差错不一样了

    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named '__frch_code_0' in 'class'

    没有找到对应的get的属性方法。觉得默认以为我这个是一个实体类,然后以get方式获取值,但我这里明显是String[ ] 数组形式的数据。

     

    失败3

    再从网上搜索资料。还有一种用法

           <if test="codes != null and codes.length > 0">
                    AND u5.office_code in
                    <foreach item="code" index="index" collection="codes" open="(" separator="," close=")">
                        #{codes[${index}]}
                    </foreach>
                </if>

    这种还是会报错。

    nested exception is org.apache.ibatis.exceptions.PersistenceException:
    ### Error querying database. Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'codes[0]'. It was either not specified and/or could not be found for the javaType ([Ljava.lang.String;) : jdbcType (null) combination.
    ### Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'codes[0]'. It was either not specified and/or could not be found for the javaType ([Ljava.lang.String;) : jdbcType (null) combination.

     

    成功

    最后,还是使用 '${ }'

    用美元符号在左右两边加引号方式解决问题。

           <if test="codes != null and codes.length > 0">
                    AND u5.office_code in
                    <foreach item="code" index="index" collection="codes" open="(" separator="," close=")">
                        '${code}'
                    </foreach>
                </if>

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-10-6 23:47 , Processed in 0.060534 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表