NutzCN Logo
问答 自定义sql,获取list,获取不到,list为null
发布于 2487天前 作者 码农 2612 次浏览 复制 上一个帖子 下一个帖子
标签:
@IocBean
public class TestServiceImpl implements TestService {
    @Inject
    private Dao dao;

    @Override
    public List<Sys_user> getList() {
        String sql = "SELECT * FROM SYS_USER";
        Sql sql1 = Sqls.create(sql);
        dao.execute(sql1);
        List<Sys_user> list = sql1.getList(Sys_user.class);
        return list;
    }
}
24 回复
String sql = "SELECT * FROM SYS_USER";
        Sql sql1 = Sqls.create(sql);
        sql1.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                List<String> list =new ArrayList<String>();
                while(rs.next()){
                        list.add(rs.getString("id"));
                        list.add(rs.getString("name"));
                }
                return list;
            }
        });
        dao.execute(sql1);
        List<String> list = sql1.getList(String.class);
        return list;
org.nutz.dao.DaoException: !Nutz SQL Error: 'SELECT * FROM SYS_USER'
PreparedStatement: 
'SELECT * FROM SYS_USER'
CaseMessage=������Ч
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:104)
	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:158)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithoutTransaction(NutDaoRunner.java:125)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._run(NutDaoRunner.java:92)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:81)
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:240)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:252)
	at org.nutz.dao.impl.DaoSupport.execute(DaoSupport.java:236)
	at org.nutz.dao.impl.NutDao.execute(NutDao.java:1008)
	at com.zsxx.web.service.impl.TestServiceImpl.getList(TestServiceImpl.java:42)
	at com.zsxx.web.controller.MainModule.index(MainModule.java:21)
	at com.zsxx.web.controller.MainModule$FM$index$bc2b82b7b93ecb85001cf60bdd35a275.invoke(MainModule.java)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:31)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:30)
	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:202)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1613)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:541)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1593)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
	at org.eclipse.jetty.server.handler.ContextHandler.__doHandle(ContextHandler.java:1239)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:481)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1562)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1141)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)
	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:118)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.Server.handle(Server.java:564)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
	at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.sql.SQLException: ������Ч
	at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3711)
	at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:2763)
	at oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:494)
	at com.alibaba.druid.filter.FilterChainImpl.resultSet_getString(FilterChainImpl.java:907)
	at com.alibaba.druid.filter.stat.StatFilter.resultSet_getString(StatFilter.java:949)
	at com.alibaba.druid.filter.FilterChainImpl.resultSet_getString(FilterChainImpl.java:905)
	at com.alibaba.druid.proxy.jdbc.ResultSetProxyImpl.getString(ResultSetProxyImpl.java:693)
	at com.alibaba.druid.pool.DruidPooledResultSet.getString(DruidPooledResultSet.java:257)
	at com.zsxx.web.service.impl.TestServiceImpl$1.invoke(TestServiceImpl.java:37)
	at org.nutz.dao.impl.sql.NutSql.onAfter(NutSql.java:137)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:277)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:53)
	... 58 more

为啥不用内置的callback呢? 很明显表字段名不是id/name, 导致取值错误

@Wizzercn 内置callBack是什么

Sqls.callback.XXXX

@Wizzercn 官网有相关文档吗

@Wizzercn 输入控制台中文乱码怎么解决呢?

@Wizzercn jetty:run会中文乱码怎么解决?

@wendal 我用了records取出来的list是null

@wendal

    List<String> ids = sql.getList(String.class);
        Sql sql1 = Sqls.create("SELECT a.* FROM sys_menu a,sys_role_menu b WHERE a.id=b.menuId AND b.roleId in (@ids)");
        sql1.setParam("ids", ids.toArray());

        sql1.setCallback(Sqls.callback.records());
        list =  sql1.getList(Sys_menu.class);
java.lang.NullPointerException
	at cn.wizzer.app.web.modules.controllers.platform.sys.SysRoleController.add(SysRoleController.java:82)

这个是controller82的代码 list返回的是null
        for (Sys_menu menu : list) {

没调用dao.execute

来自炫酷的 NutzCN

java.lang.ClassCastException: org.nutz.dao.entity.Record cannot be cast to cn.wizzer.app.sys.modules.models.Sys_menu

没看过文档吧?回调跟返回值相关的

来自炫酷的 NutzCN

  Sql sql1 = Sqls.create("SELECT a.* FROM sys_menu a,sys_role_menu b WHERE a.id=b.menuId AND b.roleId in (@ids)");
        sql1.setParam("ids", ids.toArray());
        this.execute(sql1);
        sql1.setEntity(sysMenuService.getEntity(Sys_menu.class));
        sql1.setCallback(Sqls.callback.entities());
        list = sysMenuService.execute(sql1).getList(Sys_menu.class);

这种方式是能实现的,但是getEntity有bug

java.lang.StackOverflowError
	at cn.wizzer.framework.base.service.BaseServiceImpl.getEntity(BaseServiceImpl.java:37)

dao.getEntity

来自炫酷的 NutzCN

那为什么我用service不能调这个getEntity呢

因为大鲨鱼写了个bug

来自炫酷的 NutzCN

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