问题描述
有一个分页场景,前台通过ajax请求,后台用的mybatis分页插件,以下是插件配置:<propertyname="plugins"><list><beanclass="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor"><propertyname="dialect"><beanclass="com.github.miemiedev.mybatis.paginator.dialect.MySQLDialect"></bean></property></bean></list></property>
现在遇到一个问题,比如前台列表有10条数据,删除一条记录,然后再查询刷新列表,列表会少一条记录,但是分页的总数还是10条,这个分页总数是分页插件封装的,看了一下源码,里面有判断是否需要缓存,我把这条查询语句useCache设置为false,还是不行,源码如下:countTask=newCallable(){publicObjectcall()throwsException{Integercount=null;Cachecache=ms.getCache();if(cache!=null&&ms.isUseCache()){CacheKeycacheKey=executor.createCacheKey(ms,parameter,newPageBounds(),copyFromBoundSql(ms,boundSql,bufferSql.toString()));count=(Integer)cache.getObject(cacheKey);if(count==null){count=SQLHelp.getCount(bufferSql.toString(),ms,parameter,boundSql,dialect);cache.putObject(cacheKey,count);}}else{count=SQLHelp.getCount(bufferSql.toString(),ms,parameter,boundSql,dialect);}returnnewPaginator(page,limit,count);}};
如果过个几分钟再来查,分页总数就正常变成9条了,或者连续按F5刷新多次,分页总数也正常变化了,以下是插件源码执行分页的代码:ResultSetrs=null;try{connection=mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();countStmt=connection.prepareStatement(count_sql);//PageSQL和CountSQL的参数是一样的,在绑定参数时可以使用一样的boundSqlDefaultParameterHandlerhandler=newDefaultParameterHandler(mappedStatement,parameterObject,boundSql);handler.setParameters(countStmt);rs=countStmt.executeQuery();intcount=0;if(rs.next()){count=rs.getInt(1);}logger.debug("Totalcount:{}",count);returncount;}finally{try{if(rs!=null){rs.close();}}finally{try{if(countStmt!=null){countStmt.close();}}finally{if(connection!=null&&!connection.isClosed()){connection.close();}}}}
以下是应用打出的日志,执行分页查询有15条记录,但是分页总数才7条,分页的SQL拿到数据库去查是20多条:09:43:16.631[http-8080-exec-5]DEBUGc.i.c.d.m.s.S.findCategoryProductByCondition-==>Preparing:SELECTtb.idasid,tb.key_wordASkeyWord,tb.brandasbrand,tb.product_modelASproductModel,tc.category_nameascategoryNameFROMt_shop_producttbLEFTJOINt_shop_categorytcONtb.category_id=tc.idwheretb.shop_id=?andtb.category_id=?limit1509:43:16.631[http-8080-exec-5]DEBUGc.i.c.d.m.s.S.findCategoryProductByCondition-==>Parameters:1(Long),63(Long)09:43:16.927[main-SendThread(192.168.0.125:2181)]DEBUGorg.apache.zookeeper.ClientCnxn-Gotpingresponseforsessionid:0x147ce0f26a44f1eafter817ms09:43:16.927[http-8080-exec-5]INFOjdbc.sqltiming-SELECTtb.idasid,tb.key_wordASkeyWord,tb.brandasbrand,tb.product_modelASproductModel,tc.category_nameascategoryNameFROMt_shop_producttbLEFTJOINt_shop_categorytcONtb.category_id=tc.idwheretb.shop_id=1andtb.category_id=63limit15{executedin296msec}09:43:16.927[http-8080-exec-5]DEBUGc.i.c.d.m.s.S.findCategoryProductByCondition-<==Total:1509:43:16.927[http-8080-exec-5]DEBUGc.g.m.m.paginator.support.SQLHelp-TotalcountSQL[selectcount(1)from(SELECTtb.idasid,tb.key_wordASkeyWord,tb.brandasbrand,tb.product_modelASproductModel,tc.category_nameascategoryNameFROMt_shop_producttbLEFTJOINt_shop_categorytcONtb.category_id=tc.idwheretb.shop_id=?andtb.category_id=?)tmp_count]09:43:16.927[http-8080-exec-5]DEBUGc.g.m.m.paginator.support.SQLHelp-TotalcountParameters:{shopId=1,categoryId=63}09:43:16.943[http-8080-exec-5]INFOjdbc.sqltiming-selectcount(1)from(SELECTtb.idasid,tb.key_wordASkeyWord,tb.brandasbrand,tb.product_modelASproductModel,tc.category_nameascategoryNameFROMt_shop_producttbLEFTJOINt_shop_categorytcONtb.category_id=tc.idwheretb.shop_id=1andtb.category_id=63)tmp_count{executedin0msec}09:43:16.943[http-8080-exec-5]DEBUGc.g.m.m.paginator.support.SQLHelp-Totalcount:7
解决方案
解决方案二:
我也用的mybatis分页插件,删除了一条数据,数据马上会变小,不存在缓存问题啊
解决方案三:
这个是不是浏览器缓存造成的,而不是程序的问题
解决方案四:
列表数据是会变少,但totalcount总数没变,导致本来有两页数据,删除一页后,列表只有一页的数据,但是下面页码还有2页,也不是浏览器缓存,打断点跟进去每次都是这样,而且我还试过换浏览器访问,还是一样的效果