主要解决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;