栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ClassGenricType not found

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ClassGenricType not found

ClassGenricType not found

在使用 Mybatis-plus 进行批量插入时遇到了 ClassGenricType not found 这个异常。具体的说就是 entity的 type 类型得不到对应。
发生的地方在使用 ServiceImpl 作为超类的情况。

网上的解决方法是 1. 你可能没有具体指定 T 的类型
// 不指定泛型类型
public class TestService extends ServiceImpl {
}

// 不指定泛型类型
public interface TestMapper extends baseMapper {
}

所以你只需要指定好对应的泛型类型即可

// 指定泛型类型
public class TestService extends ServiceImpl {
}

// 指定泛型类型
public interface TestMapper extends baseMapper {
}
2. 你没有在实体类上对应表
@TableName("test")
public class Test {
}
我遇到的问题原因以上都不是
public class MybaseService extends ServiceImpl {
}
public class TestService extends MybaseService {
}

由于自己又搞了个base类继承ServiceImpl,所以当进行批量添加的时候,ClassGenricType not found。原因是

// mybatis-plus 中 entityClass 
protected Class entityClass = this.currentModelClass();

// 取得 entityClass 注意传递了 1
protected Class currentModelClass() {
    return ReflectionKit.getSuperClassGenericType(this.getClass(), 1);
}

// 大概意思就是获取父类 位置是1 的泛型
public static Class getSuperClassGenericType(final Class clazz, final int index) {
    Type genType = clazz.getGenericSuperclass();
    if (!(genType instanceof ParameterizedType)) {
        logger.warn(String.format("Warn: %s's superclass not ParameterizedType", clazz.getSimpleName()));
        return Object.class;
    } else {
        Type[] params = ((ParameterizedType)genType).getActualTypeArguments();
        if (index < params.length && index >= 0) {
            if (!(params[index] instanceof Class)) {
                logger.warn(String.format("Warn: %s not set the actual class on superclass generic parameter", clazz.getSimpleName()));
                return Object.class;
            } else {
                return (Class)params[index];
            }
        } else {
            logger.warn(String.format("Warn: Index: %s, Size of %s's Parameterized Type: %s .", index, clazz.getSimpleName(), params.length));
            return Object.class;
        }
    }
}

所以获取父类 MybaseService 位置为1的泛型类型即为V, 而不是T

public class MybaseService extends ServiceImpl {
}

总结:泛型位置也很重要。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/685921.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号