springboot整合mybatis时Could not resolve type alias 'xxx'

注意:不管用下列哪种解决方法,都要在启动类中加入(总结: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???怎么弄??

我的想法是重写这个方法,https://blog.csdn.net/doctor_who2004/article/details/70163144

下面是我的解决过程,记录一下,好奇怪啊!!!

我直接在启动类里面添加运行后,再注释掉

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();
//	}
}

 

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
SpringBoot整合Mybatis,出现"Invalid bound statement (not found)"这个问题的实质是mapper接口和mapper.xml没有正确地映射起来。 为了解决这个问题,你可以按照以下步骤进行操作: 1. 首先,确认你的mapper接口和mapper.xml文件位于正确的位置,并且它们的命名规范是一致的。通常情况下,mapper接口应该位于`com.smbms.mybatis.mapper`包下,而mapper.xml文件应该位于`resources/mapper`目录下。 2. 确保在主配置文件(即application.properties或application.yml)中添加了正确的配置。根据引用中的代码示例,你需要在application.properties文件中添加如下配置: ``` mybatis.typeAliasesPackage=com.smbms.mybatis.popj mybatis.mapperLocations=classpath:mapper/*.xml ``` 这样配置后,Mybatis就能正确地找到mapper.xml文件并与mapper接口进行映射。 3. 最后,重新编译和运行你的项目,确保没有其他错误导致该问题。如果问题仍然存在,可以检查一下mapper接口中的方法名和mapper.xml文件中的SQL语句是否一致,另外也可以尝试重新生成mapper接口和mapper.xml文件。 通过以上的步骤,你应该能够解决"Invalid bound statement (not found)"的问题,并成功实现SpringBootMybatis整合。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [springboot整合mybatis出现Invalid bound statement (not found)](https://blog.csdn.net/weixin_56044831/article/details/116863995)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [SpringBoot整合mybatis报Invalid bound statement (not found)错误的可能原因](https://blog.csdn.net/wsaicyj/article/details/124286481)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值