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 需要一个类型为 MyInterface 的 bean,但无法找到。注入点有以下注释:@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

在我看来,一切都配置正确。你能向我解释我在哪里以及为什么会出现这个错误吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)