这就是典型的类型转换问题,出现这个错误就说明类型转换有误,大概是出现在domain层,vo层和bo层之间的混乱 1:首先在mapper层也就是dao层,我们记住我们一定是用domain实体类去接收查询
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(); } }}
来源地址:https://blog.csdn.net/justleavel/article/details/128613595
--结束END--
本文标题: 报错:Java ClassCastException: xxx cannot be cast to xxx are in unnamed module of loader ‘app‘
本文链接: https://lsjlt.com/news/414609.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0