在一次刚搭建项目结构,想看看自己的dao层是否有效,打算用测试类,测试一下,dao和service有没有用,发现,空指针,后来就试着打印相关mapper和service,发现为空!!!

原来的代码:

@SpringBootTest
public class MybatisTest {
    @Autowired
    DemoService demoService;
    @Autowired
    DemoMapper demoMapper;
    @Test
    public void daoTEST(){
       //demoService.getUser();
        System.out.println(demoMapper);
        System.out.println(demoService);
    }
}

打印结果:
在这里插入图片描述

现在代码:


@RunWith(SpringRunner.class)
@SpringBootTest(classes = SxApplication.class)
public class MybatisTest {
    @Autowired
    DemoService demoService;
    @Autowired
    DemoMapper demoMapper;
    @Test
    public void daoTEST(){
       //demoService.getUser();
        System.out.println(demoMapper);
        System.out.println(demoService);
    }
}

结果 :
在这里插入图片描述

参考链接:https://blog.csdn.net/m0_37830775/article/details/105278710?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163955866516780274185722%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=163955866516780274185722&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-1-105278710.pc_search_result_cache&utm_term=springboot%E6%B5%8B%E8%AF%95%E7%B1%BB%E6%B3%A8%E5%85%A5service&spm=1018.2226.3001.4187

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