java - Spring:使用来自 ibatis 库的 Java Mapper 时出错。接口不是 bean
问题描述
我正在使用 Java 和 Spring 框架开发一个后端应用程序。我从一个原型项目开始,但是在运行阶段会导致一些问题。我有这个界面:
@Mapper
public interface MyInterface {
public ReturnType1 method1(long myLong);
@Select("SELECT * FROM myTab WHERE id = #{myLong}")
public ReturnType2 method2(long myLong);
...
...
}
该接口使用了ibatis库(类org.apache.ibatis.annotations.Mapper
);在代码的其他地方我有这个服务:
@Service
public class ExampleService {
@Autowired
private MyInterface myInterface;
...
}
@Service
注释在哪里org.springframework.stereotype.Service;
。多亏了@Autowired
,此服务使用具有@Mapper
作为注释的接口(见前)。
但是,在运行阶段我收到以下错误:
应用程序启动失败:字段需要找不到myInterface
类型的 bean 。MyInterface
注入点有以下注解:@org.springframework.beans.factory.annotation.Autowired(required=true)
. MyInterface
考虑在您的配置中定义一个 ' ' 类型的 bean 。
为什么会向我报告此错误?我不熟悉ibatis库......在我的项目中,我在这个路径中有一个 xml 文件:myProject/src/main/resources/mybatis/FileMapper.xml并且在 application.properties 文件中我有这一行:
mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml
在我看来,一切都配置正确。你能向我解释一下我在哪里以及为什么会收到这个错误吗?
解决方案
你好添加@MapperScan
@SpringBootApplication
@MapperScan("com.demo.mappers")
public class SpringbootDemoApplication
{
public static void main(String[] args)
{
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
推荐阅读
- scala - 根据文件名从 DF 生成 txt
- android - 如何通过改造将数组嵌套对象 Json 解析为 Kotlin?
- node.js - 在 MERN 堆栈应用程序的身份验证过程中结合电话号码验证的最佳方法
- c# - ASP.NET Core:使用动作过滤器向方法添加自定义参数
- django - Wagtail 流图像忽略 HTML 文件
- windows - 什么 Windows SDK 用于监视用户活动?
- node.js - 尝试将我的 API 部署到 heroku 时遇到此问题
- javascript - 在此 AJAX 调用循环上设置延迟
- python - 用作索引的 Python 数组必须是整数(或布尔)类型
- web-scraping - 使用 Rvest 抓取维基百科表格(错误)