摘要:平常我们使用SpringBoot都是要连接数据库的,所以你在按照正常的情况下配置你的SpringBoot项目后,如果你不想使用数据库或者
没有进行数据库配置,系统就会报错。导致和我们预期不一致不能启动项目。

如果按照平常的配置不配置数据库或者你就没有数据库会报以下错误(以下错误是我配置了错误的数据库账号和密码导致的)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Connected to the target VM, address: '127.0.0.1:53221', transport: 'socket'

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)

2020-03-12 11:32:15.710 INFO 8672 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on DESKTOP-J90MAVN with PID 8672 (E:\workingspace\demo-space\demo\target\classes started by lyc in E:\workingspace\demo-space\demo)
2020-03-12 11:32:15.716 INFO 8672 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2020-03-12 11:32:16.388 INFO 8672 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2020-03-12 11:32:16.444 INFO 8672 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 50ms. Found 3 repository interfaces.
2020-03-12 11:32:16.641 INFO 8672 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$2208748f] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-03-12 11:32:16.809 INFO 8672 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8085 (http)
2020-03-12 11:32:16.823 INFO 8672 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-03-12 11:32:16.823 INFO 8672 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.22]
2020-03-12 11:32:16.919 INFO 8672 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-03-12 11:32:16.919 INFO 8672 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1120 ms
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2020-03-12 11:32:17.065 INFO 8672 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
2020-03-12 11:32:17.558 ERROR 8672 --- [eate-1119371910] com.alibaba.druid.pool.DruidDataSource : create connection SQLException, url: jdbc:mysql://49.234.45.101:3306/work?characterEncoding=utf8, errorCode 1045, state 28000

java.sql.SQLException: Access denied for user 'root'@'115.60.149.255' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:827) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:447) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:237) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1558) ~[druid-1.1.10.jar:1.1.10]
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1623) ~[druid-1.1.10.jar:1.1.10]
at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2468) ~[druid-1.1.10.jar:1.1.10]

2020-03-12 11:32:17.752 ERROR 8672 --- [eate-1119371910] com.alibaba.druid.pool.DruidDataSource : create connection SQLException, url: jdbc:mysql://49.234.45.101:3306/work?characterEncoding=utf8, errorCode 1045, state 28000

最简单的解决办法就是找到你Application启动类添加如下注解内容

1
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

完整版

1
2
3
4
5
6
7
8
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

思路解释:其实就是让系统忽略DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class