在春季启动应用程序时清除实体表数据



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

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

我必须手动清除该表吗?或者有办法配置它吗

使用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

}
}

最新更新