org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dictionaryService': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dictionaryDao' defined in file [E:\jianshen\test\jianshenfangguanli\target\classes\com\dao\DictionaryDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/spring/boot/starter/MybatisPlusAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconf
时间: 2025-05-24 13:18:23 浏览: 108
### 解决方案
`org.springframework.beans.factory.UnsatisfiedDependencyException` 是由于 Spring 容器无法完成依赖注入而导致的异常。在使用 MyBatis-Plus 的情况下,通常是因为 `sqlSessionFactory` 或者 `dataSource` 配置不正确引起的。
以下是可能的原因以及解决方案:
#### 1. 数据源未正确定义
如果项目的 `application.yml` 或 `application.properties` 文件中没有正确配置数据源,则可能导致容器初始化失败。需要确保以下属性已定义并正确设置[^2]:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/your_database_name?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: your_username
password: your_password
driver-class-name: com.mysql.cj.jdbc.Driver
```
如果没有这些配置项或者路径有误,Spring 将抛出类似的异常。
#### 2. Mapper XML 文件缺失或位置错误
MyBatis 要求所有的 Mapper 接口都对应一个 XML 映射文件。如果 XML 文件不存在于指定的位置(如 `classpath:/mapper/xml/`),则会引发资源解析失败的问题[^1]。可以通过调整 `mybatis.mapperLocations` 属性来修正映射文件路径:
```properties
mybatis.mapperLocations=classpath*:/**/mapper/**/*.xml
```
此配置允许扫描更广泛的目录结构以找到所需的 XML 文件。
#### 3. 缺少必要的依赖库
某些场景下,项目可能遗漏了数据库驱动程序或其他必需组件的 Maven 依赖声明。例如 MySQL 连接器应被显式引入到 `pom.xml` 中[^3]:
```xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
```
另外,PageHelper 插件也可能影响 SQL 执行逻辑;因此也需要确认其版本兼容性[^4]:
```xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>最新稳定版号</version>
</dependency>
```
#### 4. 自动装配冲突
当多个模块共享相同的上下文环境时,可能会因为重复加载 Bean 导致竞争条件发生。此时可以尝试通过排除特定自动配置类的方式解决问题:
```java
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
}
```
不过这种方法仅适用于特殊需求下的调试阶段,在生产环境中需谨慎操作。
#### 示例代码片段
下面给出一段简单的 Java 配置示例用于演示如何手动注册 SqlSessionFactoryBean 实例:
```java
@Configuration
@MapperScan(basePackages = {"*.*.mapper"})
public class MyBatisConfig {
@Bean
public DataSource dataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
ds.setUsername("root");
ds.setPassword("password");
return ds;
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factoryBean.setMapperLocations(resolver.getResources("classpath*:/**/mapper/**/*.xml"));
return factoryBean.getObject();
}
}
```
以上方法能够有效缓解大多数由 `UnsatisfiedDependencyException` 引发的情况。
---
阅读全文
相关推荐





