我正在尝试在 spring boot 中创建实体对象,以便在应用程序启动时需要清除与实体相关的表数据。
例如,如果我们认为与会话相关的数据需要在应用程序启动时清除, 因为所有旧数据都没有用。
我必须手动清除表格还是可以配置表格?
答案 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
}
}