之前我在application.properties中的数据源配置是如下的:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/xxx?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
在test包下自己写了一个UserMapper测试刚写的mapper接口是否正确,出现标题所示的错误。
这里提一下,之前我用的SpringBoot是3.2.4版本的,引发了NoSuchBeanException,换成了2.7.3后才解决这个报错,但是又出现了CannotGetJdbcConnectionException。
每个人情况可能不一样,我寻找到的解决办法是在下述配置信息后面
spring.datasource.url=jdbc:mysql://localhost:3306/xxx?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
加入以下信息
&allowPublicKeyRetrieval=true
最终是这样
spring.datasource.url=jdbc:mysql://localhost:3306/xxx?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
运行自己写的UserMapperTest,成功
但是奇怪的是,如果此时你把&allowPublicKeyRetrieval=true去掉后,再运行Test,居然不会报错了。