【MyBatis】Failed to instantiate [org.mybatis.spring.SqlSessionFactoryBean]: No default constructor fo

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Failed to instantiate [org.mybatis.spring.SqlSessionFactoryBean]: No default constructor found

Spring 整合MyBatis出现的问题,排查过程:
1.问题出现在spring-mybatis.xml,检查了很久的代码,发现都没有语法问题,但是安装Maven依赖的时候还是会报这个错
2.检查SqlSessionFactoryBean的构造函数,是正常的
在这里插入图片描述
3.不是构造函数的问题,那就有可能是引用dataSource的问题,但是mybatis的Datasource配置在上一个模块中测试是通过的,所以排除配置问题,考虑与spring结合相关的问题
4.再次检查报错信息,猜测是class path的问题导致无法建bean,回到官网查看版本,并检查pom.xml
官网介绍的版本:https://mybatis.org/spring/index.html在这里插入图片描述
找到root cause了,与6.0+的spring framework对应的的mybatis-spring版本应该是3.0…
在这里插入图片描述
5.在排错过程中,犯了一个错误,就是多次重建依赖,根据Maven自动提示选择的版本,认为只要选择提示最高版本就不会错,而不是自己确定的应该选择什么,但这样子很容易选择错误的,导致不兼容

测通结果:
在这里插入图片描述

具体报错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Failed to instantiate [org.mybatis.spring.SqlSessionFactoryBean]: No default constructor found
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1306)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1198)
<!--省略加载过程--->
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.mybatis.spring.SqlSessionFactoryBean]: No default constructor found
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1300)
	... 45 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/core/NestedIOException
	at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
	at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3373)
	at java.base/java.lang.Class.getConstructor0(Class.java:3578)
	at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2754)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:79)
	... 46 more
Caused by: java.lang.ClassNotFoundException: org.springframework.core.NestedIOException
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
	... 51 more
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值