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 ...
随机推荐
- 使用python批量造测试数据
# -*- coding:utf-8 -*- import json import os import time class Virtual_Data: def __init__(self): sel ...
- mysql中对表操作----为所有列插入数据
为所有列插入数据 通常情况下,向数据表中插入数据应包含表中所有字段,也就是为表中所有字段添加数据,为表中所有字段添加数据有以下两种方式. 1.INSERT语句中指定所有字段名 使用INSER ...
- dblclick([[data],fn]) 当双击元素时,会发生 dblclick 事件。
dblclick([[data],fn]) 概述 当双击元素时,会发生 dblclick 事件.大理石量具哪家好 当鼠标指针停留在元素上方,然后按下并松开鼠标左键时,就会发生一次 click.在很短的 ...
- 006_linux驱动之_ioremap函数使用
(一)学习linux驱动之初,对ioremap函数的个人理解 (二)博客:实验探究 ioremap 这篇文章作者通过验证来阐述自己的观点,个人觉得挺好的 (三)函数原型 基本简介 void * __i ...
- 51nod 1051
* 最大子矩阵 * sum[i][j] 表示第 i 行前 j 列的和,即每一行的前缀 * i,j 指针枚举列,k指针枚举行 * Now 记录当前枚举的子矩阵的价值 * 由于记录了前缀信息,一旦 Now ...
- 「LOJ 121」「离线可过」动态图连通性「按时间分治 」「并查集」
题意 你要维护一张\(n\)个点的无向简单图.你被要求执行\(m\)条操作,加入删除一条边及查询两个点是否连通. 0:加入一条边.保证它不存在. 1:删除一条边.保证它存在. 2:查询两个点是否联通. ...
- 数据结构实验之栈与队列六:下一较大值(二)(SDUT 3333)
#include <bits/stdc++.h> using namespace std; int a[1000006]; int b[1000006]; int sta[100006]; ...
- maven+SSM+junit+jetty+log4j2环境配置的最佳实践
思路大致是 jetty插件 -> junit -> SpringMVC -> Spring -> log4j2 -> Mybatis整合 pom中的依赖跟着思路一批一批的 ...
- Python3读写JSON文件
JSON简介 JSON(JavaScript Object Notation)即JavaScript对象表示法,一种轻量级,通用的文本数据格式. JSON语法支持对象(Object),数组(Array ...
- c++ demo code
/* //多继承 #include <iostream> using namespace std; class Sofa { public: Sofa(); ~Sofa(); void s ...