解决Springboot2.x+Mybatis提示Could not resolve type alias 'xxx'的问题

报错信息(节选):

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'accountController': 
Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'accountService': Unsatisfied dependency expressed through field 'accountMapper'; 
Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Account'.  Cause: java.lang.ClassNotFoundException: Cannot find class: Account

问题概述:

此问题主要是在XML中引用类型的时候,直接使用了Alias,而因为Springboot和Mybatis之间存在一些"小摩擦",结合的不够完美导致(本人用的springboot2.1.4版本依然未解决)。具体原因分析网上较多,可惜大部分对解决方案都语焉不详。本文将解决方案完整贴出来,方便后来者,同时也给自己做个备忘。

解决方案:

  1. 弃用Alias,直接在xml中需要引入Entity类型的地方直接引入类的全路径+类名。
    网上找到的大部分都是这个方案,个人感觉这不算是个优雅的解决方案。特别是有些复杂的业务处理xml,返回类型也较多的场景,看着满目都是resultType='xxx.xxx.xxx.xxx.xxx.Xxxx’绝对不是一件让人愉快的事情,分分钟逼死强迫症。
  2. 手动设置SqlSessionFactoryBean, 设置VFS为SpringBootVFS。

新建一个配置类MybatisConfiguration,手动注入SqlSessionFactoryBean。

@Configuration
public class MybatisConfiguration {
    // 数据源
    @Resource
    private DataSource dataSource;

    // 资源文件(application.yml文件中mybatis下级资源)读取器
    @Resource
    private MybatisProperties properties;

    /**
     * 设置SqlSessionFactory, 并设置手动设置VFS为SpringBootVFS
     */
    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactoryBean sqlSessionFactory() {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(this.dataSource);
        bean.setVfs(SpringBootVFS.class); // 关键,将vfs设置为SpringBootVFS
        bean.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); // 既然手动了,别忘了把其他的配置一起加入
        bean.setMapperLocations(this.properties.resolveMapperLocations());
        return bean;
    }
}

application.yml部分,喜欢用application.properties的同学自行转换:

mybatis:
  #扫描Entity, 加入此设置方可在Mybatis的XML文件中使用实体类的Alias
  typeAliasesPackage: com.teamsoft.springboot2.*.entity
  #扫描Mybatis的XML文件,
  mapperLocations: com/teamsoft/springboot2/*/mapper/*.xml

本人原创于简书: https://www.jianshu.com/p/81e836106f81

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值