NutzCN Logo
问答 怎样用sql语句查一组数字
发布于 3034天前 作者 老司机 1758 次浏览 复制 上一个帖子 下一个帖子
标签:
public String getNodesByRoomID() {
		Sql sql = Sqls.create("select child_id from \"T_Entity_Mapping\" where parent_id=8");
		sql.setCallback(Sqls.callback.ints());
		dao.execute(sql);
		List<Integer> ids = sql.getList(Integer.class);
		return ids.toString();
		// return "ok";
	}

sql运行出来应该是多条结果,每条结果就一个数字我想得到一个数字的集合或者数组
结果报错

java.lang.ClassCastException: [I cannot be cast to java.util.List
	at org.nutz.dao.impl.sql.NutStatement.getList(NutStatement.java:113)
	at net.myDCM.module.EntityModule.getNodesByRoomID(EntityModule.java:25)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	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:40)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:113)
	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:40)
	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:183)
	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:199)
	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(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Unknown Source)

求解答

9 回复
List<Integer> ids = sql.getList(Integer.class);

改成

int[] ids = sql.getObject(int[].class);

@wendal 那我要查询复杂情况下的复杂结果呢?比如

select str0,str1,int0,str2,int3 from .........

并没有一个model去给这些数据提供class,怎样才能拿到这些数据呢?getWhat?

取决callback的类型

Sql sql = Sqls.fetchRecord("select str0,str1,int0,str2,int3 from dual");
dao.execute(sql);
Record re = sql.getObject(Record.class);

@wendal 那再多问一句,怎么用ORM去进去多表查询,没有看到内联外联方法嘛

@qq_c1bab051 没看懂

来自炫酷的 NutzCN

@wendal 比如说,

selet a.name,b.age from table_a as a ,table_b as b where a.id=b.id and a.name in... and b.age>.....

并且我又a、b对应的POJO,然后怎么用dao.query(class,Cnd)的方式去实现去实现这句查询

@qq_c1bab051 先按record取出,然后调用toEntity方法,带前缀

来自炫酷的 NutzCN

@wendal 就是说多表查询只能写sql咯?nutz没有提供ORM方法?

@qq_c1bab051 是的

来自炫酷的 NutzCN

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