簡體   English   中英

如何在Play Framework中設置2個MySQL數據庫?

[英]How do I set up 2 MySQL databases in Play Framework?

我需要設置2個單獨的數據庫。 一個用於Test類,另一個用於TestTwo類,但是我不知道如何配置application.conf文件。

application.conf(第1部分):

db.default.driver=com.mysql.jdbc.Driver 
db.default.url="jdbc:mysql://localhost/dbone?characterEncoding=UTF-8" 

db.dbtwo.driver=com.mysql.jdbc.Driver 
db.dbtwo.url="jdbc:mysql://localhost/dbtwo?characterEncoding=UTF-8" 


嘗試1失敗:兩個類都保存到數據庫1(dbone)中:
application.conf(第2部分):

ebean.default="*"
ebean.dbtwo="models.TestTwo"


嘗試2失敗:嘗試保存內容時出現錯誤:

[PersistenceException: The type [class models.TestTwo] is not a registered entity? If you don't explicitly list the entity classes to use Ebean will search for them in the classpath. If the entity is in a Jar check the ebean.search.jars property in ebean.properties file or check ServerConfig.addJar().]

application.conf(第2部分):

ebean.default="models.Test"
ebean.dbtwo="models.TestTwo"


我該如何設置以便將Test對象保存到dbone並將TestTwo對象保存到dbtwo?


編輯:TestTwo類的要求(沒什么特別的;我沒有手動分配一個Ebean服務器,除了在application.conf文件中):

package models;
import javax.persistence.*;
import play.db.ebean.*;
import play.data.validation.Constraints.Required;

@Entity
public class TestTwo extends Model{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;

    @Required
    public String testString;

    public static Model.Finder<Long, TestTwo> find = new Model.Finder<Long, TestTwo>(Long.class, TestTwo.class);

    public static TestTwo create (String testString){
        TestTwo test = new TestTwo();
        test.testString = testString;
        test.save();
        return test;
    }
}

我遇到了同樣的問題,這個問題是我在任何地方都可以找到的唯一相關主題。 解決該問題的方法不是很漂亮,但是我使屬於非默認ebean服務器的所有模型類都覆蓋了save() -, delete() -和update()方法。 這些覆蓋分別調用super.save(dbname)super.delete(dbname)super.update(dbname) ,其中dbname是ebean服務器的名稱。

啟動時,我將名稱從配置中拉出並保存,以免被硬編碼。 盡管它看起來很多余,但對我來說卻解決了這個問題。 我希望它可以幫助像我一樣在幾年后迷失於這個問題的其他人!

您是否編寫了演化腳本? 如果不是,則需要在conf>evolutions>default>1.sql編寫一個

創建表“ testtwo”:

create table testtwo(
    id          bigint auto_increment not null,
    testString      varchar(100) not null,
    constraint t1_testTwo primary key(id)
);

進一步:

SET FOREIGN_KEY_CHECKS=0;

drop table if exists testto;

SET FOREIGN_KEY_CHECKS=1;

刷新瀏覽器時,播放將要求“應用進化”,這將在數據庫中創建“ testtwo”表,您可以在其中保存實體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM