背景:最近在做定時器,需要從底部查詢數據,我想到先從啟動類入手,項目啟動就先查一下數據,看看能實現否,結果發現,如果只是調用三層中不查詢數據庫的方法,是可以在啟動類調用的,一旦在啟動類調用三層涉及到數據庫查詢就會報錯,人直接傻了。
最后發現,只需要實現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(); } }