1

以下是我的代码。我正在查询 user_group 表并在列表中获取结果。我想迭代列表。但是得到如下异常。

    List<Group> list= empDAO.getStudentList();
    for(Group o :list){
        System.out.println("NAME :"+ o.getFirstName());
    }

这是我的 DAO 方法

public List<Group> getStudentList() {
    System.out.println("INSIDE DAO");
    List<Group> groups = new ArrayList<Group>();

    List<Group> rows = jdbcTemplate.queryForList("select * from user_group");

    return rows;
}   

组类

public class Group {
    public Group() {
        // TODO Auto-generated constructor stub
    }

    private String firstName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
}

我收到以下异常

Aug 3, 2012 9:22:47 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet mvc-dispatcher threw exception
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.common.form.Group
        at com.common.controller.HelloWorldController.helloWorld(HelloWorldController.java:28)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:421)
        at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:136)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:326)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:313)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
        at java.lang.Thread.run(Thread.java:619)

谁能告诉如何解决这个错误?

4

2 回答 2

7

该行jdbcTemplate.queryForList("select * from user_group");返回 HashMap 列表,而不是 Group 项列表(您的 IDE 可能在该行显示警告)。

您可能想阅读有关 jdbcTemplate 的 Spring 文档,并且您可能想使用RowMapper将每一行转换为 Group 对象。

我还认为您想将该方法jdbcTemplate.query(String sql, RowMapper rm)用于您的用例,请检查javadoc

于 2012-08-03T16:25:55.383 回答
3

jdbcTemplate.queryForList不返回 a List<Group>,它不知道 aGroup是什么。它返回一个List<Map<String,Object>>行列表,每行一个从列名到其值的映射。

所以这样做:

for (Map<String,Object> m : jdbcTemplate.queryForList("select * from user_group")) {
    for (Map.Entry<String,Object> e : m.entrySet()) {
        String columnName = e.key;
        Object columnValue = e.value;
        ...build a Group somehow?...
    }
}
于 2012-08-03T16:28:33.897 回答