Spring Boot +MyBatis-Plus+ MyBatisX自动生成代码报错Error creating bean with name ‘userServiceImpl‘

文章描述了在SpringBoot项目中使用MyBatisPlus时遇到的依赖注入问题,发现是由于不同版本的Mybatis-PlusStarter之间的差异引发的。通过将版本从3.5.5降级到3.5.1解决了问题。
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [E:\javaCode\userCentor\target\classes\com\example\usercentor\mapper\UserMapper.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/autoconfigure/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 java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: org.apache.ibatis.session.Configuration.setArgNameBasedConstructorAutoMapping(Z)V
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-5.3.23.jar:5.3.23]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) ~[spring-beans-5.3.23.jar:5.3.23]
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.23.jar:5.3.23]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [E:\javaCode\userCentor\target\classes\com\example\usercentor\mapper\UserMapper.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/autoconfigure/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 java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: org.apache.ibatis.session.Configuration.setArgNameBasedConstructorAutoMapping(Z)V
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1534) ~[spring-beans-5.3.23.jar:5.3.23]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1417) ~[spring-beans-5.3.23.jar:5.3.23]

一直提示bean对象无法注入,看了好个帖子都是说@Mapper注解的问题,检查代码从serviceImp到@Mapper以及通过My Batis X生产的mapper.xml都没有问题。

最后重新看了pom.xml,发现

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

改为

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.1</version>
</dependency>

后解决问题

Spring Boot 项目中使用 MyBatis Plus 和 Dynamic-Datasource 时,启动失败并报错 `Error creating bean with name 'sysUserServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'`,通常涉及依赖注入失败或 Mapper 初始化配置不当的问题。以下是可能的原因及解决方案: ### 1. 注解缺失 确保所有相关的类都正确添加了必要的注解。例如: - **Service 层**:必须使用 `@Service` 注解标记为 Spring Bean- **Mapper 接口**:需要添加 `@Mapper` 注解,或者在主启动类上使用 `@MapperScan` 来扫描所有 Mapper 接口[^4]。 - **Controller 层**:使用 `@RestController` 或 `@Controller` 注解,并通过 `@Autowired` 注入 Service。 ```java @Service public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService { // ... } ``` ```java @Mapper public interface SysUserMapper extends BaseMapper<SysUser> { // ... } ``` ### 2. 多数据源配置问题 当使用 Dynamic Datasource 时,需要确认是否正确配置了多个数据源,并且没有遗漏 SQL Session Factory 的绑定。 - 检查 `application.yml` 或 `application.properties` 中的数据源配置是否完整,尤其是主从数据源的连接信息、驱动类、用户名和密码等。 - 确保 Dynamic Datasource starter 已引入,并且版本兼容当前 Spring Boot 版本。 - 如果使用了自定义数据源配置,需确认是否显式地指定了 `sqlSessionFactory`。 ```yaml spring: datasource: dynamic: primary: master datasource: master: url: jdbc:mysql://localhost:3306/master_db username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver slave1: url: jdbc:mysql://localhost:3306/slave_db username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver ``` ### 3. MyBatis Plus 配置与版本兼容性 MyBatis Plus 的版本与 Spring Boot 的版本之间可能存在兼容性问题。如果常规手段如添加 `@Mapper` 或 `@Service` 不成功,则可能是依赖冲突或版本不兼容问题[^3]。 - 检查 `pom.xml` 或 `build.gradle` 文件中是否包含正确的 MyBatis Plus Starter 和 Dynamic Datasource Starter。 - 建议使用与 Spring Boot 兼容的 MyBatis Plus 版本(如 `3.5.5`)以及 Dynamic Datasource Starter(如 `3.5.1`)。 ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.5.1</version> </dependency> ``` ### 4. Mapper 自动注入失败 如果 `SysUserServiceImpl` 继承了 `ServiceImpl` 并依赖于 `baseMapper`,但未能正确注入 `SysUserMapper`,则会抛出 `UnsatisfiedDependencyException`。确保: - `SysUserMapper` 接口已正确实现 `BaseMapper`。 - 在 Service 实现类中不要手动注入 Mapper,除非你需要扩展方法,否则直接继承即可。 - 如果确实需要手动注入,请使用 `@Autowired` 显式注入 Mapper。 ```java @Autowired private SysUserMapper sysUserMapper; ``` ### 5. 启动类配置问题 检查主启动类是否启用了自动扫描,并正确配置了组件扫描路径。 ```java @SpringBootApplication @MapperScan("com.example.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值