springboot项目引入通用mapper(tk.mybatis.mapper)的时候踩的坑:
报错信息:
java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
看到这个错误信息,需要查看我们引用的mybatis的配置文件:
第一步查看MyMapper:
package com.mybatis;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
* 特别注意,该接口不能被扫描到,否则会出错 继承自己的MyMapper
*
* @param <T>
*/
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
注意一下,这个MyMapper的路劲;
第二步查看nacos中我们配置的mybatis的路劲:
mybatis:
type-aliases-package: com.**.entity
mapper-locations: classpath*:mappers/*.xml
configuration:
map-underscore-to-camel-case: true
use-generated-keys: true
mapper:
mappers:
- com.xxx.mybatis.MyMapper
not-empty: false
identity: MYSQL
style: normal
启动类引入的@MapperScan 引入的是import tk.mybatis.spring.annotation.MapperScan;
同时要注意 @MapperScan里的basePackage不能包含通用mapper(我的是BaseDao)的路径,只包含其他的mapper的路径,不然会报错
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘baseDao’ defined in file [BaseDao.class]: Invocation of init method failed;
nested exception is tk.mybatis.mapper.MapperException: tk.mybatis.mapper.MapperException: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class