SpringBoot使用java_jar啓動Mybatis無法識別實體類別名的解決

前言

博主github

博主個人博客http://blog.healerjean.com

噁心到吐血,找一天錯誤了,噁心啊。一直在找代碼問題,快下班了,看到了一位大神的博客,解決了問題 ,真的快抑鬱了

大神博客 https://blog.csdn.net/rainbow702/article/details/63255736

1、報錯信息

        ...] org.hibernate.jpa.internal.util.LogHelper.logPersistenceUnitInformation[31]
2020-01-14 17:46:49 INFO  -[                                ]- HHH000412: Hibernate Core {5.3.10.Final} org.hibernate.Version.logVersion[46]
2020-01-14 17:46:49 INFO  -[                                ]- HHH000206: hibernate.properties not found org.hibernate.cfg.Environment.<clinit>[213]
2020-01-14 17:46:49 INFO  -[                                ]- HCANN000001: Hibernate Commons Annotations {5.0.4.Final} org.hibernate.annotations.common.Version.<clinit>[49]
2020-01-14 17:46:51 INFO  -[                                ]- {dataSource-1} inited com.alibaba.druid.pool.DruidDataSource.init[928]
2020-01-14 17:46:51 INFO  -[                                ]- HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect org.hibernate.dialect.Dialect.<init>[157]
2020-01-14 17:46:54 INFO  -[                                ]- Initialized JPA EntityManagerFactory for persistence unit 'default' org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.buildNativeEntityManagerFactory[415]
2020-01-14 17:46:54 WARN  -[                                ]- Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requireDomainAspect': Unsatisfied dependency expressed through field 'sysDomainManager'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sysDomainManager': Unsatisfied dependency expressed through field 'sysDomainDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysDomainDao': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/healerjean/proj/config/DatasourceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'clusterSqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'URL [jar:file:/D:/workspace/origin/iku/iku-parent/iku-client-webh5/iku-client-webh5.jar!/BOOT-INF/lib/iku-data-1.0.0-SNAPSHOT.jar!/mapper/system/SysAlimamaInfoMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'URL [jar:file:/D:/workspace/origin/iku/iku-parent/iku-client-webh5/iku-client-webh5.jar!/BOOT-INF/lib/iku-data-1.0.0-SNAPSHOT.jar!/mapper/system/SysAlimamaInfoMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'SysAlimamaInfo'.  Cause: java.lang.ClassNotFoundException: Cannot find class: SysAlimamaInfo org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext.refresh[557]
2020-01-14 17:46:54 INFO  -[                                ]- Closing JPA EntityManagerFactory for persistence unit 'default' org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.destroy[597]
2020-01-14 17:46:54 INFO  -[                                ]- {dataSource-1} closed com.alibaba.druid.pool.DruidDataSource.close[1823]
2020-01-14 17:46:54 INFO  -[                                ]- Stopping service [Tomcat] org.apache.catalina.core.StandardService.log[173]

2、問題來源

1、使用Spring Boot,並使用Spring Boot的Maven插件打包

2、使用MyBatis

3、將實體Maven多模塊

4、使用 SqlSessionFactoryBean.setTypeAliasesPackage 指定包掃描Domain

然後會發現:在開發時直接使用IDEA執行main方法運行時一切正常,但是打成Jar包後使用java -jar啓動時配置的Domain別名均會失效。

3、解決方案

sqlSessionFactory.setVfs(SpringBootVFS.class);
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource)
    throws Exception {
    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(dataSource);
    sessionFactory.setMapperLocations(
        new PathMatchingResourcePatternResolver().getResources(mapperLocation));
    sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
    //MyBatis無法掃描Spring Boot別名的Bug 添加下面這行代碼
    sessionFactory.setVfs(SpringBootVFS.class);
    return sessionFactory.getObject();
}

ContactAuthor

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章