NutzCN Logo
问答 sql.getList转换实体List异常
发布于 3095天前 作者 qq_d6d46f6d 2061 次浏览 复制 上一个帖子 下一个帖子
标签:

使用Nutz版本1.r.55
jdk版本 1.8
List list = sql.getList(Lesson.class); 这一句能够取得内容
但一for循环 就报错(如图二)
图一
QQ截图20160601220518_bmp
图二
QQ截图20160601220552_bmp

6 回复

不用截图!!!!!!!!! 用菜单的代码按钮重新贴!!!

java.lang.ClassCastException: org.nutz.dao.entity.Record cannot be cast to com.teachart.domain.Lesson
at com.teachart.services.lesson.impl.LessonServiceImpl.getPages(LessonServiceImpl.java:149)
at admin.teachart.controllers.lesson.LessonController.list(LessonController.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:196)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2508)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2497)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

 public QueryResult getPages(String where, int pageNumber) {
        try {
            //定义分页
            if (pageNumber == 0) {
                pageNumber = 1;
            }
            //返回list
            List<Lesson> lessons = new ArrayList<Lesson>();
            Pager pager = this.dao().createPager(pageNumber, this.getPageSize());
            //调用sql 回调查询
            Sql sql = Sqls.queryRecord("select * from lesson " + where +

                    "limit " + (pageNumber - 1) * this.getPageSize() + " ," + pageNumber * this.getPageSize() + ";"
            );
            this.dao().execute(sql);
            List<Lesson> list = sql.getList(Lesson.class);
            if (!list.isEmpty()) {
                //遍历报异常
                for (Lesson l : list) {
                    System.out.println(l.getId());
                }
            }

            for (Lesson lesson : sql.getList(Lesson.class)) {
                //Lesson l = this.getLessonAllInfo(lesson.getId());
                //lessons.add(l);
            }
            list = null;
            Sql countSql = Sqls.fetchInt("select count(1) from lesson " + where);// 带count的sql
            countSql.params().putAll(sql.params());  // 重用参数
            this.dao().execute(countSql);
            int count = countSql.getInt();

            //封装QueryResult对象
            pager.setRecordCount(count);
            return new QueryResult(lessons, pager);

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

搞定了 是自己犯二。。
谢wendal第一时间来看、不咋经常发帖所以发的太乱,影响看贴的人心情。嘿嘿

这代码看的得有点崩溃

不要手工拼条件,用Cnd来拼

不要手工分页,除非内置的分页会报错

给你点范例, 自行改一下, 完全不需要自定义SQL啊

    public QueryResult query(Cnd cnd, int pageNumber) {
        QueryResult qr = new QueryResult();
        Pager pager = new Pager().setPageNumber(pageNumber);
        qr.setList(dao.query(Lesson.class, cnd, pager));
        pager.setRecordCount(dao.count(Lesson.class, cnd));
        qr.setPager(pager);
        return qr;
    }

另外,之所以报错,是因为 Sqls.queryRecord 返回的结果集就是List<Record>, 应该用Sqls.queryEntity

添加回复
请先登陆
回到顶部