天天看点

idea中Mybatis的mapper接口使用@Autowired引入报错

1.环境

  • springBoot
  • idea
  • mybatis
  • gradle

2.源代码

@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:config/application.properties")
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@SpringBootTest(classes = MapperBoot.class)
@MybatisTest
public class GroupMapperTest {

    @Autowired
    GroupMapper groupMapper;

    @Test
    public void findAll() throws Exception {
        List<Group> list = groupMapper.findAll();
        Assert.assertNotNull(list);
        System.out.println(list.size());
    }

}
           

3.问题描述

在进行springBootTest的时候使用@Autowired引入mapper接口类,idea提示could not autowired,直接执行测试用例,发现能正常运行。
           

4.问题猜想

(1)由于执行测试用例是正常的,说明spring实例化了该接口,猜想可能是idea对mapper注解的识别问题,导致提示红线错误。 
(2)后续在项目代码中使用@Autowired引入,发现无错误提示,而项目代码启动类添加了@MapperScan,所以猜测是该注解的作用,然后把该注解加在了测试类上。发现没有作用。
(3)后续猜想是否是在测试目录下,idea对引入类的识别和代码目录下有一些区别...
           

5.网上案例

(1)网上采用给接口添加@Repository注解的方式,我认为该方式有一些不足,该注解仅仅起到了让idea识别到的作用。
           

附加

目前来说在项目中的引用没有错误提示,仅仅在测试类中出现,猜测可能是idea的bug。

stackeOverFlow上该问题的回答

继续阅读