MyBatis-Flex配置Druid(德鲁伊数据库连接池):Spring Boot 3 集成 MyBatis-Flex 配置 Druid 连接池指南

Spring Boot 3 集成 MyBatis-Flex 配置 Druid 连接池指南

前言

本文详细讲解在 Spring Boot 3 项目中集成 MyBatis-Flex 框架后,如何正确配置 Druid 数据库连接池。针对开发者常见的配置缺失导致启动失败的场景,提供完整的解决方案和原理分析。

前置知识:
MyBatis-Flex 基础整合步骤请参考《MyBatis-Flex 快速开始(Spring Boot 3 整合 MyBatis-Flex)》


一、核心配置步骤

1. 添加依赖

pom.xml 中新增 Druid 官方 Starter 依赖:

<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.24</version>
</dependency>

2. 配置数据源类型(关键!)

application.yml必须显式声明数据源类型

spring:datasource:type: com.alibaba.druid.pool.DruidDataSource

二、完整配置示例

1. 完整 POM 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.5</version><relativePath/></parent><groupId>com.example</groupId><artifactId>hello-mybatis-flex</artifactId><version>0.0.1-SNAPSHOT</version><name>hello-mybatis-flex</name><description>Spring Boot 集成 MyBatis-Flex</description><properties><java.version>21</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.mybatis-flex</groupId><artifactId>mybatis-flex-spring-boot3-starter</artifactId><version>1.10.9</version></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.24</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

2. 完整 YAML 配置

spring:application:name: hello-mybatis-flexdatasource:url: jdbc:mysql://localhost:3306/flex_testusername: flex_test_userpassword: 12345678# 必须显式指定 Druid 类型type: com.alibaba.druid.pool.DruidDataSource

三、官网Druid 数据源无法启动解析

使用 Druid 数据源后,SpringBoot项目无法启动,原因是在数据源的配置中,未添加 type 字段的配置。

这个并非是 MyBaits-Flex 的问题,而是 Spring 没有内置对 Druid 数据源类型 的主动发现机制。若使用 hikariCP 数据源,则可以不配置 type 内容。

若把数据源配置到 mybatis-flex.datasource 下,使用 mybatis-flex 的数据源发现机制, 使用 druid 则可以不用配置 type,更多文档参考:多数据源章节。

官方文档参考:
https://mybatis-flex.com/zh/faq.html#spring-下使用-druid-数据源无法启动

如果使用的是 druid 数据库连接池,则需要添加数据源类型的配置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource。

官方文档参考:
https://mybatis-flex.com/zh/faq.html#springboot-项目-启动报错-property-sqlsessionfactory-or-sqlsessiontemplate-are-required

四、原理剖析

Spring Boot 数据源加载机制

  1. 自动配置流程

    检测DataSource实现
    存在spring.datasource.type?
    使用指定类型创建数据源
    尝试默认HikariCP
  2. Druid 的特殊性
    由于 Druid 不是 Spring Boot 官方默认连接池,必须通过 type 参数显式指定实现类。

MyBatis-Flex 依赖关系

Mapper接口
SqlSessionFactory
DataSource
连接池实现

当数据源初始化失败时,整个 MyBatis 体系无法构建,导致 Mapper 接口未被注册为 Spring Bean。

错误现象

启动时出现以下异常:

  1. No qualifying bean of type 'xxxMapper' available
  2. Field xxxMapper in xxxService required a bean of type 'xxxMapper ' that could not be found.

根本原因

Spring Boot 未内置 Druid 的自动发现机制,若未通过 spring.datasource.type 指定数据源类型,将导致:

  1. 数据源无法正确初始化
  2. MyBatis 的 Mapper 接口未被扫描注册

对比其他连接池

连接池类型是否需要声明 type
HikariCP否(Spring Boot 默认支持)
Druid必须显式声明

五、报错记录

运行单元测试报错

单元测试:

package com.example.hello.mybatisflex;import com.example.hello.mybatisflex.entity.Account;
import com.example.hello.mybatisflex.mapper.AccountMapper;
import com.mybatisflex.core.query.QueryWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static com.example.hello.mybatisflex.entity.table.AccountTableDef.ACCOUNT;@SpringBootTest
class HelloMybatisFlexApplicationTests {@Autowiredprivate AccountMapper accountMapper;@Testvoid contextLoads() {QueryWrapper queryWrapper = QueryWrapper.create().select().where(ACCOUNT.AGE.eq(18));Account account = accountMapper.selectOneByQuery(queryWrapper);System.out.println(account);}}

