About the small pit when Springboot configures the database data source

Today, when I was working on a small project of Springboot, I encountered a bug that was not easy to find. Let me avoid pitfalls. When the basic environment of the project was set up and ready to run and test, a headache "little bug" appeared:

"Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured." It is roughly that there is a problem with the "url" configuration in the data source. I just started to configure the data source as follows:

spring:
  datasource:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
      username: root
      password: root

I think it may be that there is a wrong configuration in my "url", but I checked carefully and found no mistakes, and then I changed "url" to

url: jdbc:mysql://localhost:3306/reggie

But the same error will still be reported. I searched online. Some people said that it was caused by importing unnecessary dependencies, some said that it was because the configuration file was written wrong, and some said that it was because the configuration was not scanned, etc., but these reasons I checked one by one, and still reported an error, which is a headache.

Then I took a closer look at the configuration file examples of the Springboot data source and found that some examples were configured using the Druid data source

type: com.alibaba.druid.pool.DruidDataSource

Instead of directly adding "druid" under "datasource", I tried it and found that it works normally after using "type".

For the way I wrote the configuration at the beginning, I always remember that it was written like this. There are also examples of this way of writing on the Internet, and IDEA also prompts like this, so I always feel that my configuration is fine, and it has been delayed for a long time. As for why this way of writing will report an error , personal guess may be due to Springboot or database version update.

It is also to provide you with a way of thinking when you encounter "Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured."

Guess you like

Origin blog.csdn.net/m0_56680022/article/details/128955681