Mybatis分页处理

来源:互联网 发布:融学软件下载 编辑:程序博客网 时间:2024/06/05 14:52

今天被MyBatis使用statementType="STATEMENT"坑的吐血。发火

这个声明下,必须使用${}方式来传递动态参数,然后Mybatis里面用了动态的include语句,测试其他部分的时候,因为没有用到这个声明,所有程序没问题,而在查询总数时,用了这个,导致一直传不进参数。就把那个传不进的参数修改了一下取值,在动态语句里面用条件过滤掉了,就正常了莫名其妙,还以为那个变量名字打错了,看的眼睛都花了。就用复制粘贴来。改了一圈,并没有效果(因为本来也就没拼错)。后面查来查去,发现了程序里面一些小的错误,就是本来是int类型的字段,我把那个字段当成了string,不过这其实不影响程序运行。本以为错误在这里,改了一圈,发现还是没有效果。又发现之前的在sql语句上把参数类型设置为了String,以为错误在这里,但是删了还是没有。啊,最后才发现,这个查询总数的sql语句用了statementType="STATEMENT"的声明,删掉就正常了。

附代码:

<!-- sql片段对应选取准则,id属性值任意 --><sql id="criteria"><where><if test="phone!=null and phone!='' ">and phone=#{phone}</if><if test="beginTime !=null and beginTime !='' ">and gmtcreate >= #{beginTime}</if><if test="endTime !=null and endTime !='' ">and gmtcreate < #{endTime}</if><if test="status!=0">and status = #{status}</if></where></sql><!-- sql片段对应表名,id属性值任意 --><sql id="tb"><!-- 去掉最后一个, --><trim suffixOverrides=","><if test="tablename == 't_charge_rechargerecord' ">${tablename}</if><if test="tablename == 't_charge_consumerecord'">${tablename}</if></trim></sql><select id="itemsCount" resultType="long">select count(1)from<include refid="tb"></include><include refid="criteria"></include></select><select id="selectRecharge" resultMap="rechargemap">select *from<include refid="tb"></include><include refid="criteria"></include>order by gmtcreate desc<if test="pagesize !=0 and offset !=-1">limit #{pagesize} offset #{offset}</if></select><select id="selectConsume" resultMap="consumemap">select *from<include refid="tb"></include><include refid="criteria"></include>order by gmtcreate desc<if test="pagesize !=0 and offset !=-1">limit #{pagesize} offset #{offset}</if></select>


原创粉丝点击