使用shardingjdbc对iot_device_history_value进行了分库分表
SQL语句如下
device_id = #{deviceId} ORDER BY create_time DESCand date_format(create_time,'%y%m%d %H:%i:%s') ">>= date_format(#{beginTime},'%y%m%d %H:%i:%s') and date_format(create_time,'%y%m%d %H:%i:%s') <= date_format(#{endTime},'%y%m%d %H:%i:%s')
对应的mapper如下
public ListlistHistoriesByDeviceId( @Param("deviceId") Long deviceId, @Param("beginTime") Date beginTime, @Param("endTime") Date endTime );
在使用PageHelper提供的默认分页方法时总是报如下错误
### Error querying database. Cause: java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.lang.Integer ### The error may exist in file [/Users/chanmufeng/code/zhonghe/IOTCloud/ruoyi-iot/target/classes/mapper/iot/DeviceHistoryMapper.xml] ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL: select id, device_number, value, create_time, device_id from iot_device_history_value WHERe device_id = ? and date_format(create_time,'%y%m%d %H:%i:%s') >= date_format(?,'%y%m%d %H:%i:%s') and date_format(create_time,'%y%m%d %H:%i:%s') <= date_format(?,'%y%m%d %H:%i:%s') ORDER BY create_time DESC LIMIT ? ### Cause: java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.lang.Integer org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:问题排查
1.不适用shardingjdbc分页则不会出现这个问题
2.不分页也不会出现这个问题
3.用postman进行测试(这个真是见鬼!)
怀疑是shardingjdbc对分页的内容做了手脚,于是查看shardingjdbc的源码,找到shardingjdbc中ExecutorEngine类,其中有execute 方法(这个方法中可以查看最终改写后的sql),打断点之后发现无论我传1还是2,最后生成的SQL语句都没有问题,因此排除shardingjdbc的问题
问题只能出在MyBatis分页插件pageHelper身上了
问题解决果断放弃PageHelper的自动分页功能,采用手写LIMIT的方式进行,如下
无论我改将$改成#,还是在controller直接计算LIMIT参数的值,都不对,报如下错误
Error querying database. Cause: java.lang.IndexOutOfBoundsException: Index:0
查看SQL语句发现,PageHelper自动在搜索语句之前进行了count查询,自动添加了SELECT count(0)语句,也就是说即使不设置PageHelper.startPage(***)的方法,PageHelper还是有某些机制默认给我做了分页!
最后修改pageHelper的配置如下
supportMethodsArguments: false
配置的含义是:依据mapper的入参,如果参数中有pageNum,pageSize分页参数,则会自动分页
最后手写count查询,然后进行手动分页,peace!



