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.pswp.cn/pingmian/81093.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/81093.shtml
英文地址,請注明出處:http://en.pswp.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;就是…