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

 

最后发现,只需要实现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();
    }
}

 

相关文章:

  • 2022-03-05
  • 2021-08-26
  • 2021-09-26
  • 2021-09-05
  • 2021-12-11
  • 2021-05-18
  • 2022-12-23
  • 2021-12-13
猜你喜欢
  • 2021-05-25
  • 2022-12-23
  • 2021-10-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案