单元测试报错信息:

22:29:17.899 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.example.hello.mybatisflex.HelloMybatisFlexApplicationTests]: HelloMybatisFlexApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
22:29:18.409 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration com.example.hello.mybatisflex.HelloMybatisFlexApplication for test class com.example.hello.mybatisflex.HelloMybatisFlexApplicationTests.   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v3.4.5)2025-05-12T22:29:20.571+08:00  INFO 4944 --- [hello-mybatis-flex] [           main] c.e.h.m.HelloMybatisFlexApplicationTests : Starting HelloMybatisFlexApplicationTests using Java 21.0.1 with PID 4944 (started by SongGuanxun in E:\hello-world\hello-mybatis-flex)
2025-05-12T22:29:20.576+08:00  INFO 4944 --- [hello-mybatis-flex] [           main] c.e.h.m.HelloMybatisFlexApplicationTests : No active profile set, falling back to 1 default profile: "default"
2025-05-12T22:29:22.868+08:00  INFO 4944 --- [hello-mybatis-flex] [           main] c.e.h.m.HelloMybatisFlexApplicationTests : Started HelloMybatisFlexApplicationTests in 3.712 seconds (process running for 8.68)
2025-05-12T22:29:22.911+08:00  WARN 4944 --- [hello-mybatis-flex] [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener] to prepare test instance [com.example.hello.mybatisflex.HelloMybatisFlexApplicationTests@68d651f2]org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.hello.mybatisflex.HelloMybatisFlexApplicationTests': Unsatisfied dependency expressed through field 'accountMapper': No qualifying bean of type 'com.example.hello.mybatisflex.mapper.AccountMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:788) ~[spring-beans-6.2.6.jar:6.2.6]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:768) ~[spring-beans-6.2.6.jar:6.2.6]at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:146) ~[spring-beans-6.2.6.jar:6.2.6]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:509) ~[spring-beans-6.2.6.jar:6.2.6]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1451) ~[spring-beans-6.2.6.jar:6.2.6]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:405) ~[spring-beans-6.2.6.jar:6.2.6]at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:156) ~[spring-test-6.2.6.jar:6.2.6]at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:111) ~[spring-test-6.2.6.jar:6.2.6]at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:260) ~[spring-test-6.2.6.jar:6.2.6]at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:159) ~[spring-test-6.2.6.jar:6.2.6]at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$11(ClassBasedTestDescriptor.java:378) ~[junit-jupiter-engine-5.11.4.jar:5.11.4]at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:383) ~[junit-jupiter-engine-5.11.4.jar:5.11.4]at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$12(ClassBasedTestDescriptor.java:378) ~[junit-jupiter-engine-5.11.4.jar:5.11.4]at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184) ~[na:na]at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197) ~[na:na]at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179) ~[na:na]at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197) ~[na:na]at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1708) ~[na:na]at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509) ~[na:na]at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) ~[na:na]at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151) ~[na:na]at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174) ~[na:na]at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596) ~[na:na]at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:377) ~[junit-jupiter-engine-5.11.4.jar:5.11.4]at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$instantiateAndPostProcessTestInstance$7(ClassBasedTestDescriptor.java:290) ~[junit-jupiter-engine-5.11.4.jar:5.11.4]at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:289) ~[junit-jupiter-engine-5.11.4.jar:5.11.4]at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$5(ClassBasedTestDescriptor.java:279) ~[junit-jupiter-engine-5.11.4.jar:5.11.4]at java.base/java.util.Optional.orElseGet(Optional.java:364) ~[na:na]at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$6(ClassBasedTestDescriptor.java:278) ~[junit-jupiter-engine-5.11.4.jar:5.11.4]at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:31) ~[junit-jupiter-engine-5.11.4.jar:5.11.4]at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare$1(TestMethodTestDescriptor.java:105) ~[junit-jupiter-engine-5.11.4.jar:5.11.4]at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:104) ~[junit-jupiter-engine-5.11.4.jar:5.11.4]at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:68) ~[junit-jupiter-engine-5.11.4.jar:5.11.4]at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$2(NodeTestTask.java:128) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:128) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) ~[junit-platform-engine-1.11.4.jar:1.11.4]at java.base/java.util.ArrayList.forEach(ArrayList.java:1596) ~[na:na]at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:160) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:146) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:144) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:143) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:100) ~[junit-platform-engine-1.11.4.jar:1.11.4]at java.base/java.util.ArrayList.forEach(ArrayList.java:1596) ~[na:na]at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:160) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:146) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:144) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:143) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:100) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54) ~[junit-platform-engine-1.11.4.jar:1.11.4]at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:198) ~[junit-platform-launcher-1.11.4.jar:1.11.4]at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:169) ~[junit-platform-launcher-1.11.4.jar:1.11.4]at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:93) ~[junit-platform-launcher-1.11.4.jar:1.11.4]at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:58) ~[junit-platform-launcher-1.11.4.jar:1.11.4]at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:141) ~[junit-platform-launcher-1.11.4.jar:1.11.4]at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:57) ~[junit-platform-launcher-1.11.4.jar:1.11.4]at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:103) ~[junit-platform-launcher-1.11.4.jar:1.11.4]at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:85) ~[junit-platform-launcher-1.11.4.jar:1.11.4]at org.junit.platform.launcher.core.DelegatingLauncher.execute(DelegatingLauncher.java:47) ~[junit-platform-launcher-1.11.4.jar:1.11.4]at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:63) ~[junit-platform-launcher-1.11.4.jar:1.11.4]at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57) ~[junit5-rt.jar:na]at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) ~[junit-rt.jar:na]at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) ~[idea_rt.jar:na]at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) ~[junit-rt.jar:na]at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:231) ~[junit-rt.jar:na]at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55) ~[junit-rt.jar:na]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.hello.mybatisflex.mapper.AccountMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:2279) ~[spring-beans-6.2.6.jar:6.2.6]at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1702) ~[spring-beans-6.2.6.jar:6.2.6]at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1627) ~[spring-beans-6.2.6.jar:6.2.6]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:785) ~[spring-beans-6.2.6.jar:6.2.6]... 78 common frames omittedorg.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.hello.mybatisflex.HelloMybatisFlexApplicationTests': Unsatisfied dependency expressed through field 'accountMapper': No qualifying bean of type 'com.example.hello.mybatisflex.mapper.AccountMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:788)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:768)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:146)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:509)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1451)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:405)at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:156)at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:111)at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:260)at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:159)at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1708)at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)at java.base/java.util.Optional.orElseGet(Optional.java:364)at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.hello.mybatisflex.mapper.AccountMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:2279)at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1702)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1627)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:785)... 23 more进程已结束,退出代码为 -1

