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

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

例如,我有一个域模型用户",我可以通过/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 create a data.sql file in your src/main/resources folder and it will be automatically executed on startup. In this file you can 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 Boot 已经配置了 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

更多信息可以在关于数据库初始化.

如果您使用的是 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.sqldata-mysql.sql使用.

要使其工作,您必须配置 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天全站免登陆