Fairydored / SEP-cw2

0 stars 0 forks source link

用mybatis连接MySQL数据库时遇到了问题 #9

Open 000NK000 opened 5 months ago

000NK000 commented 5 months ago

大家好,我在开发一个 Spring Boot 项目,使用 MyBatis 连接 MySQL 数据库时遇到了问题,希望得到各位的帮助。

项目使用 Maven 构建,pom.xml 中引入了以下依赖:

org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.4 mysql mysql-connector-java runtime

然后在 application.yml 中配置了数据源:

spring: datasource: url: jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver

mybatis: mapper-locations: classpath:mapper/Mapper.xml type-aliases-package: com.example.demo.entity

但是当我启动项目时,出现了如下异常:


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).

我检查了数据库连接 URL、用户名、密码,都没有问题。数据库 mydb 也确实存在。但就是连接不上。

我看了一些博客,有的说要在 MySQL 命令行客户端中执行 create database mydb; 命令,我试了一下,还是不行。

请问各位有遇到过类似问题吗?我应该如何修改配置才能让 Spring Boot 项目成功连接到 MySQL 数据库?