注意:不管用下列哪种解决方法,都要在启动类中加入(总结:springboot整合mybatis要扫描三个东西:mapper接口类,映射文件xxxmapper.xml,实体类):
//指定mapper接口类的位置
@MapperScan(basePackages= "com.wordpython.mapper")
解决方案:
一:mapper.xml中的resultType中经常会用到一些自定义POJO,你可以用完全限定名来指定这些POJO的引用,例如
<select id="getUsers" resultType="com.majing.learning.mybatis.entity.User">
。
二:通过在application.properties中指定POJO扫描包来让mybatis自动扫描到自定义POJO,如下:
#配置数据库实体对象的位置和mapper映射文件的位置
mybatis.type-aliases-package=com.wordpython.po
mybatis.mapper-locations=classpath:com/wordpython/mapper/*.xml
!然后,如果使用第二种扫描包的方法,有可能还是Could not resolve type alias 'xxx',
解决办法:
参考博主A_Beaver的文章,原来mybatis的facroty需要加载SpringBoot独特的虚拟文件系统,才能识别类路径
public SpringBootVFS() {
this.resourceResolver = new PathMatchingResourcePatternResolver(getClass().getClassLoader());
}
从以上代码看,其实是通过PathMatchingResourcePatternResolver
实现资源的加载
修复该问题只需要在mybatis的配置类中,设置一下factory即可,
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setVfs(SpringBootVFS.class);//设置SpringBootVFS
bean.setTypeAliasesPackage("com.fulan.domain.red");//添加这一句
...
}
问题来了,在mybatis的配置类中设置factory???怎么弄??
下面是我的解决过程,记录一下,好奇怪啊!!!
我直接在启动类里面添加运行后,再注释掉
package com.wordpython;
import javax.sql.DataSource;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@SpringBootApplication // 一个注解顶下面3个
//@SpringBootConfiguration
//@EnableAutoConfiguration//这里有了,其他controller就不用写了
//@ComponentScan
//指定mapper类的位置
@MapperScan(basePackages = "com.wordpython.mapper")
public class ApplicationStart {
public static void main(String[] args) {
SpringApplication.run(ApplicationStart.class, args);
}
/*
* 好奇怪啊!
* 为什么把下面的注释的呢,是这样的,没加下面代码之前,报错Could not resolve type alias 'User',
* 加了之后,报其他错误,注释掉后,重新运行,没有报任何错包括Could not resolve type alias 'User',运行成功
*/
// @Autowired
// private MybatisProperties properties;
// @Autowired
// private ResourceLoader resourceLoader = new DefaultResourceLoader();
// @Autowired(required = false)
// private Interceptor[] interceptors;
// @Autowired(required = false)
// private DatabaseIdProvider databaseIdProvider;
//
// @Bean(name = "masterSqlSessionFactory")
// @Primary
// public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
// SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
// factory.setDataSource(dataSource);
// factory.setVfs(SpringBootVFS.class);
// factory.setTypeAliasesPackage("com.wordpython.po");//mybatis.type-aliases-package还是扫描不到包,所以要添加这一句
// if (StringUtils.hasText(this.properties.getConfigLocation())) {
// factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
// }
// factory.setConfiguration(properties.getConfiguration());
// if (!ObjectUtils.isEmpty(this.interceptors)) {
// factory.setPlugins(this.interceptors);
// }
// if (this.databaseIdProvider != null) {
// factory.setDatabaseIdProvider(this.databaseIdProvider);
// }
// if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
// factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
// }
// if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
// factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
// }
// if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
// factory.setMapperLocations(this.properties.resolveMapperLocations());
// }
// return factory.getObject();
// }
}