主要解决springboot项目引入通用mapper(tk.mybatis.mapper)的时候一些可能会踩的坑:诸如tk.mybatis.mapper.provider.base.BaseSelectProvider.()

java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class

 

  • 通用mapper
    import tk.mybatis.mapper.common.Mapper;
    import tk.mybatis.mapper.common.MySqlMapper;
    
    
    public interface BaseDao extends Mapper, MySqlMapper{
    }

     

  • pom 文件添加 Maven 的依赖 (同时要注意有没有冲突)

     tk.mybatis
     mapper-spring-boot-starter
     2.0.0
  • yml配置加上通用mapper路径(我的通用mapper叫BaseDao)
mapper:
    mappers: cn.hy.hyerp.erp.common.dal.BaseDao
    not-empty: false
    identity: mysql
  • 启动类引入的@MapperScan 引入的是import tk.mybatis.spring.annotation.MapperScan;

启动而不是import org.mybatis.spring.annotation.MapperScan;​​​​​​不然会报错:

Cause: java.lang.InstantiationException: tk.mybatis.mapper.provider.base.BaseSelectProvider] with root cause
java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider.()

 

  • @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

 

  • 使用BaseDao的方法如果SQL语句报错,注意在自己生成的entity里面加上@Table 和 @Column 标识entity对应的表名和字段名;通用mapper默认是将驼峰结构的字段转化为下划线的结构,如调.selectAll()方法时,personType会默认转为
    person_type,如果跟数据表的字段不对应会报错。如我在数据表的字段名为personType时,加上注解就行。
        @Column(name = "personType")
        private Boolean personType;

相关文章:

  • 2021-08-25
  • 2021-10-13
  • 2021-08-01
  • 2022-12-23
  • 2021-10-11
  • 2021-11-20
  • 2021-09-30
  • 2021-10-19
猜你喜欢
  • 2021-11-20
  • 2021-04-11
  • 2021-09-29
  • 2021-11-29
  • 2021-07-31
  • 2019-07-19
  • 2022-12-23
相关资源
相似解决方案