NutzCN Logo
问答 sql.getList(Record.class)空指针
发布于 1789天前 作者 SwainHo 1271 次浏览 复制 上一个帖子 下一个帖子
标签:
 Pager pager = new OffsetPager(start, length);
        Sql sql = Sqls.create("select a.id,a.loginname,a.phone,a.email," +
                " b.name,b.gender" +
                " from sys_user a" +
                " left join edu_user_basic_info b" +
                " on a.id = b.user_id" +
                " where 1=1 " +
                " and a.disabled ='0' and a.delFlag='0' $condition");
        sql.setCondition(cnd);
        sql.setPager(pager);
        dao().execute(sql);
        List<Record> list = sql.getList(Record.class);
        List<Edu_user_basic_info> userInfo=new ArrayList<>();
        List<Sys_user> user=new ArrayList<>();
        for (Record re : list) {
            Sys_user su = re.toEntity(dao().getEntity(Sys_user.class),"a.");
            Edu_user_basic_info info=re.toEntity(dao().getEntity(Edu_user_basic_info.class),"b.");
            userInfo.add(info);
            user.add(su);
        }
        map.put("userInfo",userInfo);
        map.put("user",user);
        map.put("draw", draw);
        map.put("recordsTotal", length);
        return map;

用的是自定义sql,请问这个callBack需要自定义一个么,大体应该怎么写

3 回复
org.nutz.dao.DaoException: java.lang.RuntimeException: SQL without entity : select a.id,a.loginname,a.phone,a.email, b.name,b.gender from sys_user a left join edu_user_basic_info b on a.id = b.user_id where 1=1  and a.disabled ='0' and a.delFlag='0'  LIMIT 0, 20
	at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithoutTransaction(NutDaoRunner.java:128)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._run(NutDaoRunner.java:81)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:70)
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:266)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:274)
	at org.nutz.dao.impl.DaoSupport.execute(DaoSupport.java:262)
	at org.nutz.dao.impl.NutDao.execute(NutDao.java:978)
	at cn.wizzer.modules.services.sys.SysUserService.listStudent(SysUserService.java:187)
	at cn.wizzer.modules.controllers.platform.sys.SysUserController.dataStudent(SysUserController.java:357)
	at sun.reflect.GeneratedMethodAccessor97.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:483)
	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 cn.wizzer.common.processor.XssSqlFilterProcessor.process(XssSqlFilterProcessor.java:35)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.common.processor.NutShiroProcessor.process(NutShiroProcessor.java:52)
	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.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.common.processor.GlobalsSettingProcessor.process(GlobalsSettingProcessor.java:37)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.common.processor.LogTimeProcessor.process(LogTimeProcessor.java:21)
	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:198)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
	at cn.wizzer.common.filter.RouteFilter.doFilter(RouteFilter.java:33)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:522)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:1110)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:785)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1425)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	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)
Caused by: java.lang.RuntimeException: SQL without entity : select a.id,a.loginname,a.phone,a.email, b.name,b.gender from sys_user a left join edu_user_basic_info b on a.id = b.user_id where 1=1  and a.disabled ='0' and a.delFlag='0'  LIMIT 0, 20
	at org.nutz.lang.Lang.makeThrow(Lang.java:133)
	at org.nutz.dao.impl.sql.callback.EntityCallback.invoke(EntityCallback.java:20)
	at org.nutz.dao.impl.sql.NutSql.onAfter(NutSql.java:135)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:277)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:53)
	at org.nutz.plugins.cache.dao.CachedNutDaoExecutor._exec(CachedNutDaoExecutor.java:317)
	at org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:187)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:66)
	at org.nutz.dao.impl.interceptor.DaoLogInterceptor.filter(DaoLogInterceptor.java:22)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:64)
	at org.nutz.dao.DaoInterceptorChain.invoke(DaoInterceptorChain.java:139)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.runCallback(NutDaoRunner.java:147)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithoutTransaction(NutDaoRunner.java:114)
	... 70 more
 public NutMap listStudent(int length, int start, int draw, List<DataTableOrder> orders, List<DataTableColumn> columns, Cnd cnd, String linkname) {
        NutMap map = new NutMap();

        if (orders != null && orders.size() > 0) {
            for (DataTableOrder order : orders) {
                DataTableColumn col = columns.get(order.getColumn());
                cnd.orderBy(Sqls.escapeSqlFieldValue(col.getData()).toString(), order.getDir());
            }
        }
        Pager pager = new OffsetPager(start, length);
        Sql sql = Sqls.create("select a.id,a.loginname,a.phone,a.email," +
                " b.name,b.gender" +
                " from sys_user a" +
                " left join edu_user_basic_info b" +
                " on a.id = b.user_id" +
                " where 1=1 " +
                " and a.disabled ='0' and a.delFlag='0' $condition");
        sql.setCondition(cnd);
        //sql.setCallback(Sqls.callback.entities());
        sql.setPager(pager);
        sql.setCallback(new SqlCallback() {
            List<Record> permArray = new ArrayList<Record>();

            @Override
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                Set<Object> permSet = new HashSet<Object>();
                Record record = new Record();
                while (rs.next()) {
                    record.put("id",rs.getString(1));
                    record.put("loginName",rs.getString(2));
                    record.put("phone",rs.getString(3));
                    record.put("email",rs.getString(4));
                    record.put("name",rs.getString(5));
                    record.put("gender",rs.getInt(6));
                    permArray.add(record);
                }
                return permArray;
            }
        });
        dao().execute(sql);
        List<Record> list = sql.getList(Record.class);

        List<Edu_user_basic_info> userInfo=new ArrayList<>();
        List<Sys_user> user=new ArrayList<>();
        for (Record re : list) {
            Sys_user su = re.toEntity(dao().getEntity(Sys_user.class));
            Edu_user_basic_info info=re.toEntity(dao().getEntity(Edu_user_basic_info.class));
            userInfo.add(info);
            user.add(su);
        }
        map.put("userInfo",userInfo);
        map.put("user",user);
        map.put("draw", draw);
        map.put("recordsTotal", length);
        return map;
    }

实在没招了,写了这么个笨方法,倒是可行....有没有通用的适合自定义sql返回多个实体的callback

https://nutzam.com/core/dao/customized_sql.html

Sql sql = Sqls.queryRecord("select ...........");
sql.setCondition(cnd);
sql.setPager(pager);
List<Record> list = sql.getList(Record.class);

另外,你写的callback是错的, while(rs.next) 内的循环,要每次新建一个Record对象, 否则永远只有一个Record对象, 然后重复放入list而已,肯定不是你想要的效果.

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