温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mybatis查询结果返回至实体类的方法

发布时间:2020-07-11 10:43:25 来源:亿速云 阅读:1113 作者:清晨 栏目:开发技术

这篇文章将为大家详细讲解有关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();
}
<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<!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指定返回类型

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<!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();
}

关于mybatis查询结果返回至实体类的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI