Spring Boot应用程序启动时如何在表中插入数据? [英] How to insert data to table on spring boot application start?

查看:89
本文介绍了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

这篇关于Spring Boot应用程序启动时如何在表中插入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