项目中用到了sharding-sphere分库分表,在mybatis中有段很正常的sql,如下
SELECT NAME,age,department,start_time
FROM test
WHERE DATE_FORMAT(start_time, '%H:%i') = #{startTime}
执行该代码出现异常
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at org.apache.shardingsphere.sql.parser.binder.segment.select.pagination.PaginationContext.getValue(PaginationContext.java:57) ~[shardingsphere-sql-parser-binder-4.1.0.jar:4.1.0]
at org.apache.shardingsphere.sql.parser.binder.segment.select.pagination.PaginationContext.(PaginationContext.java:50) ~[shardingsphere-sql-parser-binder-4.1.0.jar:4.1.0]
at org.apache.shardingsphere.sql.parser.binder.segment.select.pagination.engine.LimitPaginationContextEngine.createPaginationContext(LimitPaginationContextEngine.java:38) ~[shardingsphere-sql-parser-binder-4.1.0.jar:4.1.0]
at org.apache.shardingsphere.sql.parser.binder.segment.select.pagination.engine.PaginationContextEngine.createPaginationContext(PaginationContextEngine.java:48) ~[shardingsphere-sql-parser-binder-4.1.0.jar:4.1.0]
at org.apache.shardingsphere.sql.parser.binder.statement.dml.SelectStatementContext.(SelectStatementContext.java:100) ~[shardingsphere-sql-parser-binder-4.1.0.jar:4.1.0]
at org.apache.shardingsphere.sql.parser.binder.SQLStatementContextFactory.getDMLStatementContext(SQLStatementContextFactory.java:103) ~[shardingsphere-sql-parser-binder-4.1.0.jar:4.1.0]
该异常看上去是分页的问题,因为我的项目用到了pageHelper,后端代码也正好是需要分页的,如下:
PageHelper.startPage(pageNum, pageSize, true);
testMapper.selectByTime(startTime);
经过各种排查,猜测是sharding-sphere对sql进行处理与pageHelper的分页机制有冲突,我使用的shardingsphere版本号:4.1.1,pageHelper版本号:5.1.2
最后经过不断尝试,发现将sql改为如下就正常了,没有使用date_format函数
SELECT NAME,age,department,start_time
FROM test
WHERE start_time like concat('% ', #{startTime},':%')
有些博友说把查询的参数startTime改为时间类型,两边都使用date_format函数也能正常,这个有待确认。
欢迎各位博友讨论,有没有更好的方法解决该问题。