Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias ‘xxx‘.

错误信息:

org.apache.ibatis.type.TypeException: Could not resolve type alias 'systemConfig'.  Cause: java.lang.ClassNotFoundException: Cannot find class: systemConfig
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:123)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:95)
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:610)
	... 72 common frames omitted
Caused by: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'systemConfig'.  Cause: java.lang.ClassNotFoundException: Cannot find class: systemConfig
	at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:118)
	at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:102)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:138)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(XMLMapperBuilder.java:131)
	at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:121)
	... 74 common frames omitted
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'systemConfig'.  Cause: java.lang.ClassNotFoundException: Cannot find class: systemConfig
	at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:120)
	at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:149)
	at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:116)
	... 78 common frames omitted
Caused by: java.lang.ClassNotFoundException: Cannot find class: systemConfig
	at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:200)
	at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89)
	at org.apache.ibatis.io.Resources.classForName(Resources.java:261)
	at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:116)
	... 80 common frames omitted

上面错误信息,说找不到别名systemConfig,可我已经在application.properties文件中配置了别名路径,配置如下:

#mybatis配置
mybatis.type-aliases-package=com.net.pojo

后面尝试加入@Alias注解,任然报同样的错误

解决

原因:在项目中配置了多数据源情况下,application.properties配置的别名包路径不会起作用,需要在SqlSessionFactory对象中配置typeAliasesPackage属性,如下:

    @Bean(name = "osSqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("osDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage("com.net.pojo");
        bean.setVfs(SpringBootVFS.class);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/os/**/*.xml"));
        return bean.getObject();
    }

网上其他解决方案:

  • 将resultMap id 放在 select标签的resultType属性使用了
    resultType是用于表示select语句返回的具体类型,这里可以使用全限定类名,与别名
    而resultMap则是用来映射类属性与字段关系的,在select标签中,使用时,需使用resultMap属性填写resultMap标签id
  • 在单数据源配置时,没有在application文件中,配置mybatis.type-aliases-package,或配置了有空格等格式错误的,需要仔细检查

其他

以下贴出多数据源mybatis+springBoot所有配置:
application.properties:

# 数据库访问配置 主
# 数据库连接池使用类型
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.os.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
#将database_name改成自己的数据库的名称,多数据库配置必须使用jdbc-url配置,而不是常用的url,否则后续运行将报错
spring.datasource.os.jdbc-url=jdbc:sqlserver://172.16.58.87:1433;DatabaseName=OS
spring.datasource.os.username=sa
spring.datasource.os.password=12345678


# 数据库配置 
spring.datasource.osx.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
#将database_name改成自己的数据库的名称,多数据库配置必须使用jdbc-url配置,而不是常用的url,否则后续运行将报错
spring.datasource.osx.jdbc-url=jdbc:sqlserver://172.16.58.180:1433;DatabaseName=OSX
spring.datasource.osx.username=sa
spring.datasource.osx.password=12345678

# 日志配置
logging.file.path=E:\\OS_Log
logging.file.name=springbootdemo.log
# 日志(根目录)级别
logging.level.com.net=debug
# 控制台输出配置  时间 [线程名称] 日志级别 日志输出者名称-日志消息
logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} %clr(%5p) [%thread] %-5level %logger-- %msg%n
logging.pattern.file=%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger-- %msg%n

多数据库mybatis配置信息,注册到spring中,与原xml配置方式对应:

@Configuration
@MapperScan(basePackages = "com.net.os.dao", sqlSessionTemplateRef = "osSqlSessionTemplate")//mapper 接口包扫描路径
public class DataSourceOmmpConfig {
    @Bean(name = "osDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.os")//与application.properties前缀相同
    @Primary
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "osSqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("osDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage("com.net.pojo");//别名包扫描路径
        bean.setVfs(SpringBootVFS.class);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/os/**/*.xml"));//mapping xml文件路径
        return bean.getObject();
    }

    @Bean(name = "osTransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("osDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "osSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("osSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}

另一个数据源配置信息:

@Configuration
@MapperScan(basePackages = "com.net.osx.dao",sqlSessionTemplateRef = "osxSqlSessionTemplate")
public class DataSourceCitrixConfig {

    @Bean(name = "osxDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.osx")
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "osxSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("osxDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage("com.net.osx.pojo");
        bean.setVfs(SpringBootVFS.class);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/osx/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "osxTransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("osxDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "osxSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("osxSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}

你可能感兴趣的:(springBoot,myBatis,java,mybatis,spring,boot,数据库)