mybatis查询结果返回至实体类的示例代码
作者:流云一号
近期,利用mybatis做一个简单查询,先看主要代码:
Service层:
package com.example1.service; import java.util.List; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example1.dao.OrderMapper; import com.example1.pojo.Order; @Service public class OrderService { private Logger log = Logger.getLogger(OrderService.class); @Autowired private OrderMapper orderMapper; public void findOrders() { List<Order> orders = orderMapper.findOrders(); for (Order order : orders) { log.info(order.getId() + " : " + order.getName()); } } }
dao层:
package com.example1.dao; import java.util.List; import com.example1.pojo.Order; public interface OrderMapper { public List<Order> findOrders(); }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example1.dao.OrderMapper"> <select id="findOrders" parameterType="String" resultType="java.util.Map"> SELECT * FROM t_order </select> </mapper>
Order类:
package com.example1.pojo; public class Order { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Order [id=" + id + ", name=" + name + "]"; } }
配置妥当,执行,结果报错了:
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.example1.pojo.Order
at com.example1.service.OrderService.findOrders(OrderService.java:26)
at com.example1.service.OrderService$$FastClassBySpringCGLIB$$b6b2c61a.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at com.example1.service.OrderService$$EnhancerBySpringCGLIB$$e285c4bb.findOrders(<generated>)
at com.example1.controller.OrderController.findOrders(OrderController.java:22)
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.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
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:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:617)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1778)
at java.lang.Thread.run(Thread.java:745)
可以看出,java.util.HashMap不能直接存储到具体的实体类。
解决办法:
一、dao层通过resultType指定返回类型
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example1.dao.OrderMapper"> <select id="findOrders" parameterType="String" resultType="com.example1.pojo.Order"> SELECT * FROM t_order </select> </mapper>
二、查询结果返回list,然后遍历list,将map转换成相应的java对象
修改后的Service层:
package com.example1.service; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.example1.dao.OrderMapper; import com.example1.pojo.Order; @Service public class OrderService { private Logger log = Logger.getLogger(OrderService.class); @Autowired private OrderMapper orderMapper; public void findOrders() { List<Map> orders = orderMapper.findOrders(); for (Map orderMap : orders) { String orderJsonStr = JSONObject.toJSONString(orderMap); Order order = (Order) JSONArray.parseObject(orderJsonStr,Order.class); log.info(order.getId() + " : " + order.getName()); } } }
dao层:
package com.example1.dao; import java.util.List; import java.util.Map; public interface OrderMapper { public List<Map> findOrders(); }
其中,关于map转java对象的方法有好多种,具体可以参考本人另一篇文章:
https://www.jb51.net/article/190478.htm
以上两种方法均实际测试通过。到此这篇关于mybatis查询结果返回至实体类的示例代码的文章就介绍到这了,更多相关mybatis结果返回实体类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!