彻底解决Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource

时间:2024-12-20 07:12:28

错误消息

springboot2启动项目报错,应该是数据库连接的问题,导致无法启动。

错误消息如下:

  1. ***************************
  2. APPLICATION FAILED TO START
  3. ***************************
  4. Description:
  5. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
  6. Reason: Failed to determine a suitable driver class
  7. Action:
  8. Consider the following:
  9. If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
  10. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

解决方法

不需要连接数据库

********************************** 项目不需要连接数据库,启动报错 ******************************************

解决方法如下:

只要在将@SpringBootApplication修改为@SpringBootApplication(exclude= {})就可以启动的时候不需要连接数据库。

  1. // 这里需要注意Springboot版本
  2. // 下面的配置只对起作用
  3. // 如果是,需要把中引用的jdbc驱动注释掉
  4. // 如果是,需要把中引用的mybatis或者mybatis-plus注释掉
  5. @SpringBootApplication(exclude= {})

需要连接数据库

********************************** 需要连接数据库,启动报错 ******************************************

解决方法如下:

第一种

yml配置示例如下

  1. #在/或者文件中没有添加数据库配置信息.
  2. spring:
  3. datasource:
  4. url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
  5. username: root
  6. password: 123456
  7. driver-class-name:

第二种

properties文件示例如下

 = jdbc:mysql://localhost:3306/test?setUnicode=true&characterEncoding=utf8

第三种

mysql的版本不同,示例如下

  1. #mysql8以下的版本,请检查文件种依赖的mysql jar包的版本
  2. driver-class-name:
  3. #mysql8以下的url写法
  4. url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
  5. #mysql8的版本写法,多了个cj
  6. driver-class-name:
  7. #同时mysql8的url也需要加入时区,参照如下
  8. url: jdbc:mysql://192.168.1.110:3306/test?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true

第四种

项目没有加载到yml或者properties文件,特别是自己的pom打包是jar的项目,请查看自己的文件中的packaging

<packaging>jar</packaging>

