在Spring Boot中启动应用程序时清除实体表数据

时间:2020-09-07 09:57:15

标签: spring spring-boot

我正在尝试在 spring boot 中创建实体对象,以便在应用程序启动时需要清除与实体相关的表数据。

例如,如果我们认为与会话相关的数据需要在应用程序启动时清除, 因为所有旧数据都没有用。

我必须手动清除表格还是可以配置表格?

1 个答案:

答案 0 :(得分:1)

使用CommandLineRunner并放置代码以清除其表,CommandLineRunners始终在应用程序启动时运行

@Component
public class TableClearer implements CommandLineRunner{
//Autowire your dependency here eg the entity repository
@Override
public void run(String... args) throws Exception {
   //place your code here
    
}

}