weixin_43562801 2024-08-16 02:06 采纳率: 0%
浏览 67

SpringBoot整合Mybatis-plus8.0.30报错


org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'persistenceExceptionTranslationPostProcessor' defined in class path resource [org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfiguration.class]: Unsatisfied dependency expressed through method 'persistenceExceptionTranslationPostProcessor' parameter 0: Invalid bean definition with name 'driverInfoMapper' defined in file [E:\workspace\Springsecture-01-test\target\classes\com\xgj\application\mapper\DriverInfoMapper.class]: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:795) ~[spring-beans-6.1.11.jar:6.1.11]
    
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'driverInfoMapper' defined in file [E:\workspace\Springsecture-01-test\target\classes\com\xgj\application\mapper\DriverInfoMapper.class]: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:857) ~[spring-beans-6.1.11.jar:6.1.11]
    at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:663) ~[spring-beans-6.1.11.jar:6.1.11]

使用的是mybatis-plus-boot-starter下3.5.3.2的版本;spring-boot-starter-parent使用的是3.3.2版本报的错;启动类上加了 代码

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan("com.xgj.application.mapper")

后面改成下面依赖

  <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
            <version>3.5.5</version>
        </dependency>

继续报错,还是报如下错(依赖树没有冲突)

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'driverInfoServiceImpl': Unsatisfied dependency expressed through field 'baseMapper': Error creating bean with name 'driverInfoMapper' defined in file [E:\workspace\Springsecture-01-test\target\classes\com\xgj\application\mapper\DriverInfoMapper.class]: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:787) ~[spring-beans-6.1.11.jar:6.1.11]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:767) ~[spring-beans-6.1.11.jar:6.1.11]
  • 写回答

5条回答 默认 最新

  • GISer Liu 2024-08-16 02:08
    关注

    该回答引用自GPT-deepseek, 由博主 GISer Liu 编写:

    问题分析

    根据报错信息,问题主要集中在 driverInfoMapper 这个 Bean 的定义上。具体错误是 Invalid value type for attribute 'factoryBeanObjectType': java.lang.String,以及 Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

    解决方案

    1. 检查 DriverInfoMapper 接口

    首先,确保 DriverInfoMapper 接口正确继承了 MyBatis-Plus 的 BaseMapper 接口,并且没有其他错误的注解或配置。

    package com.xgj.application.mapper;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.xgj.application.entity.DriverInfo;
    
    public interface DriverInfoMapper extends BaseMapper<DriverInfo> {
        // 其他自定义方法
    }
    

    2. 检查 application.propertiesapplication.yml 配置文件

    确保数据库连接配置正确,特别是 mybatis-plus 相关的配置。

    # application.properties
    spring.datasource.url=jdbc:mysql://localhost:3306/your_database
    spring.datasource.username=root
    spring.datasource.password=your_password
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
    mybatis-plus.mapper-locations=classpath:mapper/*.xml
    mybatis-plus.type-aliases-package=com.xgj.application.entity
    

    3. 检查依赖

    确保依赖没有冲突,并且使用正确的版本。根据报错信息,建议使用以下依赖:

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.26</version>
    </dependency>
    

    4. 检查启动类注解

    确保启动类上的注解配置正确,特别是 @MapperScan 注解。

    package com.xgj.application;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    
    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
    @MapperScan("com.xgj.application.mapper")
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    5. 检查 DriverInfoServiceImpl

    确保 DriverInfoServiceImpl 类正确注入了 DriverInfoMapper

    package com.xgj.application.service.impl;
    
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.xgj.application.entity.DriverInfo;
    import com.xgj.application.mapper.DriverInfoMapper;
    import com.xgj.application.service.DriverInfoService;
    import org.springframework.stereotype.Service;
    
    @Service
    public class DriverInfoServiceImpl extends ServiceImpl<DriverInfoMapper, DriverInfo> implements DriverInfoService {
        // 其他业务逻辑
    }
    

    总结

    通过以上步骤,逐一检查和修正 DriverInfoMapper 的定义、配置文件、依赖、启动类注解以及 DriverInfoServiceImpl 类的注入,应该能够解决 UnsatisfiedDependencyExceptionBeanDefinitionStoreException 的问题。如果问题依然存在,建议检查日志中的其他错误信息,或者尝试重新构建项目。

    如果该回答解决了您的问题,请采纳!如果没有,请私信联系或评论您的疑惑

    评论

报告相同问题?

问题事件

  • 修改了问题 8月16日
  • 创建了问题 8月16日