【SpringBoot3集成Mybatis-plus】启动报错问题解决

记录日常开发中,springboot 3 集成mubatisPlus,项目编译无报错,启动报错:Bean named ‘ddlApplicationRunner’ is expected to be of type ‘org.springframework.boot.Runner’ but was actually of type ‘org.springframework.beans.factory.support.NullBean’ 有效解决方案。

报错内容:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'ddlApplicationRunner' is expected to be of type 'org.springframework.boot.Runner' but was actually of type 'org.springframework.beans.factory.support.NullBean'
	at org.springframework.beans.factory.support.AbstractBeanFactory.adaptBeanInstance(AbstractBeanFactory.java:410) ~[spring-beans-6.1.2.jar:6.1.2]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:391) ~[spring-beans-6.1.2.jar:6.1.2]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204) ~[spring-beans-6.1.2.jar:6.1.2]
	at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:778) ~[spring-boot-3.2.1.jar:3.2.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:341) ~[spring-boot-3.2.1.jar:3.2.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1358) ~[spring-boot-3.2.1.jar:3.2.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1347) ~[spring-boot-3.2.1.jar:3.2.1]
	at com.hqwy.service.SpringbootApplication.main(SpringbootApplication.java:13) ~[classes/:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.2.1.jar:3.2.1]

亲测有效解决方案:

  • 修改Mybatis-Plus的依赖坐标
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
	<version>3.5.5</version>
	<exclusions>
		<exclusion>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<!-- mybatis-plus 所需依赖  -->
<dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis-spring</artifactId>
</dependency>

亲测第二种有效解决方案:

本着缺什么就补什么的原则,将缺少的Bean对象注入到容器中。

import com.baomidou.mybatisplus.autoconfigure.DdlApplicationRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Edwin
 * @version 1.0.0
 */
@Component
public class GlobalConfig {

    @Bean
    public DdlApplicationRunner ddlApplicationRunner(@Autowired(required = false) List ddlLrist) {
        return new DdlApplicationRunner(ddlLrist);
    }

}

end 到此!问题完美解决,这个问题困扰了我好几天… 苦苦寻觅到的解决方式。

  • 13
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
当在Spring Boot 3中尝试整合MyBatis-Plus时,遇到`Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required`这个错误,通常是由于你没有正确配置SpringMyBatis-Plus之间的依赖关系。这是必要的,因为MyBatis-Plus依赖于MyBatis的`SqlSessionFactory`或`SqlSessionTemplate`来执行SQL操作。 以下是解决这个问题的步骤: 1. **配置MyBatis**: 在Spring Boot应用的配置文件(application.yml 或 application.properties)中添加MyBatisMyBatis-Plus相关的配置。例如: ```yaml mybatis: type-aliases-package: com.example.demo.entity # 如果你的实体类包名在此 mapper-locations: classpath:mapper/*.xml # XML映射文件的位置 config-location: classpath:mybatis-config.xml # 配置文件位置(可选) mybatis-plus: global-config: # 全局配置类 db-config: # 数据库配置 id-type: ID_WORKER # 自增主键策略 table-prefix: "tb_" # 表前缀 ``` 2. **添加依赖**: 确保在你的`pom.xml`或`build.gradle`文件中包含了Spring BootSpring Data JPA(如果使用)、MyBatis以及MyBatis-Plus的依赖。例如: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> ``` 3. **注入SqlSessionFactory或SqlSessionTemplate**: 在你的Repository或Service类上,需要通过`@Autowired`注解来注入`SqlSessionFactory`或`SqlSessionTemplate`。例如: ```java @Autowired private SqlSessionFactory sqlSessionFactory; ``` 4. **检查是否扫描Mapper接口**: 如果你使用的是Java配置(即不使用XML),记得在`@Configuration`类中启用Mapper扫描: ```java @MapperScan("com.example.demo.mapper") // 替换为你的Mapper接口所在的包名 ``` 按照以上步骤检查并配置,应该能解决`Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required`的错误。如果你在配置过程中还是遇到问题,可以提供具体的错误堆栈以便更精确地诊断。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值