根据条件查询数据列表,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循环遍历数组报错情况,及其解决方法的更多相关文章

  1. 【FAQ】应用集成HMS Core部分服务出现“ 6003报错”情况的解决方法来啦

    背景 开发者在应用中集成HMS Core部分服务时,android sdk 以及flutter等跨平台sdk,会出现编译打包后,运行报6003错误码的情况.根据查询可以得知,错误代码 6003 表示证 ...

  2. C# foreach 循环遍历数组

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  3. IT兄弟连 Java语法教程 数组 使用foreach循环遍历数组元素

    从JDK5之后,Java提供了一种更简单的循环:foreach循环,也叫作增强for循环,这种循环遍历数组和集合更加简洁.使用foreach循环遍历数组和集合元素时,无需获得数组或集合的长度,无需根据 ...

  4. VS2019 字符串对指针char*赋值编译器报错原因及解决方法

    2019-05-26   21:55:08 前几天在敲代码时,将字符串“Hellow world!”赋值给指针char*类型指针时编译器报错的问题 网上搜索后发现 char*是历史遗留问题,如果程序修 ...

  5. sysctl -p 报错问题的解决方法

    最近执行sysctl -p 命令时一直报错,类似这种格式: error: permission denied on key...... 经过网上搜索, 原来这些问题都是因为openvz模版的问题,要进 ...

  6. vue.js常见的报错信息及其解决方法的记录

    1.Vue packages version mismatch 翻译:vue包版本匹配错误 报错样例: 报错原因:通常出现于一些依赖库的更新或者安装新的依赖库之后(可以认为npm update已经成为 ...

  7. 关于前段JS代码报错问题的解决方法

    最近接手别人的一个项目,项目导入到Myeclipse中,JS代码一直报错,说missing semicolon.该错误是Myeclipse在检查JS代码的过程出现的. 后来经过检查,发现JS代码本身没 ...

  8. navicat连接mysql报错1251的解决方法

    1.新安装的mysql8,使用破解版的navicat连接的时候一直报错,如图所示: 2.网上查找原因发现是mysql8 之前的版本中加密规则是mysql_native_password,而在mysql ...

  9. linux服务器启动报错UNEXPECTED INCONSISTENCY解决方法

    内网的linux服务器给开发员用来测试以及共享文件使用,今天早上发现xshell连接不上该服务器,一开始进入系统显示reboot and select proper boot device or in ...

随机推荐

  1. Cairo

    Cairo 英[ˈkaɪrəʊ] 美[ˈkaɪroʊ] n. 开罗(埃及首都); [例句]From Cairo came expressions of regret at the attac

  2. C#readonly和const对比

    readonly:常量,只能在声明时或构造函数中赋值,与static连用表示静态常量 const:静态常量,只能在声明时赋值

  3. Android-File读写+SharedPreferences的存取地址

    写了两个demo,一个是使用SharedPreferences将数据存储在应用文件中并读取,另一个是使用Context的openFileOutput和openFileInput将数据存储在应用文件中并 ...

  4. Task.Run 和 Task.Factory.StartNew 区别

    Task.Run 是在 dotnet framework 4.5 之后才可以使用, Task.Factory.StartNew 可以使用比 Task.Run 更多的参数,可以做到更多的定制. 可以认为 ...

  5. Oracle不足补零函数

    ,') from dual ,') from dual ,') from dual ,') from dual ,') from dual

  6. 可嵌入的脚本引擎 Jx9

    Jx9是一个可嵌入的脚本引擎,基于JSON实现了图灵完备(Turing complete)的编程语言. Jx9 是那些需要流行和高效率脚本支持应用程序(比如:游戏.数据库系统,文本编辑器,网络应用程序 ...

  7. C指针的运算

    指针的运算在数组中才有意义 int *p; p++,一定是在一片连续的区域才有意义,当然越界也会带来诸多问题. void main() { ; int *p = &num;//这根本无界可言 ...

  8. redis与spring整合实例

    1)首先是redis的配置. 使用的是maven工程,引入redis与spring整合的相关jar包 <!-- redis服务 start--> <dependency> &l ...

  9. Docker:18.x版本安装与卸载

    Docker CE 分为 stable, test, 和 nightly 三个更新频道.每六个月发布一个 stable 版本 初始化机器 #1.关闭警报 vi /etc/inputrc #然后将set ...

  10. 代码审计-Typecho反序列化getshell

    0x01 漏洞代码 install.php: <?php $config = unserialize(base64_decode(Typecho_Cookie::get('__typecho_c ...