Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在spring boot中启动应用程序时清除实体表数据_Spring_Spring Boot - Fatal编程技术网

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

在spring boot中启动应用程序时清除实体表数据,spring,spring-boot,Spring,Spring Boot,我试图在spring boot中创建一个entity对象,这样在应用程序启动时需要清除与实体相关的表数据 如果我们考虑一个会话相关的数据需要在应用程序启动时被清除, 因为所有的旧数据都是无用的 我必须手动清除表还是有办法配置表?使用CommandLineRunner并放置代码以清除表。它们的CommandLineRunner总是在应用程序启动时运行 @Component public class TableClearer implements CommandLineRunner{ //Autow

我试图在spring boot中创建一个entity对象,这样在应用程序启动时需要清除与实体相关的表数据

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


我必须手动清除表还是有办法配置表?

使用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,它是否仅适用于选定的表您的问题不清楚您是否只希望在应用程序启动时清除特定的表。那样的话,你可以看看。