使用示例数据初始化的Spring + Hibernate MySQL数据库 [英] Spring+Hibernate mysql dataBase initialized with sample data

查看:46
本文介绍了使用示例数据初始化的Spring + Hibernate MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想部署一个Spring-mvc + Hibernate项目,并且我想要:

I want to deploy a Spring-mvc + Hibernate project, and I want:

  1. 自动为第一个项目创建所有数据库模式
  2. 如果添加了属性,则更新架构.
  3. 在某些表中添加一些记录,USER等的根用户.

如果将 hibernate.hbm2ddl.auto 设置为"create" ,则每次重新启动项目时,我都会丢失以前的数据.如果设置为"update" ,我将在其中放置sql来创建示例记录的 import.sql 不会执行.

If hibernate.hbm2ddl.auto is set to "create", I will lost previous data every time I restart the project. If set to "update", my import.sql where i put sql to create sample records, won't be executed.

那么,在产品"环境中最好的做法是什么?

So, what's the best pactice in a "product" environment?

推荐答案

hibernate.hbm2ddl.auto 在要保留旧数据的情况下无法自动处理更复杂的数据库更新方案

hibernate.hbm2ddl.auto is not able to automatically handle more complex database update scenarios, in cases when you want to retain old data.

对于生产,我建议 http://www.liquibase.org/我在Liquibase方面拥有丰富的经验(在多个项目中使用了几年).

I have good experience with Liquibase (using it for a few years, in multiple projects).

有一些学习曲线和一些重复项(您必须自己创建数据库结构,而这些工具不能从实体中创建数据库结构).

There is some learning curve, and some duplication (you will have to create DB structure on your own, those tools do not create DB structure from entities).

请参见

See http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-execute-liquibase-database-migrations-on-startup for details how to onboard one of those tools.

即使没有Spring Boot,它也非常简单(只需将新库添加到类路径中,创建第一个脚本和一个Spring Bean).

Even without Spring Boot, is is really simple (just add new library to the classpath, create first script and one Spring Bean).

如果您对使用上述概念的示例项目感兴趣,参见 https://spring.io/blog/2016/05/31/zero-downtime-deployment-with-a-database

IF you are interested in sample project using concepts described above, see https://spring.io/blog/2016/05/31/zero-downtime-deployment-with-a-database

IT描述了更复杂的情况-如何在不停机的情况下更新数据库.

IT describes even more sophisticated scenario- how to update DB without downtime for application.

这篇关于使用示例数据初始化的Spring + Hibernate MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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