运行应用失败

当Mapper接口被依赖注入到其他组件中(比如Service中)时,会启动失败。

下面是依赖注入Service的代码示例:

package com.example.hello.mybatisflex.service;import com.example.hello.mybatisflex.entity.Account;
import com.example.hello.mybatisflex.mapper.AccountMapper;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import static com.example.hello.mybatisflex.entity.table.AccountTableDef.ACCOUNT;@Service
public class AccountService {@Autowiredprivate AccountMapper accountMapper;public void test() {QueryWrapper queryWrapper = QueryWrapper.create().select().where(ACCOUNT.AGE.eq(18));Account account = accountMapper.selectOneByQuery(queryWrapper);System.out.println(account);}}

启动失败,报错如下:

.   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v3.4.5)2025-05-12T22:51:15.777+08:00  INFO 13200 --- [hello-mybatis-flex] [           main] c.e.h.m.HelloMybatisFlexApplication      : Starting HelloMybatisFlexApplication using Java 21.0.1 with PID 13200 (E:\hello-world\hello-mybatis-flex\target\classes started by SongGuanxun in E:\hello-world\hello-mybatis-flex)
2025-05-12T22:51:15.785+08:00  INFO 13200 --- [hello-mybatis-flex] [           main] c.e.h.m.HelloMybatisFlexApplication      : No active profile set, falling back to 1 default profile: "default"
2025-05-12T22:51:17.561+08:00  WARN 13200 --- [hello-mybatis-flex] [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountService': Unsatisfied dependency expressed through field 'accountMapper': No qualifying bean of type 'com.example.hello.mybatisflex.mapper.AccountMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2025-05-12T22:51:17.586+08:00  INFO 13200 --- [hello-mybatis-flex] [           main] .s.b.a.l.ConditionEvaluationReportLogger : Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2025-05-12T22:51:17.699+08:00 ERROR 13200 --- [hello-mybatis-flex] [           main] o.s.b.d.LoggingFailureAnalysisReporter   : ***************************
APPLICATION FAILED TO START
***************************Description:Field accountMapper in com.example.hello.mybatisflex.service.AccountService required a bean of type 'com.example.hello.mybatisflex.mapper.AccountMapper' that could not be found.The injection point has the following annotations:- @org.springframework.beans.factory.annotation.Autowired(required=true)Action:Consider defining a bean of type 'com.example.hello.mybatisflex.mapper.AccountMapper' in your configuration.进程已结束,退出代码为 1

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/81093.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

安全生产调度管理系统的核心功能模块

安全生产调度管理系统是运用现代信息技术构建的智能化管理平台&#xff0c;旨在实现生产安全风险的全面管控和应急资源的优化调度。该系统通过整合物联网、大数据、人工智能等前沿技术&#xff0c;建立起覆盖风险监测、预警预测、指挥调度、决策支持的全链条安全管理体系。 一…

桃芯ingchips——windows HID键盘例程无法同时连接两个,但是安卓手机可以的问题

目录 环境 现象 原理及解决办法 环境 PC&#xff1a;windows11 安卓&#xff1a;Android14 例程使用的是HID Keyboard&#xff0c;板子使用的是91870CQ的开发板&#xff0c;DB870CC1A 现象 连接安卓手机时并不会出现该现象&#xff0c;两个开发板都可以当做键盘给手机发按…

JavaScript - JavaScript 运算符之圆括号运算符与方括号运算符(圆括号运算符概述、圆括号运算符用法、方括号运算符概述、方括号运算符用法)

一、圆括号运算符概述 圆括号运算符&#xff08;()&#xff09;主要用于函数调用、表达式分组、多种语法结构登 二、圆括号运算符用法 调用函数 function greet() {console.log("Hello!"); }greet();# 输出结果Hello!当箭头函数有多个参数或零个参数时需要括号 c…

AG-UI 协议:重构多模态交互,开启智能应用新纪元

一、协议诞生的时代背景&#xff1a;填补 AI 生态最后一块拼图 在人工智能技术飞速发展的今天&#xff0c;AI 代理&#xff08;Agent&#xff09;作为能够主动执行复杂任务的智能实体&#xff0c;正从实验室走向生产环境&#xff0c;重塑各个行业的工作流程。然而&#xff0c;…

嵌入式学习的第二十天-数据结构-调试+链表的一般操作

一、调试 1.一般调试 2.找段错误 二、链表的一般操作 1.单链表的修改 int ModifyLinkList(LinkList*ll,char*name,DATATYPE*data) {DATATYPE * tmp FindLinkList(ll, name);if(NULL tmp){return 1;}memcpy(tmp,data,sizeof(DATATYPE));return 0; } 2.单链表的销毁 int D…

如何同时管理不同平台的多个账号?

在当今数字营销、电商运营、跨境贸易盛行的时代&#xff0c;同时管理多个平台的账号几乎成了从业者的标配。无论是做社媒营销的广告主&#xff0c;还是操作亚马逊、eBay、Shopee 等平台的跨境卖家&#xff0c;多账号运营都是提升曝光、分散风险、扩大收益的重要方式。 然而&am…

STM32外设AD/DA-基础及CubeMX配置

STM32外设AD/DA-基础及CubeMX配置 一&#xff0c;什么是AD/DA二&#xff0c;基础概念1&#xff0c;模拟 vs 数字2&#xff0c;AD转换1&#xff0c;分辨率 (Resolution)2&#xff0c;参考电压 (Reference Voltage, Vref)3&#xff0c;采样率 (Sampling Rate) 3&#xff0c;DA转换…

【软考 霍夫曼编码的文档压缩比】

霍夫曼编码的文档压缩比计算基于字符频率的最优编码分配&#xff0c;以下是详细步骤及相关案例&#xff1a; 一、压缩比计算公式 [ \text{压缩比} \frac{\text{压缩前总比特数}}{\text{压缩后总比特数 编码表存储开销}} ] 通常以 比率&#xff08;如 3:1&#xff09; 或 百分…

关闭VSCode 自动更新

参考&#xff1a;关闭VSCode 自动更新_vscode关闭自动更新-CSDN博客 vscode的设置 Update: Mode Update: Enable Windows Background Updates Extensions: Auto Check Updates Extensions: Auto Update

Flask框架搭建

1、安装Flask 打开终端运行以下命令&#xff1a; pip install Flask 2、创建项目目录 在Windows上&#xff1a; venv\Scripts\activate 执行 3、创建 app.py 文件 可以在windows终端上创建app.py文件 &#xff08;1&#xff09;终端中创建 使用echo命令 echo "fr…

5G-A和未来6G技术下的操作系统与移动设备变革:云端化与轻量化的发展趋势

目录 5G技术带来的革命性变革 云端化操作系统的实现路径 完全云端化模式 过渡性解决方案 未来操作系统的发展方向 功能架构演进 安全机制强化 移动设备的形态变革 终端设备轻量化 物联网设备简化 实施挑战与应对策略 技术挑战 商业模式创新 总结与展望 5G技术作为…

【漫话机器学习系列】261.工具变量(Instrumental Variables)

工具变量&#xff08;Instrumental Variables&#xff09;通俗图解&#xff1a;破解内生性困境的利器 在数据建模与因果推断过程中&#xff0c;我们经常遇到一个棘手问题&#xff1a;内生性&#xff08;Endogeneity&#xff09;。它会导致模型估计产生偏差&#xff0c;进而误导…

CSS:颜色的三种表示方式

文章目录 一、rgb和rgba方式二、HEX和HEXA方式&#xff08;推荐&#xff09;三、hsl和hsla方式四、颜色名方式 一、rgb和rgba方式 10进制表示方法 二、HEX和HEXA方式&#xff08;推荐&#xff09; 就是16进制表示法 三、hsl和hsla方式 语法&#xff1a;hsl(hue, satura…

支付宝授权登录

支付宝授权登录 一、场景 支付宝小程序登录&#xff0c;获取用户userId 二、注册支付宝开发者账号 1、支付宝开放平台 2、点击右上角–控制台&#xff0c;创建小程序 3、按照步骤完善信息&#xff0c;生成密钥时会用到的工具 4、生成的密钥&#xff0c;要保管好&#xff…

涂色不踩雷:如何优雅解决 LeetCode 栅栏涂色问题

文章目录 摘要描述例子&#xff1a; 题解答案&#xff08;Swift&#xff09;题解代码分析动态规划核心思路初始条件 示例测试及结果示例 1&#xff1a;示例 2&#xff1a;示例 3&#xff1a; 时间复杂度空间复杂度总结实际场景联系 摘要 在用户体验和界面设计中&#xff0c;颜…

GEE计算 RSEI(遥感生态指数)

&#x1f6f0;️ 什么是 RSEI&#xff1f;为什么要用它评估生态环境&#xff1f; RSEI&#xff08;遥感生态指数&#xff0c;Remote Sensing Ecological Index&#xff09; 是一种通过遥感数据计算得到的、综合反映区域生态环境质量的指标体系。 它的设计初衷是用最少的变量&…

图像处理:预览并绘制图像细节

前言 因为最近在搞毕业论文的事情&#xff0c;要做出一下图像细节对比图&#xff0c;所以我这里写了两个脚本&#xff0c;一个用于框选并同时预览图像放大细节&#xff0c;可显示并返回框选图像的坐标&#xff0c;另外一个是输入框选图像的坐标并将放大的细节放置在图像中&…

基于javaweb的SSM驾校管理系统设计与实现(源码+文档+部署讲解)

技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、小程序、安卓app、大数据、物联网、机器学习等设计与开发。 主要内容&#xff1a;免费功能设计、开题报告、任务书、中期检查PPT、系统功能实现、代码编写、论文编写和辅导、论文…

限制 MySQL 服务只能被内网 `192.168.1.*` 网段的设备访问

1. 修改 MySQL 配置文件 MySQL 默认监听所有网络接口(0.0.0.0),需要将其绑定到内网 IP 地址或限制访问范围。 (1)编辑 MySQL 配置文件 找到 MySQL 的主配置文件,通常是 /etc/my.cnf 或 /etc/mysql/my.cnf。使用文本编辑器打开: sudo vi /etc/my.cnf(2)设置 bind-a…

uniapp-商城-55-后台 新增商品(分类、验证和弹窗属性)

1、概述 在前面 &#xff0c;我们将商品页面的布局给完成了&#xff0c;这里来对表单的标签输入进行校验&#xff0c;看看这里的校验还是不是也需要兼容微信小程序&#xff0c;还有没有前面遇到的自定义正则进行校验的情况。 另外这里还需要完成商品属性的添加&#xff0c;就是…