在spring boot中启动应用程序时清除实体表数据
我试图在spring boot中创建一个entity对象,这样在应用程序启动时需要清除与实体相关的表数据在spring boot中启动应用程序时清除实体表数据,spring,spring-boot,Spring,Spring Boot,我试图在spring boot中创建一个entity对象,这样在应用程序启动时需要清除与实体相关的表数据 如果我们考虑一个会话相关的数据需要在应用程序启动时被清除, 因为所有的旧数据都是无用的 我必须手动清除表还是有办法配置表?使用CommandLineRunner并放置代码以清除表。它们的CommandLineRunner总是在应用程序启动时运行 @Component public class TableClearer implements CommandLineRunner{ //Autow
如果我们考虑一个会话相关的数据需要在应用程序启动时被清除, 因为所有的旧数据都是无用的
我必须手动清除表还是有办法配置表?使用CommandLineRunner并放置代码以清除表。它们的CommandLineRunner总是在应用程序启动时运行
@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
}
}
我相信spring数据库初始化可以为您解决这个问题。您可以将
spring.jpa.hibernate.ddl auto
与create drop
值一起使用,谢谢@Sajjad,它是否仅适用于选定的表您的问题不清楚您是否只希望在应用程序启动时清除特定的表。那样的话,你可以看看。