Flask接口签名sign原理与实例代码浅析
282
2022-12-01
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
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
}
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
SELECT * FROM t_order
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$niSVW$FastClassBySpringCGLIB$$b6b2c61a.invoke(
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(
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.proceniSVWssRequest(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.StandardConthttp://extValve.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指定返回类型
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
SELECT * FROM t_order
二、查询结果返回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
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转java对象的方法有好多种,具体可以参考本人另一篇文章:
https://jb51.net/article/190478.htm
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~