spring boot 与mybatis整合,type-aliases-package、type-handlers-package等配置不起作用,导致类加载失败

认知科技技术团队 发布于 2017/04/14 09:07
阅读 26K+
收藏 0

刚刚接触spring boot,项目中整合了mybatis,但配置没用mybatis-spring-boot-autoconfigure自动配置,

导致mybatis的配置:

 

mybatis.type-aliases-package=com.example.domain.model
mybatis.type-handlers-package=com.example.typehandler

java -jar  xxx.war 运行方式,自动扫描机制不起作用,发生类加载失败异常:

 

     Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'XXXXX'.  Cause: Java.lang.ClassNotFoundException: Cannot find class: XXXXX

    一开始猜测mybatis在spring boot环境中的bug,扫描这些包中的类,但类加载找不到,肯定找的路径不对,所以肯定是这一块出现了问题,搜了下github上的issue,找到了关键点:

 

org.mybatis.spring.SqlSessionFactoryBean
public void setVfs(Class<? extends org.apache.ibatis.io.VFS> vfs)

 

spring boot这种运行方式的文件系统与之前的不一样,所以mybatis需要设置spring boot vsf。

 

 

sqlSessionFactoryBean.setVfs(SpringBootVFS.class);

 

http://blog.csdn.net/doctor_who2004/article/details/70163144

 由于源码这块还没好好看,如果你看过源码,也会看到mybatis-spring-boot-autoconfigure中的配置:

 

MybatisAutoConfiguration类:
 
 
@Bean @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setVfs(SpringBootVFS.class);

 

 

最后一行的配置,说明了mybatis对应spring boot环境的配置,

 

 

至于怎么扫描的,请看源码

 
SpringBootVFS

 

来源:http://blog.csdn.net/doctor_who2004/article/details/70163144

加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部