如果pom中指定使用jar,系统不会自动读取到yml或者properties文件的,需要我们手动配置。

  1. <!--build放在</dependencies>标签的后面,主要加入的是resources标签 -->
  2. <!--resources标签可以告诉系统启动的时候能够读取到这些后缀的文件 -->
  3. <build>
  4. <plugins>
  5. <plugin>
  6. <groupId></groupId>
  7. <artifactId>spring-boot-maven-plugin</artifactId>
  8. </plugin>
  9. </plugins>
  10. <resources>
  11. <resource>
  12. <directory>src/main/java</directory>
  13. <includes>
  14. <include>**/*.yml</include>
  15. <include>**/*.properties</include>
  16. <include>**/*.xml</include>
  17. </includes>
  18. <filtering>false</filtering>
  19. </resource>
  20. <resource>
  21. <directory>src/main/resources</directory>
  22. <includes>
  23. <include>**/*.yml</include>
  24. <include>**/*.properties</include>
  25. <include>**/*.xml</include>
  26. </includes>
  27. <filtering>false</filtering>
  28. </resource>
  29. <resource>
  30. <directory>lib</directory>
  31. <includes>
  32. <include>**/*.jar</include>
  33. </includes>
  34. </resource>
  35. </resources>
  36. </build>

第五种

项目使用了springcloud+nacos系列,启动项目时候需要手动指定【--=test】,那么在resources文件夹下就必须要有或者文件,同时配置文件中连接的nacos地址里面也必须配置对应的命名空间,和对应服务名称的yml文件,否则也是报错。下面是配置文件的截图

 

 

 第六种

如果你的所有配置信息都是好的,也就是说项目之前是好的,现在突然不行了。可以尝试清除缓存。点击File-Invalidata Caches...接下来工具会重启,重新启动后maven-clean以下试试。

第七种

注意如果使用了nacos这种配置的,一定要注意自己的yml或者properties文件中的spring引入的位置是在nacos的配置地址上面还是下面,下面的文件会把上面的配置覆盖掉,导致失效。

第八种

yml或者properties文件放错位置或者maven编译的时候没有把文件同步到target下面。

结束

-----华丽的分割线,以下是凑字数,大家不用花时间看,快去改代码-----

-----华丽的分割线,以下是凑字数,大家不用花时间看,快去改代码-----

-----华丽的分割线,以下是凑字数,大家不用花时间看,快去改代码-----

  1. package cn.;
  2. import .slf4j.Slf4j;
  3. @Slf4j
  4. public class MakeUpTheWordCount {
  5. private String make_up_the_word_count_column_999999999_1;
  6. private String make_up_the_word_count_column_999999999_2;
  7. private String make_up_the_word_count_column_999999999_3;
  8. private String make_up_the_word_count_column_999999999_4;
  9. private String make_up_the_word_count_column_999999999_5;
  10. private String make_up_the_word_count_column_999999999_6;
  11. private String make_up_the_word_count_column_999999999_7;
  12. private String make_up_the_word_count_column_999999999_8;
  13. private String make_up_the_word_count_column_999999999_9;
  14. private String make_up_the_word_count_column_999999999_10;
  15. private String make_up_the_word_count_column_999999999_11;
  16. private String make_up_the_word_count_column_999999999_12;
  17. private String make_up_the_word_count_column_999999999_13;
  18. private String make_up_the_word_count_column_999999999_14;
  19. private String make_up_the_word_count_column_999999999_15;
  20. private String make_up_the_word_count_column_999999999_16;
  21. private String make_up_the_word_count_column_999999999_17;
  22. private String make_up_the_word_count_column_999999999_18;
  23. private String make_up_the_word_count_column_999999999_19;
  24. private String make_up_the_word_count_column_999999999_20;
  25. public String getMake_up_the_word_count_column_999999999_1() {
  26. return make_up_the_word_count_column_999999999_1;
  27. }
  28. public void setMake_up_the_word_count_column_999999999_1(String make_up_the_word_count_column_999999999_1) {
  29. this.make_up_the_word_count_column_999999999_1 = make_up_the_word_count_column_999999999_1;
  30. }
  31. public String getMake_up_the_word_count_column_999999999_2() {
  32. return make_up_the_word_count_column_999999999_2;
  33. }
  34. public void setMake_up_the_word_count_column_999999999_2(String make_up_the_word_count_column_999999999_2) {
  35. this.make_up_the_word_count_column_999999999_2 = make_up_the_word_count_column_999999999_2;
  36. }
  37. public String getMake_up_the_word_count_column_999999999_3() {
  38. return make_up_the_word_count_column_999999999_3;
  39. }
  40. public void setMake_up_the_word_count_column_999999999_3(String make_up_the_word_count_column_999999999_3) {
  41. this.make_up_the_word_count_column_999999999_3 = make_up_the_word_count_column_999999999_3;
  42. }
  43. public String getMake_up_the_word_count_column_999999999_4() {
  44. return make_up_the_word_count_column_999999999_4;
  45. }
  46. public void setMake_up_the_word_count_column_999999999_4(String make_up_the_word_count_column_999999999_4) {
  47. this.make_up_the_word_count_column_999999999_4 = make_up_the_word_count_column_999999999_4;
  48. }
  49. public String getMake_up_the_word_count_column_999999999_5() {
  50. return make_up_the_word_count_column_999999999_5;
  51. }
  52. public void setMake_up_the_word_count_column_999999999_5(String make_up_the_word_count_column_999999999_5) {
  53. this.make_up_the_word_count_column_999999999_5 = make_up_the_word_count_column_999999999_5;
  54. }
  55. public String getMake_up_the_word_count_column_999999999_6() {
  56. return make_up_the_word_count_column_999999999_6;
  57. }
  58. public void setMake_up_the_word_count_column_999999999_6(String make_up_the_word_count_column_999999999_6) {
  59. this.make_up_the_word_count_column_999999999_6 = make_up_the_word_count_column_999999999_6;
  60. }
  61. public String getMake_up_the_word_count_column_999999999_7() {
  62. return make_up_the_word_count_column_999999999_7;
  63. }
  64. public void setMake_up_the_word_count_column_999999999_7(String make_up_the_word_count_column_999999999_7) {
  65. this.make_up_the_word_count_column_999999999_7 = make_up_the_word_count_column_999999999_7;
  66. }
  67. public String getMake_up_the_word_count_column_999999999_8() {
  68. return make_up_the_word_count_column_999999999_8;
  69. }
  70. public void setMake_up_the_word_count_column_999999999_8(String make_up_the_word_count_column_999999999_8) {
  71. this.make_up_the_word_count_column_999999999_8 = make_up_the_word_count_column_999999999_8;
  72. }
  73. public String getMake_up_the_word_count_column_999999999_9() {
  74. return make_up_the_word_count_column_999999999_9;
  75. }
  76. public void setMake_up_the_word_count_column_999999999_9(String make_up_the_word_count_column_999999999_9) {
  77. this.make_up_the_word_count_column_999999999_9 = make_up_the_word_count_column_999999999_9;
  78. }
  79. public String getMake_up_the_word_count_column_999999999_10() {
  80. return make_up_the_word_count_column_999999999_10;
  81. }
  82. public void setMake_up_the_word_count_column_999999999_10(String make_up_the_word_count_column_999999999_10) {
  83. this.make_up_the_word_count_column_999999999_10 = make_up_the_word_count_column_999999999_10;
  84. }
  85. public String getMake_up_the_word_count_column_999999999_11() {
  86. return make_up_the_word_count_column_999999999_11;
  87. }
  88. public void setMake_up_the_word_count_column_999999999_11(String make_up_the_word_count_column_999999999_11) {
  89. this.make_up_the_word_count_column_999999999_11 = make_up_the_word_count_column_999999999_11;
  90. }
  91. public String getMake_up_the_word_count_column_999999999_12() {
  92. return make_up_the_word_count_column_999999999_12;
  93. }
  94. public void setMake_up_the_word_count_column_999999999_12(String make_up_the_word_count_column_999999999_12) {
  95. this.make_up_the_word_count_column_999999999_12 = make_up_the_word_count_column_999999999_12;
  96. }
  97. public String getMake_up_the_word_count_column_999999999_13() {
  98. return make_up_the_word_count_column_999999999_13;
  99. }
  100. public void setMake_up_the_word_count_column_999999999_13(String make_up_the_word_count_column_999999999_13) {
  101. this.make_up_the_word_count_column_999999999_13 = make_up_the_word_count_column_999999999_13;
  102. }
  103. public String getMake_up_the_word_count_column_999999999_14() {
  104. return make_up_the_word_count_column_999999999_14;
  105. }
  106. public void setMake_up_the_word_count_column_999999999_14(String make_up_the_word_count_column_999999999_14) {
  107. this.make_up_the_word_count_column_999999999_14 = make_up_the_word_count_column_999999999_14;
  108. }
  109. public String getMake_up_the_word_count_column_999999999_15() {
  110. return make_up_the_word_count_column_999999999_15;
  111. }
  112. public void setMake_up_the_word_count_column_999999999_15(String make_up_the_word_count_column_999999999_15) {
  113. this.make_up_the_word_count_column_999999999_15 = make_up_the_word_count_column_999999999_15;
  114. }
  115. public String getMake_up_the_word_count_column_999999999_16() {
  116. return make_up_the_word_count_column_999999999_16;
  117. }
  118. public void setMake_up_the_word_count_column_999999999_16(String make_up_the_word_count_column_999999999_16) {
  119. this.make_up_the_word_count_column_999999999_16 = make_up_the_word_count_column_999999999_16;
  120. }
  121. public String getMake_up_the_word_count_column_999999999_17() {
  122. return make_up_the_word_count_column_999999999_17;
  123. }
  124. public void setMake_up_the_word_count_column_999999999_17(String make_up_the_word_count_column_999999999_17) {
  125. this.make_up_the_word_count_column_999999999_17 = make_up_the_word_count_column_999999999_17;
  126. }
  127. public String getMake_up_the_word_count_column_999999999_18() {
  128. return make_up_the_word_count_column_999999999_18;
  129. }
  130. public void setMake_up_the_word_count_column_999999999_18(String make_up_the_word_count_column_999999999_18) {
  131. this.make_up_the_word_count_column_999999999_18 = make_up_the_word_count_column_999999999_18;
  132. }
  133. public String getMake_up_the_word_count_column_999999999_19() {
  134. return make_up_the_word_count_column_999999999_19;
  135. }
  136. public void setMake_up_the_word_count_column_999999999_19(String make_up_the_word_count_column_999999999_19) {
  137. this.make_up_the_word_count_column_999999999_19 = make_up_the_word_count_column_999999999_19;
  138. }
  139. public String getMake_up_the_word_count_column_999999999_20() {
  140. return make_up_the_word_count_column_999999999_20;
  141. }
  142. public void setMake_up_the_word_count_column_999999999_20(String make_up_the_word_count_column_999999999_20) {
  143. this.make_up_the_word_count_column_999999999_20 = make_up_the_word_count_column_999999999_20;
  144. }
  145. }