繁体   English   中英

Spring:使用 ibatis 库中的 Java Mapper 时出错。 接口不是bean

[英]Spring: error using Java Mapper from ibatis library. The interface is not a 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作为注释的接口( @Mapper )。

但是,在运行阶段,我收到以下错误:

应用程序myInterface启动:字段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

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

你好添加@MapperScan

@SpringBootApplication
@MapperScan("com.demo.mappers")
public class SpringbootDemoApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(SpringbootDemoApplication.class, args);
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM