这就是典型的类型转换问题,出现这个错误就说明类型转换有误,大概是出现在domain层,vo层和bo层之间的混乱
1:首先在mapper层也就是dao层,我们记住我们一定是用domain实体类去接收查询出来的参数
2:然后我们在serviceImpl层接收到后,我们要将查询出来的数据赋值给vo层返回给前端,这里使用BeanCopyUtils将mapper层查询出来的数据赋给vo层
3:BeanCopyUtils
package com.zhi.common.utils;import cn.hutool.core.collection.CollUtil;import cn.hutool.core.lang.SimpleCache;import cn.hutool.core.map.MapUtil;import cn.hutool.core.util.ObjectUtil;import cn.hutool.core.util.ReflectUtil;import cn.hutool.core.util.StrUtil;import lombok.AccessLevel;import lombok.NoArgsConstructor;import org.springframework.cglib.beans.BeanCopier;import org.springframework.cglib.beans.BeanMap;import org.springframework.cglib.core.Converter;import java.util.List;import java.util.Map;@NoArgsConstructor(access = AccessLevel.PRIVATE)public class BeanCopyUtils { public static <T, V> V copy(T source, Class<V> desc) { if (ObjectUtil.isNull(source)) { return null; } if (ObjectUtil.isNull(desc)) { return null; } final V target = ReflectUtil.newInstanceIfPossible(desc); return copy(source, target); } public static <T, V> V copy(T source, V desc) { if (ObjectUtil.isNull(source)) { return null; } if (ObjectUtil.isNull(desc)) { return null; } BeanCopier beanCopier = BeanCopierCache.INSTANCE.get(source.getClass(), desc.getClass(), null); beanCopier.copy(source, desc, null); return desc; } public static <T, V> List<V> copyList(List<T> sourceList, Class<V> desc) { if (ObjectUtil.isNull(sourceList)) { return null; } if (CollUtil.isEmpty(sourceList)) { return CollUtil.newArrayList(); } return StreamUtils.toList(sourceList, source -> { V target = ReflectUtil.newInstanceIfPossible(desc); copy(source, target); return target; }); } @SuppressWarnings("unchecked") public static <T> Map<String, Object> copyToMap(T bean) { if (ObjectUtil.isNull(bean)) { return null; } return BeanMap.create(bean); } public static <T> T mapToBean(Map<String, Object> map, Class<T> beanClass) { if (MapUtil.isEmpty(map)) { return null; } if (ObjectUtil.isNull(beanClass)) { return null; } T bean = ReflectUtil.newInstanceIfPossible(beanClass); return mapToBean(map, bean); } public static <T> T mapToBean(Map<String, Object> map, T bean) { if (MapUtil.isEmpty(map)) { return null; } if (ObjectUtil.isNull(bean)) { return null; } BeanMap.create(bean).putAll(map); return bean; } public enum BeanCopierCache { INSTANCE; private final SimpleCache<String, BeanCopier> cache = new SimpleCache<>(); public BeanCopier get(Class<?> srcClass, Class<?> targetClass, Converter converter) { final String key = genKey(srcClass, targetClass, converter); return cache.get(key, () -> BeanCopier.create(srcClass, targetClass, converter != null)); } private String genKey(Class<?> srcClass, Class<?> targetClass, Converter converter) { final StringBuilder key = StrUtil.builder() .append(srcClass.getName()).append('#').append(targetClass.getName()); if(null != converter){ key.append('#').append(converter.getClass().getName()); } return key.toString(); } }}
出现上述错误大概是在想要get到mapper层查询数据的List<实体类>时候出现了错误,一定要记住mapper层查询出来的是对应实体层的,而不是vo层
来源地址:https://blog.csdn.net/justleavel/article/details/128613595