溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mybatis查詢結(jié)果返回至實體類的方法

發(fā)布時間:2020-07-11 10:43:25 來源:億速云 閱讀:1106 作者:清晨 欄目:開發(fā)技術

這篇文章將為大家詳細講解有關mybatis查詢結(jié)果返回至實體類的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

近期,利用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 + "]";
 }
}

配置妥當,執(zhí)行,結(jié)果報錯了:

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>

二、查詢結(jié)果返回list,然后遍歷list,將map轉(zhuǎn)換成相應的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查詢結(jié)果返回至實體類的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI