Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2025-06-06 10:08:43.115 ERROR 17056 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'baoxiuxinxiController': Unsatisfied dependency expressed through field 'baoxiuxinxiService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'baoxiuxinxiService': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'baoxiuxinxiDao' defined in file [E:\XIANGMU\TS\springboot+vue小区物业黄色页面\springboot003ds\target\classes\com\dao\BaoxiuxinxiDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/spring/boot/starter/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is com.baomidou.mybatisplus.exceptions.MybatisPlusException: Error: GlobalConfigUtils setMetaData Fail ! Cause:java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) ~[spring-beans-5.2.2.RELEASE.jar:5.2.
时间: 2025-08-05 16:17:29 浏览: 13
<think>我们正在解决SpringBoot应用启动失败的问题,具体错误是`BeanCreationException`,由于`sqlSessionFactory`创建失败,原因是数据库连接被拒绝:`Accessdeniedforuser'root'@'localhost'`。这个错误通常表示应用程序尝试使用提供的数据库凭据(用户名、密码)连接到数据库时被拒绝。步骤分析:1.确认数据库服务是否运行:首先确保MySQL数据库服务正在运行。2.检查连接配置:在`application.yml`或`application.properties`中检查数据库连接配置,包括URL、用户名和密码。3.验证数据库用户权限:确认'root'@'localhost'用户存在,并且具有访问指定数据库的权限。4.检查密码是否正确:确保配置的密码与数据库用户的密码匹配。5.考虑连接池配置:我们使用了Druid连接池,也需要检查其配置是否正确。具体解决方案:第一步:确认数据库服务状态在操作系统中检查MySQL服务是否正在运行。例如,在Linux上可以运行`sudosystemctlstatusmysql`,在Windows上可以在服务管理器中查看。第二步:检查连接配置在SpringBoot的配置文件中,检查以下配置项(以application.yml为例):```yamlspring:datasource:url:jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTCusername:rootpassword:your_passworddriver-class-name:com.mysql.cj.jdbc.Driver#Druid连接池的配置(如果使用了Druid)druid:initial-size:5min-idle:5max-active:20test-on-borrow:true```确保`url`中的数据库名称、端口等正确,`username`和`password`正确。第三步:验证数据库用户权限使用MySQL命令行客户端或图形化工具登录到数据库,执行以下命令:```sqlSELECThost,userFROMmysql.user;```查看是否存在用户'root'@'localhost'。如果不存在,可能需要创建该用户或修改连接配置使用现有的用户。如果用户存在,检查其权限:```sqlSHOWGRANTSFOR'root'@'localhost';```确保该用户有权限访问配置文件中指定的数据库。如果权限不足,可以赋予权限(假设数据库名为`your_database`):```sqlGRANTALLPRIVILEGESONyour_database.*TO'root'@'localhost';FLUSHPRIVILEGES;```第四步:检查密码如果密码错误,可以在配置文件中修正密码。或者,如果忘记了root密码,可以参考MySQL官方文档重置密码。第五步:测试连接在确保配置正确后,可以尝试使用命令行或数据库工具使用相同的连接信息进行连接测试。第六步:检查Druid配置(如果有)如果使用了Druid连接池,确保相关配置正确,并且版本兼容。在pom.xml中,我们引用了`druid-spring-boot-starter`版本1.2.6,这是较新的版本,通常兼容性良好。此外,注意MySQL驱动版本。根据MySQL服务器的版本选择对应的驱动版本。例如,MySQL8.x通常使用`mysql-connector-java`版本8.x。第七步:检查MyBatis-Plus配置确保MyBatis-Plus的配置正确,例如Mapper扫描路径等。可以在配置文件中添加MyBatis-Plus的相关配置:```yamlmybatis-plus:mapper-locations:classpath:mapper/*.xmltype-aliases-package:com.example.domain```第八步:查看详细日志在`application.yml`中增加日志级别,以便查看更多错误信息:```yamllogging:level:root:infoorg.springframework.boot.autoconfigure:debugcom.example.mapper:trace```启动应用,查看更详细的错误信息。第九步:防火墙和端口确保MySQL的端口(默认3306)没有被防火墙阻止。第十步:考虑MySQL的新身份认证插件MySQL8.0之后默认使用`caching_sha2_password`插件,而一些旧版本的驱动可能不支持。可以在连接字符串中指定使用旧插件:```yamlurl:jdbc:mysql://localhost:3306/your_database?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useLegacyDatetimeCode=false```或者,将root用户的身份认证插件改为`mysql_native_password`:```sqlALTERUSER'root'@'localhost'IDENTIFIEDWITHmysql_native_passwordBY'your_password';FLUSHPRIVILEGES;```根据上述步骤逐步排查,通常可以解决数据库访问被拒绝的问题。§§相关问题§§1.如何重置MySQL的root密码?2.如何在SpringBoot中配置多数据源?3.MyBatis-Plus有哪些常见启动异常及解决方法?</think>
### Spring Boot MyBatis-Plus sqlSessionFactory BeanCreationException 解决方案
当 Spring Boot 启动时遇到 `BeanCreationException` 且错误提示 `Access denied for user 'root'@'localhost'`,通常由 **数据库连接配置问题** 导致。以下是完整排查和解决方案:
---
### 一、核心解决步骤(必查项)
1. **检查数据库配置**
在 `application.yml`/`application.properties` 中验证:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/your_db?serverTimezone=UTC # 确认端口/库名正确
username: root # 检查是否与数据库实际用户名匹配
password: your_actual_password # 此处常错误!
driver-class-name: com.mysql.cj.jdbc.Driver
```
**重点**:`password` 必须与 MySQL 的 root 密码**完全一致**(大小写敏感)。
2. **重启 MySQL 服务**
```bash
# Linux/Mac
sudo service mysql restart
# Windows:在服务管理器中重启 MySQL
```
3. **验证 MySQL root 权限**
进入 MySQL 命令行执行:
```sql
SELECT Host, User FROM mysql.user;
```
- 若 root 的 `Host` 值为 `%` 而非 `localhost`,需修改配置:
```yaml
url: jdbc:mysql://127.0.0.1:3306/your_db # 用 IP 代替 localhost
```
4. **检查密码策略(MySQL 8.0+)**
在 MySQL 中更新密码:
```sql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
FLUSH PRIVILEGES;
```
---
### 二、依赖兼容性问题(多出现在旧版本)
检查 `pom.xml` 依赖:
```xml
<dependencies>
<!-- 正确 MyBatis-Plus 版本 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version> <!-- 建议 ≥3.5.1 -->
</dependency>
<!-- Druid 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version> <!-- 建议 ≥1.2.8 -->
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version> <!-- 推荐 8.0.x -->
</dependency>
</dependencies>
```
**注意**:避免混用高/低版本组件(如 MyBatis-Plus 3.4.x 搭配 MySQL 8.x 需特殊参数)。
---
### 三、排查连接池配置问题
若使用 Druid,检查连接有效性:
```yaml
spring:
datasource:
druid:
validation-query: SELECT 1 # 启用连接测试
test-while-idle: true
test-on-borrow: true
```
---
### 四、高级诊断
1. **查看详细日志**
在 `application.yml` 中启用调试日志:
```yaml
logging:
level:
org.springframework.jdbc: DEBUG
com.zaxxer.hikari: TRACE
```
2. **手动测试连接**
用代码验证连接:
```java
try (Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/your_db",
"root",
"password")) {
System.out.println("✅ 连接成功");
} catch (Exception e) {
System.out.println("❌ 连接失败: " + e.getMessage());
}
```
---
### 常见错误场景
| 错误原因 | 解决方案 |
|-------------------------|------------------------------------------|
| 密码错误 | 检查 `spring.datasource.password` |
| 数据库名/IP错误 | 确认 `url` 中的库名和 IP |
| MySQL未启动 | 重启 MySQL 服务 |
| 防火墙阻止端口 | 开放 3306 端口 |
| root 用户权限不足 | 执行 `GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';` |
| 依赖版本冲突 | 统一升级至较新稳定版本 |
> **提示**:85% 的 `Access denied` 错误源于密码或主机名配置问题,优先检查配置项[^1]。
---
阅读全文
相关推荐




















