Spring Boot应用程序启动时如何在表中插入数据?

问题描述:

如何在Spring Boot应用程序启动时将数据插入表中?我的应用程序是由JHipster生成的.我需要检查的是该表中是否已经存在该特定数据.如果没有,我应该添加它.

How can I insert data to a table on Spring Boot application start? My application is generated by JHipster. What I need to check is that if that particular data already exist in that table or not. If it doesn't I should add it.

您可以实现

You can implement an ApplicationRunner and use a repository to do whatever you need to do.

如果ApplicationRunner是spring bean,它将在应用程序启动时运行.

If the ApplicationRunner is a spring bean it is run on application startup.

对于更复杂的要求,我将尝试依靠类似

For more sophisticated requirements I would try to rely on a tool like flyway that has good integration with spring boot