简体   繁体   中英

Spring: error using Java Mapper from ibatis library. The interface is not a bean

I am developing a Back-end application using Java and Spring framework. I start from an archetypal project which, however, causes some problems in the run phase. I have this interface:

@Mapper
public interface MyInterface {

    public ReturnType1 method1(long myLong);

    @Select("SELECT * FROM myTab WHERE id = #{myLong}")
    public ReturnType2 method2(long myLong);

    ...
    ...
}

This interface uses the ibatis library (class org.apache.ibatis.annotations.Mapper ); Elsewhere in the code I have this service:

@Service
public class ExampleService {

    @Autowired
    private MyInterface myInterface;

    ...
}

where the @Service annotation is org.springframework.stereotype.Service; . Thanks to @Autowired , this service uses the interface that has @Mapper as an annotation (seen before).

However, in the run phase I get the following error:

APPLICATION FAILED TO START : Field myInterface required a bean of type MyInterface that could not be found. The injection point has the following annotations: @org.springframework.beans.factory.annotation.Autowired(required=true) . Consider defining a bean of type ' MyInterface ' in your configuration.

Why is this error reported to me? I am not familiar with the ibatis library... in my project I have an xml file in this path: myProject/src/main/resources/mybatis/FileMapper.xml and in the application.properties file I have this row:

mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml

It seems to me that everything is configured correctly. Could you explain to me where and why I get this error?

Hello add @MapperScan

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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