Mybatis动态语句,if test字符串不用进行null判断
2020-06-04 07:48
387 查看
描述:根据creator列进行过滤获取image表的数据。
代码如下:
@Select("<script>" + " select * from image " + " <where>" + " <if test='isAllCreator != 1'> " + " creator = 1 " + " </if>" + " </where>" + "</script>") List<ImageVo> testDynamicSql(@Param("isAllCreator") String isAllCreator);
想实现一个效果,针对参数isAllCreator参数,如果传递1则不过滤creator列,即获取所有creator的数据,如果不传或传其他值,则只获取creator=1的数据。
经过测试发现,不用对creator参数进行null判断,直接判断是否等于1即可,这样做也不会报错。
@Test public void testDynamicSql() { List<ImageVo> imageList = new ArrayList<>(); imageList = mybatisTestMapper.testDynamicSql("0"); imageList = mybatisTestMapper.testDynamicSql(null); imageList = mybatisTestMapper.testDynamicSql("1"); Assert.assertNotNull(imageList); }
打印的sql如下:
看网上的文章,都是先进行null判断,再进行相等判断,如果不是亲自试一下,我都怀疑自己写错了。
PS:源码找到了,如果isAllCreator参数传递null,在org.apache.ibatis.ognl.OgnlOps#equal方法中会返回false,表示if判断条件为false。
Refer:Mybatis官方文档 http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html
相关文章推荐
- MyBatis if test 字符串判断问题
- Mybatis中 if test 判断数值字符串注意项
- MyBatis if test 字符串判断问题
- mybatis,在使用if test进行字符串比较
- Mybatis if test 动态判断数字时需要注意的问题
- Mybatis 实现不确定修改几个字段的修改方法(动态语句set、if实现),避免修改时其它没被修改的字段变为null
- mybatis 里面使用<if test=""> 判断,当后台传的值是数字字符串时,怎么处理
- mybatis中if test 语句 当参数类型为string时,如何写判断条件
- Mybatis中动态sql___if判断语句
- Mybatis之动态sql语句中时间判断(if)的应用
- mybatis中利用动态sql在if语句中比较字符串
- mybatis if test 字符串判断问题
- mybatis if test 字符串比较错误处理
- myBatis查询某个时间区间数据<if test=""> 如何写判断条件,and 里面的内容怎么写?
- Mybatis的动态sql语句if和choose
- 【面试题】不用if,条件表达式,switch等判断语句------找出两个数中较大的
- 不用”if“,”?:“,”switch“或其他判断语句,求两个数中较大的数或较小的数
- Mybatis中 if标签判断字符串
- mybatis 中if判断把零识别为null
- Mybatis的if test字符串比较 报NumberFormatException错误