springboot3整合mybatis遇到的坑

本人不经常写java,本文仅作问题记录,如有问题请把不吝赐教。

坑1、Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
	at org.springframework.util.Assert.notNull(Assert.java:204)
	at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:122)
	at org.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:73)
	at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1797)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1747)
	... 107 more

因为我用的是springboot3整合mybatis,没有现成的文档,所以参考的之前自己整合了,在pom.xml引入的mybatis(version2.2.2,自动引入的)如下。


    org.mybatis.spring.boot
	mybatis-spring-boot-starter
	2.2.2

 配置好之后编译过程开始报Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required的错,自己对照查看各种配置,网上对该问题的解决方案(很多都是一样的),都没有解决我的问题,我想肯定还是我的问题,我对java了解十分有限,于是仔细阅读各种文档配置加上自己前端上的理解,思来想去有没有一种可能是2.2.2版本的mybatis跟spring boot3不对版,搜索资料发现,果然跟我猜想的一样,spring boot3 整合mybatis要求mybatis-spring-boot-starter的最低版本是3.0.0。

思考:按照文档用spring boot 整合很多jar包过程中如果出现奇怪的问题时,是不是可以把选择合适的版本作为一种解决方案。

坑2、idea中显示mybatis-spring-boot-starter 3.0.1版本不存在

spring boot3 整合mybatis要求mybatis-spring-boot-starter的最版本是3.0.0,于是我把mybatis的version改成支持的3.0.1,神奇事情发生,idea一直提示不存在3.0.1版本的mybatis,重新加载依赖,重启idea也不管用,如下图

springboot3整合mybatis遇到的坑_第1张图片

 但是阿里云的仓库中是有的springboot3整合mybatis遇到的坑_第2张图片

 于是我想把mybatis3.0.1对应pom和jar下载下来放到对应的目录下,问题就应该能解决吧,然后我切换到本地仓库盘符下准备拷贝文件,看到有个unkown文件夹(如下图),里面空空如也,这是个什么,知觉告诉这不是好东东,于是先把它删除,去idea中重新load maven change,神奇的现象发生了,pom中3.0.1正常显示了。让idea正常加载这种情况的下包是不是还有其它办法,请不吝赐教。

springboot3整合mybatis遇到的坑_第3张图片

补充:让idea正常加载包的方式一,找到设置-编译-maven-always update snapshots,然后勾选springboot3整合mybatis遇到的坑_第4张图片

 

 

思考:当maven中央仓库中有这个版本,而idea显示无此版本,是不是可以去本地仓库底下看看有没有unkown文件夹

坑三、No qualifying bean of type 'com.example.quit2.dao.AyUserDao'

解决完坑二,继续编译,出现如图所示问题

[ERROR] testMybatis  Time elapsed: 0 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext for [WebMergedContextConfiguration@4052c8c2 testClass = com.example.quit2.Quit2ApplicationTests, locations = [], classes = [com.example.quit2.Quit2Application], contextInitializerClasses = [], activeProfiles = [], propertySourceLocations = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@4b8ee4de, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@4659191b, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@3527942a, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@9da1, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@60099951, org.springframework.boot.test.context.SpringBootTestAnnotation@9a402e2], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ayUserServiceImpl': Injection of resource dependencies failed
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.quit2.dao.AyUserDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@jakarta.annotation.Resource(shareable=true, lookup="", name="", description="", authenticationType=CONTAINER, type=java.lang.Object.class, mappedName="")}

很明显错误bean找不到,why?到这里我感觉配置是不是还是有问题,于是又检查了一遍,no problem,这个接口是加了Mapper注解,为啥没加载到。

springboot3整合mybatis遇到的坑_第5张图片

于是乎,就到网上找各种方案挨个试,没解决,心态奔溃(应该是方案不对路数),当天放弃尝试,第二天放平心态重新来搞,果然发现了端倪,启动类上@MapperScan中的dao写成了doa,but接口上方中注解Mapper为啥不生效呢,那真相只有一个,注解MapperScan的优先级更高,当出现@MapperScan时,接口上方的@Mapper就被忽略掉,而我的工程目录下又没有doa这个package,所以扫描不到,会报错。
springboot3整合mybatis遇到的坑_第6张图片

@MapperScan(“com.example.quit2.dao”)扫描注解应该可以这样理解,将这个package包下所有的都注解成Mapper

你可能感兴趣的:(java,mybatis,java,spring)