spring boot集成mybatis报错 java.lang.IllegalStateException: No supported DataSource type found

背景

我参考MyBatis-Spring的文档搭建环境
https://mybatis.org/spring/zh/getting-started.html
完全按文档上操作,还是报以下错误。

报错信息

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-08-08 10:56:07.836 ERROR 16149 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [cn/wsdmteach/bi/conf/DatasourceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: No supported DataSource type found
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.22.jar:5.3.22]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486) ~[spring-beans-5.3.22.jar:5.3.22]

解决办法

pom.xml增加以下依赖:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>

重新启动项目,问题解决了。

总结

我本来以为是文档有问题,最终我发现实际上问题是我看错了文档。
mybatis-spring还有一个Spring Boot的子项目,mybatis-spring的文档只是针对spring的。
在这里插入图片描述
正确文档看这里:
http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

参考

https://blog.csdn.net/a704397849/article/details/103275184

猜你喜欢

转载自blog.csdn.net/lxyoucan/article/details/126223649
今日推荐