背景:使用Mybatis-puls生成实体类、xml、service文件,测试代码时,发现调用selectList(wrapper)方法时,报错java.lang.ClassCastException: java.util.Map cannot be cast to xxx.class,类型转换错误的问题。

通过debug发现,selectList(wrapper)返回的类型是List<xxxx.class>,里面是实体类,但真实情况确实map类型的,并不是我们要的实体类类型,这点通过debug就能发现。

解决方案,将map类型转换成debug类型。

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

//这里通过debug可以发现,listOld里面的是map类型,并不是TestDTO实体类
List<TestDTO> listOld = satCourtCaseSV.selectList(wrapper);
ObjectMapper mapper = new ObjectMapper();
//将map转成实体类类型
List<TestDTO> list = mapper.convertValue(listOld, new TypeReference<List<TestDTO>>() { });
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