java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.pagehelper.Page pagehelper报错无法类型转化。

报错信息:

严重: Servlet.service() for servlet [springmvc] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.pagehelper.Page] with root cause
java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.pagehelper.Page
    at com.*.sellergoods.service.impl.ItemCatServiceImpl.findByParentId(ItemCatServiceImpl.java:124)
    at com.alibaba.dubbo.common.bytecode.Wrapper7.invokeMethod(Wrapper7.java)
    at com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:47)
    at com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:76)
    at com.alibaba.dubbo.config.invoker.DelegateProviderMetaDataInvoker.invoke(DelegateProviderMetaDataInvoker.java:52)
    at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56)
    at com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:62)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
    at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
    at com.alibaba.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:42)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
    at com.alibaba.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:78)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
    at com.alibaba.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:73)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
    at com.alibaba.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:138)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
    at com.alibaba.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
    at com.alibaba.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:38)
    at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
    at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:104)
    at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:96)
    at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:173)
    at com.alibaba.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51)
    at com.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:57)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

代码:

public PageResult findByParentId(Long parentId,int pageNum,int pageSize) {
   PageHelper.startPage(pageNum, pageSize);
TbItemCatExample example = new TbItemCatExample();
Criteria criteria = example.createCriteria();
criteria.andParentIdEqualTo(parentId);

List<TbItemCat> all = findAll();
System.out.println("==将类目数据写入reid==");
for (TbItemCat itemCat : all) {
redisTemplate.boundHashOps("itemCate").put(itemCat.getName(), itemCat.getTypeId());
}
System.out.println("==将类目数据写入reid结束==");

Page<TbItemCat> page= (Page<TbItemCat>)itemCatMapper.selectByExample(example);

return new PageResult(page.getTotal(), page.getResult());
}

  造成异常原因是代码红色部分,因为pagehelper在设置 PageHelper.startPage(pageNum, pageSize)后到使用 Page<TbItemCat> page= (Page<TbItemCat>)itemCatMapper.selectByExample(example);在这两段代码之前都不能都再进行任何查询不然就会造成类型转化异常。

  解决办法就是将红色代码在放在 pagehelper和Page之外:

public PageResult findByParentId(Long parentId,int pageNum,int pageSize) {
        System.out.println(pageNum);
        System.out.println(pageSize);
        System.out.println(parentId);

        PageHelper.startPage(pageNum, pageSize);
        TbItemCatExample example = new TbItemCatExample();
        Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        
        Page<TbItemCat> page= (Page<TbItemCat>)itemCatMapper.selectByExample(example);

        List<TbItemCat> all = findAll();
        System.out.println("==将类目数据写入reid==");
        for (TbItemCat itemCat : all) {
            redisTemplate.boundHashOps("itemCate").put(itemCat.getName(), itemCat.getTypeId());
        }
        System.out.println("==将类目数据写入reid结束==");

        return new PageResult(page.getTotal(), page.getResult());
    }

 

posted @ 2019-06-09 15:43  灯灯噔噔  阅读(9568)  评论(2编辑  收藏  举报