mybatis问题。foreach循环遍历数组报错情况,及其解决方法
根据条件查询数据列表,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='' 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>
mybatis问题。foreach循环遍历数组报错情况,及其解决方法的更多相关文章
- 【FAQ】应用集成HMS Core部分服务出现“ 6003报错”情况的解决方法来啦
背景 开发者在应用中集成HMS Core部分服务时,android sdk 以及flutter等跨平台sdk,会出现编译打包后,运行报6003错误码的情况.根据查询可以得知,错误代码 6003 表示证 ...
- C# foreach 循环遍历数组
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- IT兄弟连 Java语法教程 数组 使用foreach循环遍历数组元素
从JDK5之后,Java提供了一种更简单的循环:foreach循环,也叫作增强for循环,这种循环遍历数组和集合更加简洁.使用foreach循环遍历数组和集合元素时,无需获得数组或集合的长度,无需根据 ...
- VS2019 字符串对指针char*赋值编译器报错原因及解决方法
2019-05-26 21:55:08 前几天在敲代码时,将字符串“Hellow world!”赋值给指针char*类型指针时编译器报错的问题 网上搜索后发现 char*是历史遗留问题,如果程序修 ...
- sysctl -p 报错问题的解决方法
最近执行sysctl -p 命令时一直报错,类似这种格式: error: permission denied on key...... 经过网上搜索, 原来这些问题都是因为openvz模版的问题,要进 ...
- vue.js常见的报错信息及其解决方法的记录
1.Vue packages version mismatch 翻译:vue包版本匹配错误 报错样例: 报错原因:通常出现于一些依赖库的更新或者安装新的依赖库之后(可以认为npm update已经成为 ...
- 关于前段JS代码报错问题的解决方法
最近接手别人的一个项目,项目导入到Myeclipse中,JS代码一直报错,说missing semicolon.该错误是Myeclipse在检查JS代码的过程出现的. 后来经过检查,发现JS代码本身没 ...
- navicat连接mysql报错1251的解决方法
1.新安装的mysql8,使用破解版的navicat连接的时候一直报错,如图所示: 2.网上查找原因发现是mysql8 之前的版本中加密规则是mysql_native_password,而在mysql ...
- linux服务器启动报错UNEXPECTED INCONSISTENCY解决方法
内网的linux服务器给开发员用来测试以及共享文件使用,今天早上发现xshell连接不上该服务器,一开始进入系统显示reboot and select proper boot device or in ...
随机推荐
- Java&Selenium根据实参启动相应浏览器
Java&Selenium根据实参启动相应浏览器 /** * 定义函数initBrowser * @param browser:字符串参数chrome/ie/xx * @return 并返回驱 ...
- PAT1046
题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805277847568384 题解 题目有几个点需要注意: 甲和 ...
- dubbo的初探
1.RPC 基本概念1.1 RPC 协议(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议 ...
- 启动nginx 80端口被占用:tcp 0 0 127.0.0.1:80 127.0.0.1:34932 TIME_WAIT -
1.启动nginx命令./sbin/nginx 2.提示80端口被占用 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already ...
- 模糊查询的sql语句
Java程序中使用的sql语句一直不明白是什么作用,在网上搜索了一些资料,看到一篇博客,稍微解答了具体每条代码的作用,因为作者加了详细的注解,可以作为参考 <JavaWeb dao层条件查询(模 ...
- idea 设置 maven 默认位置
在 idea 中创建 maven 项目 每次都要修改掉 默认的maven位置,觉得很烦.... 所以这边篇博客就是专门解决掉这个麻烦精的......(233333) 首先 File --> ...
- iis大文件上传
IS出于安全考虑限制了大文件的上传,而网上百度到的大部分解决方法都是用一个管理员权限的记事本打开一个文件修改参数,但是我发现里面根本没有网上所说的那些参数,最后自己找到了修改发布文件的webconfi ...
- luogu 2272
Tarjan 缩点 拓扑排序 套路题
- Applications (ZOJ 3705)
题解:就是题目有点小长而已,可能会不想读题,但是题意蛮好理解的,就是根据条件模拟,计算pts.(送给队友zm. qsh,你们不适合训练了.) #include <iostream> #in ...
- (转)初试 Netflix 开源持续云交付平台 Spinnaker
目录 Spinnaker 介绍 环境.软件准备 安装 Development Spinnaker 配置依赖环境 配置并安装 Spinnaker 演示 Spinnaker Pipeline 演示 Spi ...