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.
相关问题
- java.lang.IllegalArgumentException: Cannot set to
- Spring Data MongoDB - lazy access to some fields
- Declaring an explict object dependency in Spring
- Decoding body parameters with Spring
- Spring Integration - Inbound file endpoint. How to
相关文章
- java JDK动态代理和cglib动态代理最后获取的代理对象都为null的问题
- org.xml.sax.SAXParseException; lineNumber: 7; colu
- SpringMVC如何把File封装到Map中?
- Spring: controller inheritance using @Controller a
- How to load @Configuration classes from separate J
- Java spring framework - how to set content type?
- Java/Spring MVC: provide request context to child
- Spring 5 Web Reactive - Hot Publishing - How to us
You can implement an
ApplicationRunner
and use a repository to do whatever you need to do.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
If your application was generated by JHipster, then it should already include and be properly configured for Liquibase.
You have a few options:
loadData
tag in00000000000000_initial_schema.xml
for examples.All three options can be combined with preconditions to make sure the data doesn't exist before trying to insert it. If you need the changeset to run every time you boot your application, you can use the
runAlways="true"
attribute (docs for that are on this page).You can create a function in your service class which checks if the data already exists or not and if not then save that data.