SpringBoot启动类中调用Controller层,Service层,Dao层并且查询数据

 

背景:最近在做定时器,需要从底部查询数据,我想到先从启动类入手,项目启动就先查一下数据,看看能实现否,结果发现,如果只是调用三层中不查询数据库的方法,是可以在启动类调用的,一旦在启动类调用三层涉及到数据库查询就会报错,人直接傻了。

 

最后发现,只需要实现CommandLineRunner类即可,在run方法中,调用三层数据查询方法即可。

 

@SpringBootApplication
@ServletComponentScan
@EnableScheduling
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class EnergyApplication implements CommandLineRunner {
    
    @Autowired
    private LandBaseInfoDao landBaseInfoDao;

    public static void main(String[] args) {
        SpringApplication.run(EnergyApplication.class,args);
    }

    @Override
    public void run(String... args) throws Exception {

        landBaseInfoDao.getAll();
    }
}

 

posted @ 2021-04-27 10:11  万里哥  阅读(2854)  评论(0编辑  收藏  举报