简体   繁体   English

如何在Play Framework中使用Ebean将初始数据加载/插入数据库中?

[英]How to load / insert initial data into database with Ebean in Play Framework?

I need to some initial data (from csv files) into database. 我需要一些初始数据(来自csv文件)到数据库中。 I am using Ebean with Play! 我正在将Ebean与Play一起使用! Framework. 框架。 I read the doc . 我看了文档 It says using YAML files to store the data and call Ebean.save(). 它说使用YAML文件存储数据并调用Ebean.save()。 And this is done in a Test. 这是在测试中完成的。

My questions: 我的问题:

  1. Where should I insert my data? 我应该在哪里插入数据? (Test is probably not the ideal place, as this data should be used in production) (测试可能不是理想的地方,因为此数据应在生产中使用)

  2. Can I write my own code to get my data from existing csv files, instead of using YAML files? 我可以编写自己的代码来从现有的csv文件中获取数据,而不使用YAML文件吗?

Any suggestions or doc links will be appreciated. 任何建议或文档链接将不胜感激。

Thanks! 谢谢!

A Play Framework 2.4 solution to loading initial data on application startup, using Ebean: 使用Ebean的Play Framework 2.4解决方案,用于在应用程序启动时加载初始数据:

First, create a class where you want perform the initial data load. 首先,在要执行初始数据加载的位置创建一个类。 According to the docs this has to be in the constructor. 根据文档,这必须在构造函数中。

public class InitialData {
    public InitialData() {

        if (Ebean.find(User.class).findRowCount() == 0) {
            InputStream is = this.getClass().getClassLoader().getResourceAsStream("initial-data.yml");

            @SuppressWarnings("unchecked")
            Map<String, List<Object>> all = 
                (Map<String, List<Object>>)Yaml.load(is, this.getClass().getClassLoader());

            //Yaml.load("initial-data.yml"); //calls Play.application() which isn't available yet
            Ebean.save(all.get("users"));
        }
    }
}

NOTE: I used Yaml's load(InputStream is, ClassLoader classloader) method over its load(String resourceName) method because it does not work. 注意:我在其load(String resourceName)方法上使用Yaml的load(InputStream is, ClassLoader classloader)方法,因为它不起作用。 I believe this is because of its use of Play.application(), as this is not available before the application is started. 我相信这是因为它使用了Play.application(),因为在启动应用程序之前这是不可用的。 Source Here 来源这里

Second, According to Play docs, to execute code before startup, use Eager-bindings. 其次,根据Play文档,要在启动之前执行代码,请使用Eager绑定。 This will cause your code in the InitialData constructor to execute. 这将导致您在InitialData构造函数中的代码执行。

public class MainModule extends AbstractModule implements AkkaGuiceSupport {
    @Override
    protected void configure() {
        //this is where the magic happens
        bind(InitialData.class).asEagerSingleton();
    }
}

Make sure to add your MainModule class to the application.conf 确保将MainModule类添加到application.conf

# modules
play.modules.enabled += "module.MainModule"

With further search, I found the solution is described here 经过进一步的搜索,我发现这里描述了解决方案

The code that perform initial insertion needs to be hooked into Play's startup. 需要将执行初始插入的代码挂接到Play的启动程序中。

Hooking into Play's startup is as simple as creating a class called Global that implements GlobalSettings in the root package, and overriding the onStart() method. 挂接到Play的启动很简单,只需创建一个名为Global的类,该类在根包中实现GlobalSettings,然后覆盖onStart()方法。 Let's do that now, by creating the app/Global.java fileHooking into Play's startup is as simple as creating a class called Global that implements GlobalSettings in the root package, and overriding the onStart() method. 现在,通过创建app / Global.java文件来执行此操作。挂接到Play的启动就像创建一个名为Global的类一样简单,该类在根包中实现GlobalSettings并覆盖onStart()方法。 Let's do that now, by creating the app/Global.java file 现在,通过创建app / Global.java文件来执行此操作

Note that, it said the class needs to be in the root package. 请注意,它表示该类必须位于根包中。

code example: 代码示例:

import play.*;
import play.libs.*;
import com.avaje.ebean.Ebean;
import models.*;
import java.util.*;

public class Global extends GlobalSettings {
    @Override
    public void onStart(Application app) {
        // Check if the database is empty
        if (User.find.findRowCount() == 0) {
            Ebean.save((List) Yaml.load("initial-data.yml")); // You can use whatever data source you have here. No need to be in YAML.
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM