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上该问题的回答

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值