浅谈mybatis中SQL语句给boolean类型赋值问题
作者:一只特立独行的猫猫 时间:2023-01-19 15:15:42
我就废话不多说了,大家还是直接看代码吧~
<select id="getBiTree" parameterType="String" resultMap="MenuVoListMap">
SELECT m.menu_id , m.parent_id , m.`name` , 1 opens FROM menu m
WHERE m.is_valid = 1 AND (m.type = 0 or m.type = 1) and m.menu_id IN (${menuId})
</select>
其中 opens是boolean类型,在这里将他的值赋值为1,即为true
<result property="open" column="opens"/> 这里的opens和上面的opens对应
补充知识:mybatis中当查询条件有Boolean类型的时候,为false时查询无效
eg:
<if test="isOk != null and isOk !=''">
and is_ok = #{isOk}
</if>
此时当isOk为false时,并未查询出is_ok对应的结果来
找原因:
直接到数据库使用脚本查询
select * from table
where is_ok = false
此时能查出is_ok为0的数据
select * from table where is_ok = true
此时能查出is_ok为1的数据
此时定位问题是否在
<if test="isOk != null and isOk !=''">
通过排查去掉and isOk!='',可以正确的查出
所以正确的查询结构是:
<if test="isOk != null">
and is_ok = #{isOk}
</if>
经研究:
mybatis的if判断里面最好不要使用boolean值:
mybatis会默认把空值转为false。所以如果遇见前面传空值,这个字段在mybatis里面永远就是false了,可以使用数字类型代替,但是不要使用0作为参数。
来源:https://blog.csdn.net/fhjdzkp/article/details/77775660
标签:mybatis,SQL,boolean,赋值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解MyBatis XML配置解析
2023-11-23 13:02:09
![](https://img.aspxhome.com/file/2023/7/59887_0s.png)
mybatis中mapper-locations的作用
2023-11-10 18:02:58
![](https://img.aspxhome.com/file/2023/0/59350_0s.png)
Java实战之基于swing的QQ邮件收发功能实现
2023-11-15 01:34:26
spring mvc 实现获取后端传递的值操作示例
2023-08-10 12:55:52
C语言编程C++动态内存分配示例讲解
2023-11-02 18:00:12
![](https://img.aspxhome.com/file/2023/5/105185_0s.png)
Idea Jrebel 报错:Cannot reactivate,offline seat in use
2022-01-11 05:08:22
![](https://img.aspxhome.com/file/2023/6/60576_0s.png)
IDEA不编译除了.java之外的文件的解决办法(推荐)
2023-09-16 15:50:44
![](https://img.aspxhome.com/file/2023/0/58570_0s.png)
一文搞懂Mybatis-plus的分页查询操作
2023-11-25 10:23:17
基于FeignException$InternalServerError的解决方案
2023-04-25 15:50:45
![](https://img.aspxhome.com/file/2023/2/60492_0s.png)
Java读文件修改默认换行符的实现
2023-11-29 08:24:32
Java超详细分析泛型与通配符
2023-07-28 08:34:26
![](https://img.aspxhome.com/file/2023/2/60592_0s.jpg)
Java 多线程等待优雅的实现方式之Phaser同步屏障
2023-11-29 09:47:43
![](https://img.aspxhome.com/file/2023/9/60549_0s.png)
SpringBoot之如何指定配置文件启动
2023-11-17 15:17:48
![](https://img.aspxhome.com/file/2023/5/59505_0s.png)
使用Java8 Stream流的skip + limit实现批处理的方法
2023-11-29 06:17:39
Java Fluent Mybatis实战之构建项目与代码生成篇下
2023-11-24 10:59:52
![](https://img.aspxhome.com/file/2023/7/59787_0s.png)
Spring Boot学习入门之AOP处理请求详解
2023-11-27 10:55:17
双重检查锁定模式Java中的陷阱案例
2023-11-13 22:11:02
![](https://img.aspxhome.com/file/2023/5/59575_0s.png)
Java读取.properties配置文件方法示例
2023-08-24 16:32:56
![](https://img.aspxhome.com/file/2023/9/58159_0s.png)
mybatis-plus分页查询的实现示例
2023-11-25 04:57:57
![](https://img.aspxhome.com/file/2023/5/60025_0s.png)
MyBatis-Plus实现多数据源的示例代码
2023-11-11 12:58:11
![](https://img.aspxhome.com/file/2023/3/59383_0s.png)