【springboot】springboot集成mysql

发布时间 2023-07-02 18:59:46作者: LastBattle

pom.xml增加依赖的坐标

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

<!--        <dependency>-->
<!--            <groupId>com.mysql</groupId>-->
<!--            <artifactId>mysql-connector-j</artifactId>-->
<!--            <scope>runtime</scope>-->
<!--        </dependency>-->

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.26</version>
    <scope>runtime</scope>
</dependency>

报错如下:

18:42:43.414 [Thread-1] DEBUG org.springframework.boot.devtools.restart.classloader.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@5d1c613f

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::      (v2.7.14-SNAPSHOT)

2023-07-02 18:42:43.653  INFO 73321 --- [  restartedMain] com.oo2.oo2.Oo2Application               : Starting Oo2Application using Java 1.8.0_361 on JAMESSHI-MB2 with PID 73321 (/Users/jamesshi/java/oo2/target/classes started by jamesshi in /Users/jamesshi/java/oo2)
2023-07-02 18:42:43.654  INFO 73321 --- [  restartedMain] com.oo2.oo2.Oo2Application               : No active profile set, falling back to 1 default profile: "default"
2023-07-02 18:42:43.682  INFO 73321 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-07-02 18:42:43.682  INFO 73321 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-07-02 18:42:44.102  WARN 73321 --- [  restartedMain] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.oo2.oo2]' package. Please check your configuration.
2023-07-02 18:42:44.459  INFO 73321 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8885 (http)
2023-07-02 18:42:44.464  INFO 73321 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-07-02 18:42:44.465  INFO 73321 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.76]
2023-07-02 18:42:44.508  INFO 73321 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-07-02 18:42:44.509  INFO 73321 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 827 ms
2023-07-02 18:42:44.751  WARN 73321 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2023-07-02 18:42:44.753  INFO 73321 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-07-02 18:42:44.769  INFO 73321 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-07-02 18:42:44.791 ERROR 73321 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


Process finished with exit code 0

这是因为配置文件application.properties里面没有配置mysql对应的信息

spring.datasource.url=jdbc:mysql://localhost:3306?characterEncoding=UTF8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=12345678
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

这次可以启动成功了

18:49:25.626 [Thread-1] DEBUG org.springframework.boot.devtools.restart.classloader.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@5d1c613f

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::      (v2.7.14-SNAPSHOT)

2023-07-02 18:49:25.916  INFO 75190 --- [  restartedMain] com.oo2.oo2.Oo2Application               : Starting Oo2Application using Java 1.8.0_361 on JAMESSHI-MB2 with PID 75190 (/Users/jamesshi/java/oo2/target/classes started by jamesshi in /Users/jamesshi/java/oo2)
2023-07-02 18:49:25.917  INFO 75190 --- [  restartedMain] com.oo2.oo2.Oo2Application               : No active profile set, falling back to 1 default profile: "default"
2023-07-02 18:49:25.957  INFO 75190 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-07-02 18:49:25.957  INFO 75190 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-07-02 18:49:26.541  WARN 75190 --- [  restartedMain] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.oo2.oo2]' package. Please check your configuration.
2023-07-02 18:49:26.990  INFO 75190 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8885 (http)
2023-07-02 18:49:26.998  INFO 75190 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-07-02 18:49:26.999  INFO 75190 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.76]
2023-07-02 18:49:27.072  INFO 75190 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-07-02 18:49:27.073  INFO 75190 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1116 ms
2023-07-02 18:49:27.540  INFO 75190 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2023-07-02 18:49:27.582  INFO 75190 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8885 (http) with context path ''
2023-07-02 18:49:27.591  INFO 75190 --- [  restartedMain] com.oo2.oo2.Oo2Application               : Started Oo2Application in 1.959 seconds (JVM running for 2.464)