使用mybatis的一个坑
来源:互联网 发布:南京网络品牌推广 编辑:程序博客网 时间:2024/06/06 17:12
刚刚在使用mybatis写sql,一开始我定义了一个接口:
List<Video> getVideosByCondition(@Param("keyword") String keyword, @Param("status") List<String> status, @Param("deptId") Integer deptId, @Param("userId") String userId, @Param("categoryId") Integer categoryId, @Param("isDeleted") String isDeleted, @Param("index") Integer index, @Param("size") Integer size);
然后xml的写法:
<select id="getVideosByCondition" resultType="com.ovp.domain.Video"> select * from video where 1=1 <if test="keyword != null and keyword.length()>0"> and ( title like CONCAT('%', #{keyword,jdbcType=VARCHAR}, '%' ) or tag1 like CONCAT('%', #{keyword,jdbcType=VARCHAR}, '%' ) or videoId = #{keyword,jdbcType=VARCHAR} ) </if> <if test="status != null and status.size()>0"> and status in <foreach item="item" collection="status" separator="," open="(" close=")" index="index"> #{item,jdbcType=VARCHAR} </foreach> </if> <if test="deptId != null "> and deptId=#{deptId,jdbcType=INTEGER} </if> <if test="userId != null and userId.length()>0"> and userId = #{userId,jdbcType=VARCHAR} </if> <if test="categoryId != null"> and categoryId=#{categoryId,jdbcType=INTEGER} </if> <if test="isDeleted != null and isDeleted.length()>0"> and deleted=#{isDeleted,jdbcType=VARCHAR} </if> order by createdTime DESC <if test="index!=null and size!=null"> limit #{index,jdbcType=INTEGER}, #{size,jdbcType=INTEGER} </if></select>
接口传递的index和size分别是分页的参数。
然后查询结果一直有误,传递的参数也一直查不出问题。后来打印了输出的sql,发现,index的值一直是1。。。
排查来排查去,最后觉得是xml的问题。终于发现foreach里也有index。
而且当我的status的值是null时,查询没有任何问题。测试了一下发现了原因
我在使用foreach的时候,由于元素有2个,所以index是从0到1赋值,也就是说最后一个循环,index的值时1,
到了下面的limit语句,index其实是复用了上面的变量,所以值仍然是1。所以就一直有问题。。。。
最后我换了index变量为其他值。问题解决。
阅读全文
0 0
- 使用mybatis的一个坑
- mybatis 的一个坑
- 使用mybatis开发一个项目的过程
- 使用MyBatis的一个好习惯
- 构建一个使用mybatis框架的项目
- 使用Mybatis出现的一个异常bug
- Mybatis语法错误的一个坑
- Mybatis的TypeHandler的一个坑
- 由Mybatis发现的一个坑
- 一个mybatis的配置文件
- MyBatis使用Map批量插入数据的一个例子
- 学习Mybatis:Mybatis的使用
- MyBatis(一) 不使用MyBatis做一个简单的查询
- MyBatis插件的使用:mybatis-generator、mybatis-plugin、mybatis-pagehelper
- mybatis SqlBuilder的一个用法
- Spring+Mybatis 的一个错误
- Mybatis 一个简单的例子
- mybatis一个莫名的NullPointerException
- mysql中创建表删除表及表查询
- MD5加密技术(加盐)
- 数据库系统概念(机械工业出版社,第六版)复习——第十二章:事务管理
- javascript深入理解js闭包
- 棋盘覆盖问题(分治)
- 使用mybatis的一个坑
- 375. Guess Number Higher or Lower II
- iOS学习笔记-workspace添加工程时只有一个proj文件问题
- 一个屌丝程序猿的人生(五)
- AJAX 动态加载后台数据 绑定select
- 40. Combination Sum II
- php
- React.js 官方资料摘记:组合 VS 继承
- 图像质量评价与结构相似性指标