Oleksandr Cherniaiev
Oleksandr Cherniaiev

Reputation: 813

Idea inspects batis mapper bean wrong

There's web project with Spring and MyBatis. I use IntelliJ IDEA for development. IDEA cannot correctly inspect MyBatis beans and produces annoying underscorings, though link to Data Access Object is present.

Inspection comment:

 Could not autowire. No beans of 'ApplicationMapper' type found.

My Spring and MyBatis configurations: Spring:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:spring/mybatis-config.xml"/>

</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.db.gbs.gbsapps.rds.backend.model.integration.mapping"/>
</bean>

mybatis-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <mappers>
        <mapper resource="mybatis/ApplicationMapper.xml"/>
    </mappers>
</configuration>

Is there a way to fix this small issue?

Upvotes: 7

Views: 12511

Answers (7)

Jess
Jess

Reputation: 3715

@Resource
UserMapper userMapper;

Other related thing : resource vs autowired

Upvotes: 0

DanielJyc
DanielJyc

Reputation: 753

In my case, myBatis plugin dose not work. Adding the following before the variable declaration will work.

@Autowired
@SuppressWarnings("SpringJavaAutowiringInspection")
private ApplicationMapper applicationMapper;@Autowired

Upvotes: 0

Clement.Xu
Clement.Xu

Reputation: 1308

How about defining a new annotation for convenience:

@Repository
@Mapper
public @interface MyMapper{

}

@MyMapper
public interface ApplicationMapper {
    //...
}

Upvotes: 2

@Repository
@Mapper 
public interface ApplicationMapper {

will do the trick

Upvotes: 19

zhouji
zhouji

Reputation: 5336

Another way is to add @Component or @Repository to your mapper interface.

Such as:

@Repository
public interface ApplicationMapper {
    //...
}

Upvotes: 7

rkJun
rkJun

Reputation: 481

I got the same problem. In my Intelli J Inspection Error,

Could not autowire. No beans of 'ApplicationMapper' type found. less... (Ctrl+F1) Checks autowiring problems in a bean class.

in my case, disabled inspections. (Alt + Enter quick fix or change settings)

Settings - Editor - Inspections - Spring - Spring Core - Code - Autowiring for Bean Class - disable

(2015.04.27 update) After installed myBatis plugin, I had solved this problem, too

Upvotes: 3

Oleksandr Cherniaiev
Oleksandr Cherniaiev

Reputation: 813

Updated MyBatis plugin is fixed and does not have this issue.

Upvotes: 3

Related Questions