栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

shardingjdbc搭配PageHelper分页失败的问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

shardingjdbc搭配PageHelper分页失败的问题

问题背景

使用shardingjdbc对iot_device_history_value进行了分库分表

SQL语句如下


    
    
        device_id = #{deviceId}
        
            and 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')
        
    
    ORDER BY create_time DESC

对应的mapper如下

public List listHistoriesByDeviceId(
     @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的方式进行,如下