Spring Boot-加载初始数据 [英] Spring Boot - Loading Initial Data

查看:85
本文介绍了Spring Boot-加载初始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在应用程序启动之前加载初始数据库数据的最佳方法是什么?我正在寻找的东西将会用数据填充我的H2数据库.

I'm wondering what the best way to load initial database data before the application starts? What I'm looking for is something that will fill my H2 database with data.

例如,我有一个域模型"User",我可以通过转到/users来访问用户,但是最初数据库中没有任何用户,所以我必须创建它们.反正有没有自动用数据填充数据库?

For example, I have a domain model "User" I can access users by going to /users but initially there won't be any users in the database so I have to create them. Is there anyway to fill the database with data automatically?

此刻,我有一个由容器实例化并为我创建用户的Bean.

At the moment I have a Bean that gets instantiated by the container and creates users for me.

示例:

@Component
public class DataLoader {

    private UserRepository userRepository;

    @Autowired
    public DataLoader(UserRepository userRepository) {
        this.userRepository = userRepository;
        LoadUsers();
    }

    private void LoadUsers() {
        userRepository.save(new User("lala", "lala", "lala"));
    }
}

但是我非常怀疑这是最好的方法.是吗?

But I very much doubt that is the best way of doing it. Or is it?

推荐答案

您只需在 src/main/resources 文件夹中创建一个 data.sql 文件,即可将在启动时自动执行.在此文件中,您只需添加一些插入语句,例如:

You can simply create a data.sql file in your src/main/resources folder and it will be automatically executed on startup. In this file you just add some insert statements, eg.:

INSERT INTO users (username, firstname, lastname) VALUES
  ('lala', 'lala', 'lala'),
  ('lolo', 'lolo', 'lolo');


类似地,您也可以创建 schema.sql 文件(或schema-h2.sql)来创建架构:


Similarly, you can create a schema.sql file (or schema-h2.sql) as well to create your schema:

CREATE TABLE task (
  id          INTEGER PRIMARY KEY,
  description VARCHAR(64) NOT NULL,
  completed   BIT NOT NULL);

尽管通常情况下您不必执行此操作,因为Spring引导已将Hibernate配置为基于内存数据库中的实体创建架构.如果您确实要使用schema.sql,则必须通过将其添加到application.properties中来禁用此功能:

Though normally you shouldn't have to do this since Spring boot already configures Hibernate to create your schema based on your entities for an in memory database. If you really want to use schema.sql you'll have to disable this feature by adding this to your application.properties:

spring.jpa.hibernate.ddl-auto=none

有关此文档的更多信息,请参见

More information can be found at the documentation about Database initialization.

如果您使用的是 Spring boot 2 ,则数据库初始化仅适用于嵌入式数据库(H2,HSQLDB等).如果还要将其用于其他数据库,则需要更改spring.datasource.initialization-mode属性:

If you're using Spring boot 2, database initialization only works for embedded databases (H2, HSQLDB, ...). If you want to use it for other databases as well, you need to change the spring.datasource.initialization-mode property:

spring.datasource.initialization-mode=always


如果您使用多个数据库供应商,则可以根据要使用的数据库平台将文件命名为 data-h2.sql data-mysql.sql 使用.


If you're using multiple database vendors, you can name your file data-h2.sql or data-mysql.sql depending on which database platform you want to use.

要使其正常工作,您必须通过以下方式配置spring.datasource.platform属性:

To make that work, you'll have to configure the spring.datasource.platform property though:

spring.datasource.platform=h2

这篇关于Spring Boot-加载初始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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